Reputation: 133
My objective is to create a Dragster and Dragrace class. My dragster class is complete, but I'm lost on how to finish my DragRace class. The main method is supposed to display results like this.
Dragster characteristics: Reaction Time: 0.6, Torque: 1000, Traction: 0.8286078156824775, Burn Time: 3
Dragster time: 6.634217763058126 seconds.
I have to create an array that holds 5 dragsters then displays the results for each one.
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();
}
}
I'm lost in the dragrace class as what to do next. If anyone could give some insight.
Upvotes: 1
Views: 72
Reputation: 1
The toString method you are using at the end of the program is converting the list itself to a string, which isn't what you want.
You will want to call toString() on each dragster individually
Upvotes: 0
Reputation: 3537
Simply do this:
public void printResults() {
for (Dragster d: DragsterList) {
System.out.println(d.toString());
System.out.println();
System.out.println("Dragster time:" + d.race());
}
}
Basically, you just need to loop through each car in your list, print the car, and race it once.
Upvotes: 1