1.类与对象
(1)设计一个银行类MyBank,初始化时没有钱,saveMoney方法实现存钱,参数是float类型,withdrawMoney取钱,参数是float类型,取钱需要付1元手续费,如果取钱数超过存款数,取钱操作失败,返回“not enough”,getDeposit方法获取当前存款数(返回float类型)
class MyBank { float money =0; void saveMoney(float amout) { money=money+amout; } void withdrawMoney(float amout) { if(amout<=money-1) { money -=(amout+1); } else { System.out.println("not enough"); } } float getDeposit()//返回类型没有参数 { return money; } }
测试 | 期望输出 | 实际输出 | ||
---|---|---|---|---|
MyBank bank=new MyBank(); bank.saveMoney(100); bank.withdrawMoney(80); System.out.println(bank.getDeposit()); |
19.0 |
19.0 |
||
MyBank bank=new MyBank(); bank.saveMoney(100); bank.withdrawMoney(100); |
not enough |
not enough |
(2)观察以下代码,在注释部分添加代码。
要求在Student类中,(task1)添加一个有name和ID两个参数的构造方法,对成员变量name和ID进行初始化,(task2)实例化一个Student对象,学生姓名:Yaoming,ID:123456,通过两次调用addScore,给总分分别加上90和95,利用getTotalScore()输出名字+总分。
注:使用System.out.println()方法输出。
public class Main { public static void main(String[] args) { //task2:write code here Student a =new Student("Yaoming",123456); a.addScore(90); a.addScore(95); a.getTotalScore(); } } class Student{ private String name; private int ID; private int total; //task1:write code here public Student(String name,int ID) { this.name=name; this.ID=ID; } void addScore(int a){ total=total+a; } void getTotalScore() { String s=name+":"+total; System.out.println(s); } }
期望输出 | 实际输出 | ||
---|---|---|---|
Yaoming:185 |
Yaoming:185 |
2.继承与多态
(1)有个People类如下,设计一个子类Student,继承People类,增加一个public void study()方法,让wealthValue增加1.重写父类的play()方法,让财富值-2.Student的构造方法中,将name前加上student:
class People{
public int wealthValue=100;
public String name;
public People(String name){
this.name=name;
}
public void eat(){
wealthValue--;
}
public void play(){
wealthValue--;
}
public int getWealthValue(){
return wealthValue;
}
}
class Student extends People { public void study() { wealthValue=wealthValue+1; } public void play() { wealthValue=wealthValue-2; } public Student(String name) { super("student:"+name); } }
(2)设计一个圆类Circle,包含私有的成员变量radius(半径);公有的构造方法,对radius进行初始化;公有的方法double getArea(),获取圆的面积;公有的方法double getPerimeter(),获取圆的周长。
设计一个圆柱体类Cylinder,继承Circle类,还包含私有的成员变量height(圆柱体的高);公有的方法double getVolume(),获取圆柱体体积。
说明:圆周率π取Math类中的类常量PI,即Math.PI 。
class Circle { private double radius; public Circle(double radius)//不能忘 { this.radius=radius; } public double getArea() { return radius*radius*Math.PI; } public double getPerimeter() { return 2*radius*Math.PI; } } class Cylinder extends Circle { private double height; public Cylinder(double radius,double height) { super(radius); this.height=height; } public double getVolume() { return height*getArea(); } }
测试 | 期望输出 | 实际输出 | ||
---|---|---|---|---|
Circle c=new Cylinder(5,8);//radius is 5,height is 8. System.out.printf("The circle`s area is:%.2f,perimeter is:%.2f.\n",c.getArea(),c.getPerimeter()); System.out.printf("The cylinder`s volume is:%.2f.\n",((Cylinder)c).getVolume()); |
The circle`s area is:78.54,perimeter is:31.42. The cylinder`s volume is:628.32. |
The circle`s area is:78.54,perimeter is:31.42. The cylinder`s volume is:628.32. |
||
Circle c=new Cylinder(4,5); System.out.printf("The circle`s area is:%.2f,perimeter is:%.2f.\n",c.getArea(),c.getPerimeter()); System.out.printf("The cylinder`s volume is:%.2f.\n",((Cylinder)c).getVolume()); |
The circle`s area is:50.27,perimeter is:25.13. The cylinder`s volume is:251.33. |
The circle`s area is:50.27,perimeter is:25.13. The cylinder`s volume is:251.33. |
||
Circle c=new Cylinder(3,3); System.out.printf("The circle`s area is:%.2f,perimeter is:%.2f.\n",c.getArea(),c.getPerimeter()); System.out.printf("The cylinder`s volume is:%.2f.\n",((Cylinder)c).getVolume()); |
The circle`s area is:28.27,perimeter is:18.85. The cylinder`s volume is:84.82. |
The circle`s area is:28.27,perimeter is:18.85. The cylinder`s volume is:84.82. |
3.接口与异常类
(1)设计一个Car类,有一个车龄属性,构造函数初始化车龄
设计一个Company类,有个一个估值属性,构造函数初始化公司的估值
Car和Company都可以买保险,保险费车的是车龄乘以100,公司是估值除以100
设计一个Insurable接口,包括一个抽象方法getFee();
现在有个系统已经实现的People类中包含一个方法display(Insuable s),能够打印出输入参数的费用
根据下面测试样例的调用方式,你将如何设计?请完成类和接口的设计
例如:
测试 | Result |
---|---|
Car p=new Car(20); Company c=new Company(1000000); People.display(p); People.display(c); |
2000.0 10000. |
public class Main { public static void main(String[] args) { Car p=new Car(20); Company c=new Company(1000000); People.display(p); People.display(c); } } interface Insurable { double get(); } class Car implements Insurable { int Carage; public Car(int Cargae) { this.Carage=Cargae; } @Override public double get() { return Carage * 100; } } class Company implements Insurable { int Volua; public Company(int Volua) { this.Volua=Volua; } @Override public double get() { return Volua/100; } } class People { public static void display(Insurable s) { System.out.println(s.get()); } }
测试 | 期望输出 | 实际输出 | |
---|---|---|---|
Car p=new Car(20); Company c=new Company(1000000); People.display(p); People.display(c); |
2000.0 10000.0 |
2000.0 10000. |
(2)现有一个类Game,其中一个方法是void input(int i),调用该方法时,如果输入的数据是7会抛出异常。现要求设计一个类Person,包括一个void play(int number)方法,该方法中,要求实例化Game,并调用input方法,将变量number作为参数输入到input方法中。如果出现异常,输出error
例如:
测试 | Result |
---|---|
Person p=new Person(); p.play(5); |
5 |
Person p=new Person(); p.play(7); |
error |
class Game{ void input(int i)throws Exception { if(i==7) { throw new Exception(); } else { System.out.println(i); } } } class Person { void play(int number) { Game game = new Game(); try { game.input(number); } catch (Exception e) { System.out.println("error"); } } }
Person p=new Person(); p.play(5); |
5 |
5 |
|
Person p=new Person(); p.play(7); |
error |
error |