求高手用JAVA编写一个通用程序,计算交通工具运行1000KM所需要时间.
已知每种交通工具速度都是3个整数ABC的表达式,其中Car007速度公式为A*B/C,Plane为A+B+C,编写ComputeTime.java;Plane.java;...
已知每种交通工具速度都是3个整数ABC的表达式,
其中Car007速度公式为A*B/C,Plane为A+B+C,
编写ComputeTime.java;Plane.java;Car007.java和接口Common.java
要求增加第三种交通工具时不必改动现有程序,只需编写新的交通工具程序 展开
其中Car007速度公式为A*B/C,Plane为A+B+C,
编写ComputeTime.java;Plane.java;Car007.java和接口Common.java
要求增加第三种交通工具时不必改动现有程序,只需编写新的交通工具程序 展开
2个回答
展开全部
package test;
public interface Common {
public double computeSpeed(double a, double b, double c);
}
public class Car007 implements Common{
@Override
public double computeSpeed(double a, double b, double c) {
// TODO Auto-generated method stub
if(c == 0.0) {
throw new ArithmeticException("除数为0");
}
if( a * b < 0.0) {
try {
throw new Exception("速度不能为负数");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("a = " + a + "; b = " + b);
}
}
return a * b / c;
}
}
public class Plane implements Common{
@Override
public double computeSpeed(double a, double b, double c) {
// TODO Auto-generated method stub
if( a + b + c < 0.0) {
try {
throw new Exception("速度不能为负数");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("a = " + a + "; b = " + b + ";c = " + c);
}
}
return a + b + c;
}
}
public class ComputeTime {
/*
*调用时, s = 1000
*/
public double getTime(double s, double a, double b, double c) {
double time = Integer.MAX_VALUE;//表示车速为0,不可能到达
double speed = new Car007().computeSpeed(a, b, c);
if(!(speed == 0.0)) {
time = s / speed;
}
return time;
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new ComputeTime().getTime(1000, 10, 20, 30));
}
}
//计算结果:150.0
public interface Common {
public double computeSpeed(double a, double b, double c);
}
public class Car007 implements Common{
@Override
public double computeSpeed(double a, double b, double c) {
// TODO Auto-generated method stub
if(c == 0.0) {
throw new ArithmeticException("除数为0");
}
if( a * b < 0.0) {
try {
throw new Exception("速度不能为负数");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("a = " + a + "; b = " + b);
}
}
return a * b / c;
}
}
public class Plane implements Common{
@Override
public double computeSpeed(double a, double b, double c) {
// TODO Auto-generated method stub
if( a + b + c < 0.0) {
try {
throw new Exception("速度不能为负数");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("a = " + a + "; b = " + b + ";c = " + c);
}
}
return a + b + c;
}
}
public class ComputeTime {
/*
*调用时, s = 1000
*/
public double getTime(double s, double a, double b, double c) {
double time = Integer.MAX_VALUE;//表示车速为0,不可能到达
double speed = new Car007().computeSpeed(a, b, c);
if(!(speed == 0.0)) {
time = s / speed;
}
return time;
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new ComputeTime().getTime(1000, 10, 20, 30));
}
}
//计算结果:150.0
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
学习了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询