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

*Note: Car will still burn fuel even if it is not being driven. You are given th

ID: 3859759 • Letter: #

Question

*Note: Car will still burn fuel even if it is not being driven.

You are given the following UML diagram of classes and a flow chart. Implement the classes as they are shown in the UML diagram, and then implement the operations shown in the flow chart as described in section Problem Details below. Process Car UML. data from Vehicle datafile fuel double mpg double currentSpeed int Process car no baseMpg double Calculate and scaleFactor double update mpg tupdateMpg0:void Add car to array is a Calculate fuel remaining Car All cars processed? make string model string Stop tupdateFuelRemaining (time Travelled:hours) void Write output file Main Stop

Explanation / Answer

// Vehicle.java


public class Vehicle {
   public Vehicle(double fuel, int currentSpeed, double baseMpg,
           double scaleFactor) {
       this.fuel = fuel;
       this.currentSpeed = currentSpeed;
       this.baseMpg = baseMpg;
       this.scaleFactor = scaleFactor;
      
       this.mpg = baseMpg - (scaleFactor*currentSpeed) + 0.01*(Math.exp(currentSpeed/20));
      
   }
   private double fuel;
   private double mpg;
   private int currentSpeed;
   private double baseMpg;
   private double scaleFactor;
   public double getFuel() {
       return fuel;
   }
   public void setFuel(double fuel) {
       this.fuel = fuel;
   }
   public double getMpg() {
       return mpg;
   }
   public void setMpg(double mpg) {
       this.mpg = mpg;
   }
   public int getCurrentSpeed() {
       return currentSpeed;
   }
   public void setCurrentSpeed(int currentSpeed) {
       this.currentSpeed = currentSpeed;
   }
   public double getBaseMpg() {
       return baseMpg;
   }
   public void setBaseMpg(double baseMpg) {
       this.baseMpg = baseMpg;
   }
   public double getScaleFactor() {
       return scaleFactor;
   }
   public void setScaleFactor(double scaleFactor) {
       this.scaleFactor = scaleFactor;
   }
}

// Car.java


public class Car extends Vehicle{
   public Car(double fuel, int currentSpeed, double baseMpg,
           double scaleFactor, String make, String model) {
       super(fuel, currentSpeed, baseMpg, scaleFactor);
       this.make = make;
       this.model = model;
   }
   private String make;
   private String model;
   public String getMake() {
       return make;
   }
   public void setMake(String make) {
       this.make = make;
   }
   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model = model;
   }
  
   void updateMpg(int speed)
   {
       double mpg = getBaseMpg() - (getScaleFactor()*speed) + 0.01*(Math.exp(speed/20));
       setMpg(mpg);
   }
  
   void updateFuelRemaining(double timeTravelled)
   {
       double milesTRavelled = getCurrentSpeed()*timeTravelled;
       double fuelUsed = milesTRavelled/ getMpg();
       double fuelRemaining = getFuel() - fuelUsed;
       setFuel(fuelRemaining);
   }
   @Override
   public String toString() {
       return "Car [make=" + make + ", model=" + model + ", fuel=" + getFuel() +
           ", mpg=" + getMpg() + ", currentSpeed="
               + getCurrentSpeed() + ", baseMpg=" + getBaseMpg() + ", scaleFactor="
               + getScaleFactor() + "]";
   }
  
}

// CarMain.java

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class CarMain {
   public static void main(String[] args)
   {
       List<Car> carList = new ArrayList<>();
      
       try {
   Scanner input;  
   File file = new File("cardetails.txt");
   input = new Scanner(file);
   while (input.hasNextLine()) {
   String make = input.next();
   String model = input.next();
   int currentSpeed = input.nextInt();
   double fuel = input.nextDouble();
   double baseMpg = input.nextDouble();
   double scaleFactor = input.nextDouble();
   double timeTravelled = input.nextDouble();
   Car car = new Car(fuel, currentSpeed, baseMpg, scaleFactor, make, model);
   car.updateFuelRemaining(timeTravelled);
   carList.add(car);
   }
   input.close();
  
   } catch (Exception ex) {
   ex.printStackTrace();
   }
      
       for (Car car : carList)
       {
           System.out.println(car);
       }
   }
}

// cardetails.txt

Ford Mustang 76 20.2 40 0.02 2.3
BMW Cooper 90 12 40 0.01 0.3

// sample run

Car [make=Ford, model=Mustang, fuel=15.68096859980397, mpg=38.68085536923187, currentSpeed=76, baseMpg=40.0, scaleFactor=0.02]
Car [make=BMW, model=Cooper, fuel=11.31897259247386, mpg=39.645981500331445, currentSpeed=90, baseMpg=40.0, scaleFactor=0.01]