Compile a generalProgramIt is used to calculate the time required for each vehicle to run 1000 kilometers. It is known that the parameters of each vehicle are expressions of three integers A, B, and C. Two existing tools are available: Car and plane. The formula for calculating the car speed is a * B/c, and the formula for calculating the plane speed is A + B + C. You need to write three types: computetime. java, plane. java, car007.java and common interfaces. java requires that you do not have to modify any of the previous programs if you want to add 3rd types of transportation tools in the future. You only need to write new transportation programs. The running process is as follows: Enter the four parameters of computetime from the command line. The first is the type of the vehicle, and the second, third, and fourth parameters are integers A, B, and C, respectively. The example is as follows:
Computing Time of plane: "Java computetime plane 20 30 40"
Car007 calculation time: "Java computetime car007 23 34 45"
If the 3rd types of transportation tools are ship, you only need to write ship. Java, and enter: "Java computetime ship 22 33 44"
Tip: Make full use of the interface concept. The interface object acts as a parameter.
Another way to instantiate an object: class. forname (STR ). newinstance (); for example, to instantiate a plane object, you only need to call the class. forname ("plane "). newinstance.
AnswerCode:
Access the classpath path. compile it from the bottom up.
Directory structure
Caltime
-------- | --------
|
|
Vehicle computtime. Java
|
---------
|
|
All palne. Java/car. Java
|
|
Common. Java
The Code is as follows:
1. computtime. Java ensure that the input is correct, and the numberfromatexception is not captured.
Import caltime. vehicle. All. Common;
Import java. Lang .*;
Public class computetime {
Public static void main (string ARGs []) {
System. Out. println ("vehicle:" + ARGs [0]);
System. Out. println ("parameter A:" + ARGs [1]);
System. Out. println ("parameter B:" + ARGs [2]);
System. Out. println ("parameter 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 ("average speed:" + V + "km/h ");
System. Out. println ("running time:" + T + "Hour ");
} 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 );
}
Demonstrate the classic interface usage. It can only be said. Thinking in Java has made a profound analysis on this. You can check it.