Question
Implement a class Car that will simulate taking a trip. Your car has a fuel economy rating of 52.1 miles per gallon. The gas tank
Implement a class Car that will simulate taking a trip. Your car has a fuel economy rating of 52.1 miles per gallon. The gas tank holds 12 gallons. Your program will need to simulate two trips: 1) BC to Dodger Stadium of 116 miles, and 2) BC to Yellowstone National Park of 1,017 miles. For each trip you will start with a full tank of gas. The output should look as follows. (Replace the ## with the values you calculate.)
Trip one: Bakersfield College to Dodger Stadium
Mileage: 116
Fuel consumption: 52.1 mpg
Fuel consumed: #.# gallons
Fuel remaining: #.# gallons
Trip two: Bakersfield College to Yellowstone National Park
Mileage: 1017
Fuel consumption: 52.1 mpg
Fuel consumed: 12.0 gallons
Fuel remaining: 0.0 gallons
Note: ran out of fuel after #### miles
The constructor will set the fuel efficiency rating to 52.1 mpg and the initial fuel level for each trip to zero. Write a method addGas(), to fill the fuel tank. Write a method getGasInTank(), returning the current amount of gasoline in the fuel tank. Write a method named roadTrip() that simulates driving the car a given distance, reducing the
amount of gasoline in the fuel tank. Let the user know they ran out of gas if they try to drive further than the fuel in the tank and the mileage allows, output the mileage where they would be when the gas ran out. Use int for mileage and discard any fractional values resulting from division. These are several statements you will need to include in your main method:
Car trip1 = new Car(52.1);
trip1.addGas(12);
trip1.roadTrip(116);
double gasLeft = trip1.getGasInTank();
Car trip2 = new Car(52.1);
.
.
.
Be attentive to the instructions here, to the letter. All values must be hard-coded, do not use the Scanner class.
private int tripMileage0; private double fuelConsumptionRate = 0; private double tankCapacity = 12.0; private double fuel!nTank = 0; 8-public CarTrip double mpg) 10 12 public double getGasInTankO I FIXME: complete method statements I FIXME: complete method statements 14 15 16 public double addGas(double fuelIn) [ I FIXME: complete method statements 18 19 20 public double roadTrip(int milesTravelled) 21 I FIXME: complete method statements 24 public static void main(String] args) 25 26 / Trip // FIXME: complete code needed for Trip 1 28 29 Trip 2 CarTrip trip2 = new CarTrip(52.1); trip2.addGas(12); trip2.roadTrip(1017); 32 System.out.println("Trip two: Bakersfield College to Yellowstone National Park"; System.out.printlnC"Mileage: trip2.tripMileage); 36 37 38- 39 if (trip2.getGasInTank() >= 0) { System.out.printlnC"Fuel consumption: trip2.fuelConsumptionRate); System.out.println("Fuel consumed:"trip2.tripMileage / trip2.fuelConsumptionRategallons"; System.out.printlnC"Fuel remaining: "+ trip2.getGasInTankgallons"; 41 43- 45 47 else System.out.printlnC"Ran out of fuel at " trip2.tankCapacity trip2.fuelConsumptionRatemiles; System.out.println; returnStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started