Question

In JAVA In this assignment you will use a class Car to represent a car that travels to various de...

In JAVA

In this assignment you will use a class Car to represent a car that travels to various destinations. Your car has a fuel economy rating of 32.3 miles per gallon. The gas tank holds 19.5 gallons. Your program will need to simulate two trips: 1) BC to Yosemite Valley, and 2) BC to Washington, D.C.. For each trip you will start with a full tank of gas. The output should look as follows.

Trip one: Bakersfield College to Dodger Stadium

Mileage: 204

Trip two: Bakersfield College to Washington, D.C.

Mileage: 2,686

The constructor will set the fuel efficiency rating to 32.3 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. The output must show the mileage at which they would be where they run out of gas. Use int for mileage and discard any fractional values resulting from division.

All values must be hard-coded, do not use the Scanner class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class Trip
{
   String tripId;
   String start_point;
   String end_point;

   int number_of_miles;

   public Trip(String tripId,String start_point, String end_point, int number_of_miles) {
       super();
       this.tripId = tripId;
       this.start_point = start_point;
       this.end_point = end_point;
       this.number_of_miles = number_of_miles;
   }
   public void statTrip()
   {
       System.out.println("Trip" + tripId+":" + start_point+" to "+end_point);
       System.out.println("Mileage:"+number_of_miles);
   }

@Override
   public String toString() {
       return "Trip [tripId=" + tripId + ", start_point=" + start_point + ", end_point=" + end_point
               + ", number_of_miles=" + number_of_miles + "]";
   }
}

class Car {

   private double fuel_economy;
   private double max_tank_capacity;
   private double current_tank_capacity = 0;
   List<Trip> trips = new ArrayList<>();
  
   public Car(double fuel_economy, double max_tank_capacity) {
       this.fuel_economy = fuel_economy;
       this.max_tank_capacity = max_tank_capacity;
   }

   public boolean addGas(double num_of_gallens)
   {
       if (current_tank_capacity + num_of_gallens > max_tank_capacity) {
           return false;
       }

       else {
           current_tank_capacity = current_tank_capacity + num_of_gallens;
       }
       return true;
   }
  
   public double getGasInTank()
   {
       return current_tank_capacity;
   }
   public void addTrips(Trip trip)
   {
           trips.add(trip);
   }
  
   public void roadTrips()
   {
       Iterator tripIterator = trips.iterator();
       while(tripIterator.hasNext())
       {
           Trip trip = (Trip) tripIterator.next();
          
           if(current_tank_capacity*fuel_economy>trip.number_of_miles)
           {
               trip.statTrip();
               current_tank_capacity = current_tank_capacity - (trip.number_of_miles/fuel_economy);
           }
           else {
               System.out.println("Running out of gas,\nFor Trip:"+trip.toString());
           }
          
          
       }
   }
}
public class TestCar
{
   public static void main(String[] args) {

       Trip one = new Trip("One","Bakersfield College", "Dodger Stadium", 204);
       Trip two = new Trip("Two","Bakersfield College", "Washington, D.C", 2686);
       Car car = new Car(32.3, 19.5);
       car.addGas(19.5);
       car.addTrips(one);
       car.addTrips(two);
       car.roadTrips();
   }
}

Add a comment
Know the answer?
Add Answer to:
In JAVA In this assignment you will use a class Car to represent a car that travels to various de...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python code DC Final Project Implement a class Car with the following properties. A car has...

    python code DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. The following two lines are written in a File called FuelEffic.txt (you have to read these from the txt file) Miles per gallon: 20 Tank Size (in gallons): 25 Therefore, based on these...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT