Java学习----面向对象习题练习

今日练习如下:

1、猜数字游戏

  • 需要记录猜数的整个过程
  • 根据猜测的次数进行评价
package com.han4;

import java.util.Scanner;

public class GuessGame {
	private int target;
	private int[] history = new int[15];
	private int count;
	private boolean bb = false;

	public void setTarget(int target) {
		this.target = target;
	}

	public void begin() {
		Scanner sc = new Scanner(System.in);
		while (count < 15) {
			System.out.println("第" + (count + 1) + "次猜测,请输入数字:输入-1退出");
			int tmp = sc.nextInt();
			if (tmp == -1) {
				System.exit(0);
			}
			String res = guess(tmp);
			if ("成功".equals(res)) {
				break;
			} else {
				System.out.println(res);
			}

		}
		if (count < 5) {
			System.out.println("牛");
		} else if (count < 10) {
			System.out.println("厉害");
		} else if (count < 15) {
			System.out.println("一般");
		} else {
			System.out.println("菜");
		}
		showHistory();
		sc.close();
	}

	private String guess(int num) {
		String res = "";
		if (num == target) {
			bb = true;
			res = "成功";
		} else if (num > target) {
			res = "猜大了!";
		} else {
			res = "猜小了!";
		}
		history[count++] = num;
		return res;
	}

	private void showHistory() {
		if (bb)
			System.out.println("猜对了");

		for (int i = 0; i < count; i++) {
			System.out.println(history[i]);
		}
	}
}
package com.han4;

public class Test1 {

	public static void main(String[] args) {
		GuessGame gg = new GuessGame();
		gg.setTarget(1234);
		gg.begin();
	}

}

 

2、计算圆的面积和周长,输入半径值

package com.han4;

public class SuanYuan {
	private double r;
	private final double PI = 3.14;

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	double getArea() {
		return PI * r * r;
	}

	double getPerimeter() {
		return 2 * PI * r;
	}
}

 

package com.han4;

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		System.out.println("请输入半径:");
		Scanner sc = new Scanner(System.in);
		SuanYuan r1 = new SuanYuan();
		while (true) {
			double r = sc.nextDouble();
			if (r < 0) {
				break;
			} else {
				r1.setR(r);
				System.out.println("半径的圆面积为:" + r1.getArea() + ",周长为:" + r1.getPerimeter());
			}
		}
		sc.close();
	}

}

3、请定义一个交通工具Vehicle的类,其中有:
属性:速度(speed),车的类型(type);方法:移动(move()),设置速度(setSpeed(double s)),加速
speedUp(double s),减速speedDown(double s)。最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过构造方法给它初始化speed,type的值,并且打印出来。另外,调用加速,减速的方法对速度进行改变。

package com.han5;

public class Vehicle {
	private double speed;
	private String type;

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	void move() {
		System.out.println("以" + speed + "的速度运行");
	}

	public void speedUp(double s) {
		speed += s;
	}

	public void speedDown(double s) {
		speed -= s;
	}

	public void show() {
		System.out.println(type + "的速度为" + speed);
	}
}
package com.han5;

public class Test5 {

	public static void main(String[] args) {
		Vehicle vh1 = new Vehicle();
		vh1.setSpeed(10);
		vh1.setType("自行车");
		vh1.show();
		vh1.move();
		vh1.speedUp(3);
		vh1.speedUp(2);
		vh1.show();
		vh1.speedDown(1);
		vh1.move();
		vh1.speedDown(4);
		vh1.show();
	}

}

 

4、利用继承来描述人,老师,学生之间的关系

  • 老师含有属性:name,age,subject,含有方法:eat,teach,toString
  • 学生含有属性:name,age,class,含有方法:eat,study,toString
  • 利用继承的思想,从老师和学生中抽象出人,并创建Test进行调用并输出(System.out)
package com.han6;

public class Test1 {
	public static void main(String[] args) {
		Teacher t = new Teacher();
		t.setName("老王");
		t.setAge(32);
		t.setSubject("数学");
		System.out.println(t);
		Student s = new Student();
		s.setName("小明");
		s.setAge(18);
		s.setClz("三年级二班");
		System.out.println(s);
	}

}

class Person {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void eat() {
		System.out.println("正在吃饭...");
	}

	public String toString() {
		return "姓名为:" + name + ",现在年龄为:" + age;
	}
}

class Teacher extends Person {
	private String subject;

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void teach() {
		System.out.println("正在上课...");
	}

	public String toString() {
		return "姓名为:" + getName() + ",现在年龄为:" + getAge() + ",教授课程为:" + subject;
	}

}

class Student extends Person {
	private String clz;

	public String getClz() {
		return clz;
	}

	public void setClz(String clz) {
		this.clz = clz;
	}

	public void study() {
		System.out.println("正在上课...");
	}

	public String toString() {
		return "姓名为:" + getName() + ",现在年龄为:" + this.getAge() + ",所在班级为:" + clz;
	}
}

5、定义一个类Calculate1,实现加、减两种运算,然后编写一个派生类Calculate2,实现乘除两种算法

package com.han6;

//定义一个类Calculate1,实现加、减两种运算,然后编写一个派生类Calculate2,实现乘除两种算法
public class Test2 {
	public static void main(String[] args) {
		Calculate1 c1 = new Calculate1();
		c1.setNum1(2);
		c1.setNum2(3);
		System.out.println(c1.add());
		System.out.println(c1.sub());

		Calculate2 c2 = new Calculate2();
		c2.setNum1(6);
		c2.setNum2(3);
		System.out.println(c2.add());
		System.out.println(c2.sub());
		System.out.println(c2.mul());
		System.out.println(c2.div());
	}
}

class Calculate1 {
	private int num1;
	private int num2;

	public int getNum1() {
		return num1;
	}

	public void setNum1(int num1) {
		this.num1 = num1;
	}

	public int getNum2() {
		return num2;
	}

	public void setNum2(int num2) {
		this.num2 = num2;
	}

	public int add() {
		return num1 + num2;
	}

	public int sub() {
		return num1 - num2;
	}
}

class Calculate2 extends Calculate1 {
	public int mul() {
		return getNum1() * getNum2();
	}

	public double div() {
		return 1. * getNum1() / getNum2();
	}
}

 

你可能感兴趣的:(Java学习,学习)