2015-02-10 251 views
1

我的目标是创建一个Dragster和Dragrace类。我的dragster类是完整的,但我迷失在如何完成我的DragRace类。主要方法应该显示这样的结果。Java构造函数问题

高速赛车特征:反应时间:0.6,扭矩:1000,牵引:0.8286078156824775,燃烧时间:3

高速赛车时间:6.634217763058126秒。

我必须创建一个数组,其中包含5个拖动器,然后显示每个拖放器的结果。

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(); 



    } 
} 

因为下一步该做什么,我迷失在dragrace课堂。如果有人可以提供一些见解。

+2

你必须有一个比赛方法,将通过所有的高速赛车对象迭代和计算比赛时间,并显示结果。 – Kode 2015-02-10 22:27:16

回答

1

只要做到这一点:

public void printResults() { 
    for (Dragster d: DragsterList) { 
     System.out.println(d.toString()); 
     System.out.println(); 
     System.out.println("Dragster time:" + d.race()); 
    } 
} 

基本上,你只是通过每节车厢需要循环在你的名单,打印车,比赛一次。

0

您在程序结束时使用的toString方法将列表本身转换为字符串,这不是您想要的。

您将要调用toString()上的每个高速赛车单独