我的目标是创建一个Dragster and Dragrace类。我的dragster类是完整的,但我迷失了如何完成我的DragRace类。主要方法应该显示这样的结果。
速度特性:反应时间:0.6,扭矩:1000,牵引力:0.8286078156824775,燃烧时间:3
参赛者时间:6.634217763058126秒。
我必须创建一个包含5个dragster的数组,然后显示每个dragsters的结果。
public class Dragster {
private int burnTime;
private double reactionTime;
private int torque;
private double traction;
public Dragster(double reactionTime, int torque, double traction, int burnTime) {
this.reactionTime = reactionTime;
this.torque = torque;
this.traction = traction;
this.burnTime = burnTime;
}
private double conditionTires(double time){
double effectiveness = 2.5*(Math.exp((time * time - (10 * time) + 25) / 50))/Math.sqrt(2 * Math.PI);
return effectiveness;
}
public void burnout(){
traction = traction * conditionTires(burnTime);
}
public double race(){
double TORQUE2TIME = 5000;
double raceTime = reactionTime + (1/(torque/ TORQUE2TIME)*conditionTires(burnTime));
return raceTime;
}
public String toString(){
return "Reaction Time:" + reactionTime + ", Torque:" + torque + "Traction:" + traction +
"Burn Time:" + burnTime;
}
}
public class DragRace {
private DragRace{
}
public static void main(String[] args){
ArrayList<Dragster> DragsterList = new ArrayList<Dragster>();
Dragster car1 = new Dragster(0.6, 1000, 0.9, 3);
Dragster car2 = new Dragster(0.4, 800, 1.0, 5);
Dragster car3 = new Dragster(0.5, 1200, 0.95, 7);
Dragster car4 = new Dragster(0.3, 1200, 0.95, 1);
Dragster car5 = new Dragster(0.4, 900, 0.9, 6);
DragsterList.add(car1);
DragsterList.add(car2);
DragsterList.add(car3);
DragsterList.add(car4);
DragsterList.add(car5);
DragsterList.toString();
}
}
我在戏剧课中迷失了下一步该做什么。如果有人能提供一些见解。
答案 0 :(得分:1)
只需这样做:
public void printResults() {
for (Dragster d: DragsterList) {
System.out.println(d.toString());
System.out.println();
System.out.println("Dragster time:" + d.race());
}
}
基本上,您只需要遍历列表中的每辆车,打印汽车,然后竞赛一次。
答案 1 :(得分:0)
您在程序结束时使用的toString方法是将列表本身转换为字符串,这不是您想要的。
您需要单独调用每个dragster上的toString()