1. 根据要求编写类,编写完成后使用下面的测试类去测试,要求如下:
(1)写 Duration 类
1.int类型属性hours、minutes、seconds
2.构造器(三参),对hours、minutes、seconds进行初始化
3.构造器(一参),对hours、minutes、seconds进行初始化,参数为总的seconds,例如:将x秒,转为a小时b分钟c秒,3661秒,就是1小时1分钟1秒
4.对每个属性提供get方法
5.getTotalSeconds()方法,用于返回总的seconds
6.toString()方法,代码为 return hours + ":" + minutes + ":" + seconds;
//测试类
public class Driver {
public static void main(String[] args) {
Track myTrack = new Track();
myTrack.setTitle("Watching The Wheels");
//Duration myDuration = new Duration(234);
Duration myDuration = new Duration(0, 3, 48);
myTrack.setDuration(myDuration);
System.out.println("myTrack is: " + myTrack);
System.out.println("Title = " + myTrack.getAddress ());
System.out.println("Duration = " + myTrack.getDuration().toString());
}
}
(2)写 Track 类
1.String类型属性title、Duration类型属性duration
2.对每个属性提供get和set方法
3.toString()方法,代码为 return "my Track address is: " + title + " duration is: " + duration;
(1)Duration类的编写:
public class Duration {
private int hours;
private int minutes;
private int seconds;
public Duration() {}
public Duration(int hours,int minutes,int seconds) {
this.hours=hours;
this.minutes=minutes;
this.seconds=seconds;
}
public Duration(int totalSeconds) {
this.hours=totalSeconds/3600;
this.minutes=(totalSeconds-hours*3600)/60;
this.seconds=totalSeconds-hours*3600-minutes*60;
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
public int getTotalSeconds() {
return hours*3600+minutes*60+seconds;
}
@Override
public String toString() {
return hours + ":" + minutes + ":" + seconds;
}
}
(2)Track类的编写:
public class Track {
private String title;
private Duration duration;
public void setTitle(String title) {
this.title=title;
}
public String getTitle() {
return title;
}
public void setDuration(Duration duration) {
this.duration=duration;
}
public Duration getDuration() {
return duration;
}
@Override
public String toString() {
return "my Track address is: " + title + " duration is: " + duration;
}
}
2. 某汽车租赁公司有多种汽车可以出租;使用面向对象的方式,分析此场景,写出对应的代码结构。
1.Vehicle是所有车的父类,有属性:品牌、车牌号,有返回总租金的方法getsumrent(int days)
2.小轿车类Car是Vehicle的子类,属性:车型(两厢、三厢、越野),两厢每天300,三厢每天350,越野每天500。
3.多座汽车类=是Vehicle的子类,属性:座位数,座位数<=16的每天400,座位数>16的每天600。
编写代码:
public class Vehicle {
//品牌
private String kind;
//车牌
private String carId;
public int getsumrent(int days) {
return 0;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getCarId() {
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
@Override
public String toString() {
return "Vehicle [kind=" + kind + ", carId=" + carId + "]";
}
public Vehicle(String kind, String carId) {
super();
this.kind = kind;
this.carId = carId;
}
public Vehicle() {
super();
}
}
class Car extends Vehicle{
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Car [type=" + type + "]";
}
public Car(String kind, String carId, String type) {
super(kind, carId);
this.type = type;
}
public Car() {}
@Override
public int getsumrent(int days) {
if(type.equals("越野车")) {
return days*500;
}else if(type.equals("两厢车")) {
return days*300;
}else if(type.equals("三厢车")) {
return days*350;
}else {
return 0;
}
}
class Bus extends Vehicle{
private int pNum;
public int getpNum() {
return pNum;
}
public void setpNum(int pNum) {
this.pNum = pNum;
}
@Override
public String toString() {
return "bus [pNum=" + pNum + "]";
}
public Bus(String kind, String carId, int pNum) {
super(kind, carId);
this.pNum = pNum;
}
public Bus(String kind, String carId) {
super(kind, carId);
}
@Override
public int getsumrent(int days) {
if(pNum>16) {
return days*600;
}else{
return days*400;
}
}
}