question archive Consider a class motorboat that represents motorboats
Subject:MathPrice:4.89 Bought3
Consider a class motorboat that represents motorboats. A motorboat has attributes for
• The capacity of the fuel tank
• The amount of fuel in the tank
• The maximum speed of the boat
• The current speed of the boat
• The efficiency of the boat's motor
• The distance traveled
The class has methods to
• Change the speed of the boat
• Operate the boat for an amount of time at the current speed
•Refuel the boat with some amount of fuel
• Return the amount of fuel in the tank
• Return the distance traveled so for
If the boat has efficiency e, the amount of fuel used when traveling at a speed s for time t is e x s2xt. The distance traveled in that time is sxt.
Write a method heading for each method.
Write preconditions and postconditions for each method.
Write some Java statements that test the class.
Implement the class
public class MotorBoat {
// The capacity of the fuel tank
private double capacity;
// 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 currSpeed;
// The efficiency of the boat's motor
private double efficiency;
// The distance traveled
private double distanceTraveled;
// Change the speed of the boat
private void changeSpeed(double newSpeed) {
this.currSpeed = newSpeed;
}
// Operate the boat for an
// amount of time at the current speed
// I can't get it
// Refuel the boat with some amount of fuel
private void refuel(double newAmount) {
this.amount += newAmount;
}
// Return the amount of fuel in the tank
private double getFuelAmount() {
return this.amount;
}
// Return the distance traveled so far
private double getDistanceTraveled() {
return this.distanceTraveled;
}
//
// If the boat has efficiency e, the amount of fuel used when traveling at a
// speed s for time t is e x s^2(s squared) x t. The distance traveled in
// that time is s x t.
private void computeAmount() {
this.amount = this.efficiency * (this.currSpeed * this.currSpeed)
* (this.distanceTraveled / this.currSpeed);
}
// a. Write a method heading for each method.
// b. Write preconditions and postconditions for each method.
// c. Write some Java statements that test the class.
// d. Implement the class.
public static void main(String[] args) {
MotorBoat boat = new MotorBoat();
System.out.println("Current Speed: " + boat.currSpeed);
boat.changeSpeed(200);
System.out.println("Current Speed: " + boat.currSpeed);
System.out.println("Fuel Amount: " + boat.getFuelAmount());
boat.refuel(2000);
System.out.println("Fuel Amount: " + boat.getFuelAmount());
System.out.println("Distance Traveled: " + boat.getDistanceTraveled());
boat.computeAmount();
System.out.println("Fuel Amount: " + boat.getFuelAmount());
}
}