In: Computer Science
Java language
This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object.
a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before you make the purchase. Write a class MotorBoat that represents motorboats. A motorboat has attributes for
and the following methods:
Note: If the rate of fuel consumption is r and the distance traveled is d, then the amount of fuel remaining in the tank = fuel in tank – r*d
b) Write a tester program that creates a motorboat with capacity 100 liters, fuel in tank 50 liters, max. speed 100 kmph, rate of fuel consumption 1. Set its speed to 25 kmph and operate it for 30 minutes. Print the current speed, distance driven and the fuel remaining in the tank. Then increase the speed by 25, and operate it for 30 minutes. Print the current speed, distance driven and fuel in tank. Your output should be:
Current speed: 25.0 kmph
Distance driven: 12.5 km
Fuel in tank: 37.5 liters
Current speed: 50.0 kmph
Distance driven: 25.0 km
Fuel in tank: 12.5 liters
Test your program to check for other cases as well.
JAVA CODE(MotorBoat.java)
class MotorBoat
{
double fueltank_capacity;
double fuel_in_tank;
double max_speed;
double current_speed;
double fuel_consumption;
double distance_travelled;
//constructor to initialize the object
public MotorBoat(double capacity,double fuel,double
maxi_speed,double fuel_consumed)
{
fueltank_capacity=capacity;
fuel_in_tank=fuel;
max_speed=maxi_speed;
current_speed=0;
fuel_consumption=fuel_consumed;
}
// method to get current speed of boat
public double getCurrent_speed()
{
return current_speed;
}
// method to set current speed of boat
public void setCurrent_speed(double speed)
{
current_speed=speed;
}
// method to change speed of boat
public void changeSpeed(double speed)
{
if(speed > max_speed)
{
current_speed=max_speed;
}
else
{
current_speed+=speed;
}
}
// method to run boat for given minutes
public void boatRunning(int minutes)
{
distance_travelled= current_speed *
((double)minutes/60);
fuel_in_tank = fuel_in_tank -
fuel_consumption * distance_travelled;
}
// method to refuel the tank
public void reFuel(double fuel)
{
if(fuel >
fueltank_capacity)
{
fuel_in_tank=fueltank_capacity;
}
}
// method to get fuel in tank
public double fuelInTank()
{
return fuel_in_tank;
}
// method to find distance travelled
public double distanceTravelled()
{
return distance_travelled;
}
}
JAVA CODE (Demo.java)
public class Demo
{
public static void main(String args[])
{
// creating MotorBoat object
MotorBoat ob=new
MotorBoat(100,50,100,1);
// change the boat speed
ob.changeSpeed(25);
// run the boat for 30
minutes
ob.boatRunning(30);
//displaying speed, distance
travelled, fuel in tank
System.out.printf("Current Speed:
%.1f kmph",ob.getCurrent_speed());
System.out.printf("\nDistance
driven: %.1f km",ob.distanceTravelled());
System.out.printf("\nFuel in tank:
%.1f liters",ob.fuelInTank());
// change the boat speed
ob.changeSpeed(25);
// run the boat for 30
minutes
ob.boatRunning(30);
//displaying speed, distance
travelled, fuel in tank
System.out.printf("\nCurrent Speed:
%.1f kmph",ob.getCurrent_speed());
System.out.printf("\nDistance
driven: %.1f km",ob.distanceTravelled());
System.out.printf("\nFuel in tank:
%.1f liters\n",ob.fuelInTank());
}
}
SCREENSHOT FOR OUTPUT: