1.猜数字游戏
public class GuessGame {
private int aa = 0;
private int[] history = new int[15];
private int count = 0;
public GuessGame() {
aa = (int) (Math.random() * 100);
}
public boolean guess(int num) {
history[count++] = num;
boolean res = false;
if (num > aa)
System.out.println("你猜大了");
else if (num < aa)
System.out.println("你猜小了");
else {
System.out.println("你猜对了");
res = true;
}
return res;
}
public static void main(String[] args) {
GuessGame ff = new GuessGame();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入你要猜测的正整数值:");
int kk = sc.nextInt();
boolean ret = ff.guess(kk);
if (ret)
break;
}
sc.close();
}
}
2.计算圆的周长和面积
public class Text1 {
private double redius;// 半径值
public void Circle(double r) {
redius = r;
}
public double getAre() {
return Math.PI * redius * 2;
}
public double getRound() {
return Math.PI * redius * redius;
}
public static void main(String[] args) {
Text1 ff = new Text1();
ff.Circle(2);
System.out.println(ff.getAre());
System.out.println(ff.getRound());
}
}
3.请定义一个交通工具Vehicle的类,其中有:
属性:速度(speed)车的类型(t0ype)
方法:移动(move()) ,设置速度(setSpeed(double s )), 加速speedUP(double s),减速speedDown(double s). 最后再测试类中Vehicle中的main()中实例化一个交通工具对象
并通过构造方法给它初始化speed,type的值,并打印出来。另外,调用加速,减速的方法对速度进行改变
public class Vehicle {
private double speed;
private String type;
public void move() {
}
public String setType(String type) {
type = type;
return type;
}
public double setSpeed(double s) {
speed = s;
return speed;
}
public double speedUp(double s) {
speed += s;
return speed;
}
public double speedDown(double s) {
speed -= s;
if (speed <= 0)
speed = 0;
return speed;
}
public static void main(String[] args) {
Vehicle ss = new Vehicle();
System.out.println(ss.setType("aaa00"));
System.out.println(ss.setSpeed(2.0));
System.out.println(ss.speedDown(1.3));
System.out.println(ss.speedUp(3.3));
}
}
4.利用继承来描述人,老师,学生之间的关系
1)老师含有属性:name age subject 含有方法:eat teach toString
2)学生含有属性:name age class 含有方法:eat study toString
3)利用继承思想从老师和学生中抽象出人,并创建Text 进行调用并输出(System.out)
public class Person {
private String name;
private int age;
private String subject;
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 String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
class Teacher extends Person {
public void teach(String subject) {
System.out.println("老师教:" + subject);
}
public void eat(String something) {
System.out.println("吃饭:" + something);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "姓名:" + this.getName() + "年龄:" + this.getAge();
}
}
class Student extends Person {
private String class1;
public void study(String subject) {
System.out.println("学生学:" + subject);
}
public void eat(String something) {
System.out.println("吃饭:" + something);
}
public void class1(String class1) {
System.out.println("学生班级:" + class1);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "姓名:" + this.getName() + "年龄:" + this.getAge();
}
public String getClass1() {
return class1;
}
public void setClass1(String class1) {
this.class1 = class1;
}
}
测试类:
public class Text2 {
public static void main(String[] args) {
Teacher dd = new Teacher();
dd.setName("zzz");
dd.setAge(34);
System.out.println(dd.toString());
dd.teach("数学");
dd.eat("饺子");
Student aa = new Student();
aa.setName("www");
aa.setAge(10);
System.out.println(aa.toString());
aa.class1("三年级1班");
aa.eat("面");
aa.study("数学");
}
}
5.定义一个类Calculate1,实现加减两种运算,然后编写一个派生类Calculate2,实现乘除两种算法
public class Calculate1 {
private double num1;
private double num2;
public double add() {
return num1 + num2;
}
public double sub() {
return num1 - num2;
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
}
class Calculate2 extends Calculate1 {
public double multiple() {
return this.getNum1() * this.getNum2();
}
public double didivision() {
return this.getNum1() / this.getNum2();
}
}
测试类:
public class Text3 {
public static void main(String[] args) {
Calculate2 aa = new Calculate2();
aa.setNum1(2.2);
aa.setNum2(2.3);
System.out.println(aa.add());
System.out.println(aa.sub());
System.out.println(aa.multiple());
System.out.println(aa.didivision());
}
}