Create two files to submit: Vehicle.java - Class definition, object class VehicleTester.java - Driver program, contains main() method
(1) Create two files to submit:
- Vehicle.java - Class definition, object class
- VehicleTester.java - Driver program, contains main() method
General requirements:
-
A vehicle has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank.
-
We need to simulate driving the vehicle for a given distance while reducing the amount of gasoline in the fuel tank.
-
We also need to be able to check how much gas is in the tank, and we need to be able to add gasoline to the fuel tank. Build the Vehicle class with the following specifications:
-
Instance data:
Variable mpg for fuel efficiency (miles per gallon = mpg)
Variable gas to save how many gallons of gas left in the tank -
Constructors:
Default constructor with no parameter. Use 0 as initial values.
Overloaded constructor with two parameters -
Methods:
getMPG() & setMPG()
getGas() & setGas()
toString() method
drive() to simulate that the car is driven for certain miles. For example, v1.drive(100) means vehicle v1 is driven 100 miles. You need to calculate the gas cost and update the gas tank: gas = gas - miles/mpg. You also need to check if there is enough gas left since gas should not be negative.
You need to figure out the formal parameters for the above methods.
(2) In your testing class, prompt the user for information to create two objects of the Vehicle class. Let each vehicle drive 200 miles. Print out the left gas for each vehicle.
Ex:
Vehicle 1
Enter the mpg:
40
Enter the gas left:
10.5
Vehicle 2
Enter the mpg: 35
Enter the gas left: 2.1
Vehicle 1 has MPG = 40, and 10.5 gallons of gas left.
Vehicle 2 has MPG = 35, and 2.1 gallons of gas left.
Vehicle 1 has 5.5 gallons of gas left after driving 200 miles.
Vehicle 2 does not have enough gas left to drive 200 miles.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images