设计Flight类。要求:(1)具有机型、班次、额定载客数和实际载客数等属性;(2)具有输入/输出属性功能;(3)求载客率;(4)重载toString方法,用于对象属性值的输出
时间: 2024-01-21 22:02:34 浏览: 99
下面是一个符合要求的Flight类的示例:
```
public class Flight {
private String aircraftType;
private String flightNumber;
private int capacity;
private int actualPassengers;
public Flight(String aircraftType, String flightNumber, int capacity, int actualPassengers) {
this.aircraftType = aircraftType;
this.flightNumber = flightNumber;
this.capacity = capacity;
this.actualPassengers = actualPassengers;
}
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入机型:");
this.aircraftType = scanner.nextLine();
System.out.print("请输入班次:");
this.flightNumber = scanner.nextLine();
System.out.print("请输入额定载客数:");
this.capacity = scanner.nextInt();
System.out.print("请输入实际载客数:");
this.actualPassengers = scanner.nextInt();
}
public void output() {
System.out.println("机型:" + this.aircraftType);
System.out.println("班次:" + this.flightNumber);
System.out.println("额定载客数:" + this.capacity);
System.out.println("实际载客数:" + this.actualPassengers);
System.out.println("载客率:" + this.getLoadFactor());
}
public double getLoadFactor() {
return (double) this.actualPassengers / this.capacity;
}
@Override
public String toString() {
return "Flight{" +
"aircraftType='" + aircraftType + '\'' +
", flightNumber='" + flightNumber + '\'' +
", capacity=" + capacity +
", actualPassengers=" + actualPassengers +
'}';
}
}
```
在这个示例中,Flight类包含了机型、班次、额定载客数和实际载客数等属性,并提供了输入/输出属性的功能。其中,input方法用于从控制台读取用户输入,output方法用于将对象的属性值输出到控制台。此外,Flight类还提供了getLoadFactor方法,用于计算载客率。最后,重载了toString方法,用于将对象的属性值以字符串的形式输出。
阅读全文