对于那些有帮助的人我真的很感激!!
以下是问题: 考虑一个代表摩托艇的MotorBoat类。摩托艇具有以下私人属性:
船的最高速度
目前船的速度
船用电机的效率
行进的距离
该课程有以下方法:
改变船的速度
以当前速度操作船只一段时间
用一定量的燃料给船加油
返回油箱中的燃油量
返回到目前为止行进的距离如果船有效率e,那么 在时间t以速度s行驶时使用的燃料量是e x s2 x t。在那段时间内行进的距离是s x t。
这是我到目前为止所得到的:
public class MotorBoat {
//the capacity of the fuel tank
private double capaity;
//the amount of fuel in the tank
private double amount;
//the maximum speed of the boat
private double maxSpeed;
//the current speed of the boat
private double curSpeed;
//the efficiency of the boat's motor
private double efficiency;
//the distance traveled
private double distance;
//Change the speed of the boat
public void changeSpeed(double newSpeed)
{
this.curSpeed = newSpeed;
}
//Operate the boat for an amount of time at the current speed
public void operateAmount(double newAmount)
{
this.amount = newAmount;
}
//Refuel the boat with some amount of fuel
//I don't get this one
//Return the amount of fuel in the tank
public double getAmount()
{
return amount;
}
//Return the distance traveled so far
public double getDistance()
{
return distance;
}
//If the boat has efficiency e, the amount of fuel used when traveling at a speed s for time t is e x s2 x t. The distance traveled in that time is s x t.
public double amountUsed()
{
double amountUsed = this.efficiency*(this.curSpeed*this.curSpeed)*(this.getDistance()/this.curSpeed);
return amountUsed();
}
public static void main(String[] args) {
MotorBoat boat = new MotorBoat();
System.out.println("Current Speed: " + boat.curSpeed);
boat.changeSpeed(200);
System.out.println("Current Speed: " + boat.curSpeed);
System.out.println("Fuel Amount: " + boat.getAmount());
boat.operateAmount(2000);
System.out.println("Fuel Amount: " + boat.getAmount());
System.out.println("Distance Traveled: " + boat.getDistance());
boat.amountUsed();
System.out.println("Fuel Amount: " + boat.amountUsed());
}
}
答案 0 :(得分:0)
你需要一个构造函数,或者在尝试获取它们之前设置所有值,否则会出现空指针
public MotorBoat(double a, double b, double c /* etc*/){
this.a = a;
this.b = b;
this.c = c;
}
还返回amunt使用需要在()
中移除return
否则您正在引用该函数,而不是您在函数中创建的临时变量
最后,你需要为你想要从类外部访问的所有私有值获取getter和setter,比如在类的实例上,然后在非类范围内的类上操作时使用这些函数,在这种情况下说你的静态manin
下次你问一个问题时,请指出错误,以及你正在寻求帮助的地方,对于每个人的家庭作业寻求帮助没有任何问题,但除非你告诉你试过并提供一个描述
你需要什么帮助