编写一个通用程序,用来计算每一种交通工具运行1000公里所需的时间,已知每种交通工具的参数都是3个整数A、B、C的表达式。现有两种工具:Car 和Plane,其中Car 的速度运算公式为:A
*
B
/
C,Plane 的速度运算公式为:A
+
B
+
C。需要编写三类:ComputeTime.java,Plane.java,Car007.java和接口Common.java,要求在未来如果增加第3种交通工具的时候,不必修改以前的任何程序,只需要编写新的交通工具的程序。其运行过程如下,从命令行输入ComputeTime的四个参数,第一个是交通工具的类型,第二、三、四个参数分别时整数A、B、C,举例如下:
计算Plane的时间: " java ComputeTime Plane 20 30 40 "
计算Car007的时间: " java ComputeTime Car007 23 34 45 "
如果第3种交通工具为Ship,则只需要编写Ship.java,运行时输入: " java ComputeTime Ship 22 33 44 "
提示:充分利用接口的概念,接口对象充当参数。
实例化一个对象的另外一种办法:Class.forName(str).newInstance();例如需要实例化一个Plane对象的话,则只要调用Class.forName( " Plane " ).newInstance()便可。
解答代码:
访到classpath 路径下即可,请从下往上编译
目录结构
CalTime
--------|--------
| |
| |
vehicle ComputTime.java
|
---------
| |
| |
all Palne.java / Car.java
|
|
Common.java
代码如下:
1 . ComputTime.java 请确保输入正确,其中没有捕捉NumberFromatException
import CalTime.vehicle.all.Common;
import java.lang. * ;
public class ComputeTime {
public static void main(String args[]) {
System.out.println( " 交通工具: " + args[ 0 ]);
System.out.println( " 参数A: " + args[ 1 ]);
System.out.println( " 参数B: " + args[ 2 ]);
System.out.println( " 参数C: " + args[ 3 ]);
double A = Double.parseDouble(args[ 1 ]);
double B = Double.parseDouble(args[ 2 ]);
double C = Double.parseDouble(args[ 3 ]);
double v,t;
try {
Common d = (Common) Class.forName( " CalTime.vehicle. " + args[ 0 ]).newInstance();
v = d.runTimer(A,B,C);
t = 1000 / v;
System.out.println( " 平均速度: " + v + " km/h " );
System.out.println( " 运行时间: " + t + " 小时 " );
} catch (Exception e) {
System.out.println( " class not found " );
}
}
}
2 .Plane.java
package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Plane implements Common {
public double runTimer( double a, double b, double c) {
return (a + b + c);
}
}
3 . Car.java
package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Car implements Common {
public double runTimer( double a, double b, double c) {
return ( a * b / c );
}
}
4 .Common.java
package CalTime.vehicle.all;
public interface Common {
double runTimer( double a, double b, double c);
}
演示了接口的经典使用方法。嗬嗬,只有意会,不可言传。 Thinking in Java 对此也做出了深刻的分析,可以查看下。
本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/neuqnwc/archive/2005/10/21/512090.aspx
计算Plane的时间: " java ComputeTime Plane 20 30 40 "
计算Car007的时间: " java ComputeTime Car007 23 34 45 "
如果第3种交通工具为Ship,则只需要编写Ship.java,运行时输入: " java ComputeTime Ship 22 33 44 "
提示:充分利用接口的概念,接口对象充当参数。
实例化一个对象的另外一种办法:Class.forName(str).newInstance();例如需要实例化一个Plane对象的话,则只要调用Class.forName( " Plane " ).newInstance()便可。
解答代码:
访到classpath 路径下即可,请从下往上编译
目录结构
CalTime
--------|--------
| |
| |
vehicle ComputTime.java
|
---------
| |
| |
all Palne.java / Car.java
|
|
Common.java
代码如下:
1 . ComputTime.java 请确保输入正确,其中没有捕捉NumberFromatException
import CalTime.vehicle.all.Common;
import java.lang. * ;
public class ComputeTime {
public static void main(String args[]) {
System.out.println( " 交通工具: " + args[ 0 ]);
System.out.println( " 参数A: " + args[ 1 ]);
System.out.println( " 参数B: " + args[ 2 ]);
System.out.println( " 参数C: " + args[ 3 ]);
double A = Double.parseDouble(args[ 1 ]);
double B = Double.parseDouble(args[ 2 ]);
double C = Double.parseDouble(args[ 3 ]);
double v,t;
try {
Common d = (Common) Class.forName( " CalTime.vehicle. " + args[ 0 ]).newInstance();
v = d.runTimer(A,B,C);
t = 1000 / v;
System.out.println( " 平均速度: " + v + " km/h " );
System.out.println( " 运行时间: " + t + " 小时 " );
} catch (Exception e) {
System.out.println( " class not found " );
}
}
}
2 .Plane.java
package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Plane implements Common {
public double runTimer( double a, double b, double c) {
return (a + b + c);
}
}
3 . Car.java
package CalTime.vehicle;
import CalTime.vehicle.all.Common;
public class Car implements Common {
public double runTimer( double a, double b, double c) {
return ( a * b / c );
}
}
4 .Common.java
package CalTime.vehicle.all;
public interface Common {
double runTimer( double a, double b, double c);
}
演示了接口的经典使用方法。嗬嗬,只有意会,不可言传。 Thinking in Java 对此也做出了深刻的分析,可以查看下。
本文来自CSDN博客,转载请标明出处:http: // blog.csdn.net/neuqnwc/archive/2005/10/21/512090.aspx