Java 习题

2024年11月30日 06:55
有3个网友回答
网友(1):

//ComputeTime.java
public class ComputeTime {
public static void main(String[] args) throws Exception{
for(int i=0;i System.out.println(i+"-----"+args[i]);
}
if(args.length!=4){
System.out.println("参数个数不对,请重新输入!");
return;
}
Common com =(Common)Class.forName(args[0]).newInstance();
int a = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[2]);
int c = Integer.parseInt(args[3]);
double[] kk = com.getTime(a, b, c);
System.out.println(args[0]+"的速度为"+kk[0]+"公里/小时,走1000公里需要"+kk[1]+"小时");
}
}
//Common.java
public interface Common {
public double[] getTime(int A,int B,int C);

}
//Ship.java
public class Ship implements Common{

public double[] getTime(int A, int B, int C) {
double suDu = A*B*C;
double shiJian = 1000/suDu;
return new double[]{suDu,shiJian};
}

}
//Plane.java
public class Plane implements Common{

public double[] getTime(int A, int B, int C) {
double suDu = A+B+C;
double shiJian = 1000/suDu;
return new double[]{suDu,shiJian};
}

}
//..........交通工具类

网友(2):

//Common接口:
package com.jia.speedinterface;

public interface Common {
public String getToolName();
public double getSpeed(String a,String b,String c);
}
//---------------
//Car007类:
package com.test;

import com.jia.speedinterface.Common;

public class Car007 implements Common{
private double A;
private double B;
private double C;
public double getSpeed(String a, String b, String c) {
A = Double.parseDouble(a);
B = Double.parseDouble(b);
C = Double.parseDouble(c);
return Math.floor(A*B/C);
}

public String getToolName() {
// TODO Auto-generated method stub
return "Car007";
}

}
//----------------------------------------

//Plan类:

import com.jia.speedinterface.Common;

public class Plan implements Common{
private double A;
private double B;
private double C;
public double getSpeed(String a, String b, String c) {
A = Double.parseDouble(a);
B = Double.parseDouble(b);
C = Double.parseDouble(c);
return Math.floor(A+B+C);
}

public String getToolName() {
// TODO Auto-generated method stub
return "Plan";
}
}

//------------------------------------------
//Ship类:

package com.test;

import com.jia.speedinterface.Common;

public class Ship implements Common{

public double getSpeed(String a, String b,String c) {
// TODO Auto-generated method stub
double dis = Double.parseDouble(a);
double spe = Double.parseDouble(b);
double speed = dis*spe;
return speed;
}

public String getToolName() {
// TODO Auto-generated method stub
return "Ship";
}

}
//--------------------------------------------------
//演示类:
package com.test;

public class CheckSpeedDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
SpeedDemo.printSpeed(Ship.class,"50","60","70");
SpeedDemo.printSpeed(Plan.class,"50","60","70");
SpeedDemo.printSpeed(Car007.class,"50","60","70");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

网友(3):

比较基础的问题