Java构造函数问题
我的目标是创建一个Dragster和Dragrace类。我的德拉格斯特课程已经完成了,但我对如何完成德拉格蕾丝课程感到困惑。主要方法应该显示这样的结果 牵引特性:反应时间:0.6,扭矩:1000,牵引力:0.828607815682475,燃烧时间:3 拖动时间:6.634217763058126秒 我必须创建一个包含5个dragster的数组,然后显示每个dragster的结果Java构造函数问题,java,Java,我的目标是创建一个Dragster和Dragrace类。我的德拉格斯特课程已经完成了,但我对如何完成德拉格蕾丝课程感到困惑。主要方法应该显示这样的结果 牵引特性:反应时间:0.6,扭矩:1000,牵引力:0.828607815682475,燃烧时间:3 拖动时间:6.634217763058126秒 我必须创建一个包含5个dragster的数组,然后显示每个dragster的结果 public class Dragster { private int burnTime; priv
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();
}
}
public-class-Dragster{
私人时间;
私人双重反应时间;
私人int扭矩;
私人双牵引;
公共牵引器(双反应时间、int扭矩、双牵引、int燃烧时间){
this.reactionTime=反应时间;
这个。扭矩=扭矩;
牵引力=牵引力;
this.burnTime=燃烧时间;
}
私人双人轮胎(双倍时间){
双重有效性=2.5*(数学经验((时间*时间-(10*时间)+25)/50))/Math.sqrt(2*数学PI);
回报有效性;
}
公共空间倦怠(){
牵引力=牵引力*条件轮胎(燃烧时间);
}
公众双种族(){
双力矩2时间=5000;
双跑道时间=反应时间+(1/(扭矩/扭矩2时间)*条件轮胎(燃烧时间);
返回时间;
}
公共字符串toString(){
返回“反应时间:”+反应时间+”,扭矩:“+扭矩+”牵引力:“+牵引力+
“燃烧时间:”+燃烧时间;
}
}
公共级德拉格雷斯{
私人德拉格雷斯酒店{
}
公共静态void main(字符串[]args){
ArrayList DragsterList=新的ArrayList();
Dragster car1=新的Dragster(0.61000,0.9,3);
Dragster car2=新的Dragster(0.4800,1.0,5);
Dragster car3=新的Dragster(0.51200,0.95,7);
Dragster car4=新的Dragster(0.31200,0.95,1);
Dragster car5=新的Dragster(0.4900,0.9,6);
DragsterList.add(car1);
DragsterList.add(car2);
DragsterList.add(car3);
DragsterList.add(car4);
DragsterList.add(car5);
DragsterList.toString();
}
}
我不知道下一步该怎么做。如果有人能提供一些见解。只需执行以下操作:
public void printResults() {
for (Dragster d: DragsterList) {
System.out.println(d.toString());
System.out.println();
System.out.println("Dragster time:" + d.race());
}
}
基本上,您只需循环浏览列表中的每辆车,打印该车,然后进行一次比赛。程序结束时使用的toString方法是将列表本身转换为字符串,这不是您想要的
您需要对每个dragster单独调用toString()您必须有一个race方法,该方法将迭代所有dragster对象,并计算竞赛时间并显示结果。