题目二 交通工具通用程序:为某研究所编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都是3个整数A、B、C的表达式。现有两种工具:Car007 和Plane,其中Car007 的速度运算公式为:A*B/C,Plane 的速度运算公式为:A+B+C。需要编写三个类:ComputeTime.java,Plane.java,Car007.java和接口Common.java,要求在未来如果增加第3种或多种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。其运行过程如下,从命令行输入ComputeTime的四个参数,第一个是交通工具的类型,第二、三、四个参数分别是整数A、B、C,举例如下:
时间: 2024-02-05 07:12:14 浏览: 26
Matlab数学建模工具箱_matlab_建模工具箱_数学建模_数学建模程序_
5星 · 资源好评率100%
Common.java
```java
public interface Common {
public double computeTime();
}
```
Car007.java
```java
public class Car007 implements Common{
private int A;
private int B;
private int C;
public Car007(int A, int B, int C) {
this.A = A;
this.B = B;
this.C = C;
}
@Override
public double computeTime() {
return 1000.0 / (A * B / C);
}
}
```
Plane.java
```java
public class Plane implements Common{
private int A;
private int B;
private int C;
public Plane(int A, int B, int C) {
this.A = A;
this.B = B;
this.C = C;
}
@Override
public double computeTime() {
return 1000.0 / (A + B + C);
}
}
```
ComputeTime.java
```java
public class ComputeTime {
public static void main(String[] args) {
String type = args[0];
int A = Integer.parseInt(args[1]);
int B = Integer.parseInt(args[2]);
int C = Integer.parseInt(args[3]);
Common common = null;
switch (type) {
case "Car007":
common = new Car007(A, B, C);
break;
case "Plane":
common = new Plane(A, B, C);
break;
default:
System.out.println("Invalid type!");
return;
}
double time = common.computeTime();
System.out.println("The time needed to run 1000 km is: " + time + " hours");
}
}
```
阅读全文