Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program will is in exercise in aggregation (or composition). You will creat

ID: 3551871 • Letter: T

Question

This program will is in exercise in aggregation (or composition). You will create a Car class, where the Car has agas Tank and has an Odometer. As you drive the car, the gas in the gas tank goes down and the miles on the odometer goes up. When you fill up the car with gas, the gas goes into the gas tank. Here is the UML class diagram:

Here is an example of a program that uses the Car class:

When the above program runs, it should generate the following output:

Some important information about the Car class:

It is MUCH easier to solve this program a piece at a time. And you should solve the eaiest parts first. For example, first write and completely test the Tank class by itself. Finish this code, and test with a main method to see if it is working If you use this main method:

then you should get this output:

You should add your own code to the main method (I don't test all of the Tank class' methods here).

Write the odometer class, and test it on its own:

The above main method should generate this output:

Again, add your own testing code to the main method.

Develop your code in three separate files: Car.java, Odometer.java, and Tank.java. Test it out with the supplied TakeATestDrive code above. You should also write some of your own test code (you don't have submit this extra test code).

Use good style, as described in the style handout in the Resources content area of D2L. Use final variables for all constants. Have a javadoc comment for each class, and a javadoc comment for each method (with @param and @return directives).

Explanation / Answer

/* OUTPUT

Toyota
Toyota Camry (30.0 MPG)-- Odometer: 1000.0 miles, Tank: 10.0 gallons
Toyota Camry (30.0 MPG)-- Odometer: 1030.0 miles, Tank: 9.0 gallons
Toyota Camry (30.0 MPG)-- Odometer: 1075.0 miles, Tank: 7.5 gallons
Drive to empty...
Toyota Camry (30.0 MPG)-- Odometer: 1300.0 miles, Tank: 0.0 gallons
Toyota Camry (30.0 MPG)-- Odometer: 1300.0 miles, Tank: 5.0 gallons
40.0 gallons of gas just leaked onto the ground!
Toyota Camry (30.0 MPG)-- Odometer: 1300.0 miles, Tank: 15.0 gallons

Ford
Ford Fusion (40.5 MPG)-- Odometer: 10.0 miles, Tank: 0.0 gallons
Ford Fusion (40.5 MPG)-- Odometer: 110.0 miles, Tank: 12.5 gallons

Chrysler
Gask tank overflowing -- dangerous puddle on the ground!
Odometer rolled over 2 times!
Chrysler Town and Country (34.0 MPG)-- Odometer: 20110.0 miles, Tank: 15.0 gallons

GM
Chevy Cruze (1.0 MPG)-- Odometer: 0.0 miles, Tank: 0.0 gallons

*/


import java.util.*;

// SAVE IT AS Tank.java
public class Tank
{
    // Put the Tank class instance variables here
    private double gallons;
    public Tank(double g)
    {
    if(g>=0 && g<15)
    gallons = g;
    else if(g>15)
    {
    System.out.println("Gask tank overflowing -- dangerous puddle on the ground!");
    gallons = 15.0;
    }
    else
    gallons = 0.0;
    }
    // Put the Tank class instance methods here
    public void addGas(double g)
    {
    gallons = gallons + g;
    if(gallons>15)
    {
    System.out.println((gallons-15)+ " gallons of gas just leaked onto the ground!");   
    gallons = 15;
    }
    }
    public void removeGas(double g)
    {
    gallons = gallons - g;
    if(gallons<0)
    {
    System.out.println("Ran out of gas -- stranded!");
    setToEmpty();
    }
    }
    public double getGallons()
    {
        return gallons;
    }
    public void setToEmpty()
    {
    gallons = 0;
    }
    public String toString()
    {
    return ""+gallons+" gallons";
    }
   // Test the Tank class with its own built-in main method
   /*
   public static void main(String [] args)
   {
      System.out.println("Tank t:");
      Tank t = new Tank(12.5);
      t.removeGas(5.0);
      t.removeGas(1.1);
      System.out.println(t);
      t.removeGas(10.0);
      System.out.println(t);
     
      System.out.println("Tank t2:");
      Tank t2 = new Tank(12.5);
      t2.addGas(2.0);
      System.out.println(t2);
      t2.addGas(5.0);
      System.out.println(t2);
     
      System.out.println("Tank t3:");
      Tank t3 = new Tank(-3.0);
      System.out.println(t3);   
     
      System.out.println("Tank t4:");
      Tank t4 = new Tank(33.0);
      System.out.println(t4);   
   }
   */
}

// SAVE IT AS Odometer.java

public class Odometer
{
    // Put the Odometer class instance variables here
    private double miles;
    // Put the Odometer class instance methods here
    public Odometer(double m)
    {
    if(m<=0)
    miles = 0;
    else if(m>100000)
    {
      miles = m%100000;
      System.out.println("Odometer rolled over "+(int)(m/100000) +" times!");
    }
    else
    miles = m;
    }
    public void addMiles(double m)
    {
    miles = miles + m;
    if(miles>100000)
    {
        System.out.println("The odometer rolled over 100000.0 miles!");
        miles = miles - 100000;
    }
    }
    public double getMiles()
    {
    return miles;
    }
    public String toString()
    {
    return ""+miles+ " miles";
    }
   // Test the Odometer class with its own built-in main method
   /*
   public static void main(String [] args)
   {
      System.out.println("Odometer o1");
      Odometer o1 = new Odometer(300.5);
      o1.addMiles(101.0);
      o1.addMiles(5000.0);
      System.out.println(o1);
     
      System.out.println("Odometer o2");
      Odometer o2 = new Odometer(2000003.0);
      System.out.println(o2);
     
      System.out.println("Odometer o3");
      Odometer o3 = new Odometer(99995.0);
      o3.addMiles(10.0);
      System.out.println(o3);
     
      System.out.println("Odometer o4");
      Odometer o4 = new Odometer(-10.0);
      System.out.println(o4);
   }
   */
}

// SAVE IT AS Car.java

public class Car
{
private String make;
private String model;
private double mpg;
private Tank CarTank;
private Odometer CarOdometer;
public Car(String make,String model,double mpg,double gas,double odo)
{
this.make = make;
this.model = model;
if(mpg<1)
this.mpg = 1;
else
this.mpg = mpg;
CarTank = new Tank(gas);
CarOdometer = new Odometer(odo);
}
public void setMPG(double mpg)
{
    this.mpg = mpg;
}
public void fillUp(double fuel)
{
//TODO
CarTank.addGas(fuel);
}
public void drive(double fuel)
{
//TODO
if(fuel>CarTank.getGallons()*mpg)
{
CarOdometer.addMiles(CarTank.getGallons()*mpg);
CarTank.setToEmpty();
}
else
{
double ran = fuel/mpg;
CarTank.removeGas(ran);
CarOdometer.addMiles(fuel);
}
}

public String toString()
{
    return make + " "+model+" ("+ mpg+" MPG)"
            +"-- Odometer: "+ CarOdometer.getMiles() +" miles, Tank: " +
            String.format("%.1f",CarTank.getGallons())+" gallons";
}
}

// SAVE IT AS TakeATestDrive.java
  
public class TakeATestDrive
{
   public static void main(String [] args)
   {
      System.out.println("Toyota");
      // Create a car that gets 30 mpg, 10 gallons in the tank, and 1000 miles on the odometer.
      Car toyota = new Car("Toyota", "Camry", 30.0, 10.0, 1000.0);
      System.out.println(toyota);
      toyota.drive(30.0);
      System.out.println(toyota);
      toyota.drive(45.0);
      System.out.println(toyota);
     
      System.out.println("Drive to empty...");
      // There isn't enough gas in the tank to go 1000 miles, so this next
      // call to drive will run the car until the tank is empty
      toyota.drive(1000.0);
      System.out.println(toyota);
      toyota.fillUp(2.0);
      toyota.fillUp(3.0);
      System.out.println(toyota);
      toyota.fillUp(50);
      System.out.println(toyota);
     
      System.out.println(" Ford");
      Car ford = new Car("Ford", "Fusion", 40.5, 0.0, 10.0);
      System.out.println(ford);
      ford.fillUp(15.0);
      ford.drive(100.0);
      System.out.println(ford);
     
      System.out.println(" Chrysler");
      Car town = new Car("Chrysler", "Town and Country", 34.0, 30, 220110.0);
      System.out.println(town);

      System.out.println(" GM");
      Car gm = new Car("Chevy", "Cruze", -14.4, -2, -3.0);
      System.out.println(gm);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote