2015-02-10 148 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()上的每個高速賽車單獨