1. We enhanced the “Vehicle” class from homework page 108.
1. We added “fuelLeft” variable inside the “Vehicle” class.
2. We added “drive” method with “distance” parameter.
2. Homework:
1. Enhance the “Vehicle” class with the following
1. Add “addMoreFuel” method with one parameter “fuel” whose data type is “int”:
1. Inside the method, it will add parameter “fuel” into “fuelLeft” variable.
2. If “fuelLeft” is more than “fuelCap” the program will do the following
1. print “Fuel overflow. Capping fuel tank to “+fuelCap to the shell.
2. Set “fuelLeft” to “fuelCap”.
3. At the end of the method, print the following 2 pieces of information:
1. “Your gas tank is now “+fuelLeft+” gallons”
2. “You can drive for “+(fuelLeft*mpg)+” miles.”
2. Inside the existing “drive” method, change the code to do the following
1. Before reducing “fuelLeft”, calculate the distance the vehicle can cover with the “fuelLeft”.
`int distanceItCanGo = fuelLeft * mpg.`
2. if the “fuelLeft” variable is less than or equal to 0, do the following:
1. print to the shell: “Out of gas at “+distanceItCanGo+” miles, add more fuel please.”
2. set the “fuelLeft” variable to 0.
3. Get input from shell.
1. “How many gallons of fuel you want to add? “
2. After the user enters some number, call the addMoreFuel method and pass whatever the user enters as the parameter.
2. Create a new class April4_HomeworkVehicleAddMoreFuel
1. Inside the class create the usual “public static void main(String[] args)” method
2. Get input from the shell the following 4 information
1. “Enter a new vehicle passenger capacity? “
2. “Enter a new vehicle fuelCap? “
3. “Enter a new vehicle mpg? “
4. “How far you want to drive? “
3. After getting these 4 pieces of information, create a new Vehicle, “april4Vehicle”. Use the first 3 input from the shells as parameters of the Vehicle constructor.
4. Call april4Vehicle.drive method and pass 4th input as the method parameter.
3. Run April4_HomeworkVehicleClass
1. For vehicle passenger capacity, enter 4.
2. For vehicle fuelCap, enter 10.
3. For vehicle mpg, enter 20.
4. For how far you want to drive, enter 1000.
5. The program now should print to the shell: “Out of gas, fill up fuel, please.”
6. The program will get input from the user “How many gallons of fuel you want to add?”. Enter 20.
7. The program will now print “Fuel overflow. Capping fuel tank to 10.”
8. If the program runs properly, it should do the following to the shell:
9. ```Enter a new vehicle passenger capacity? 4
Enter a new vehicle fuelCap? 10
Enter a new vehicle mpg? 20
How far you want to drive? 1000