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

Problem Statement You are given the following UML diagram of classes and a flow

ID: 3799992 • Letter: P

Question

Problem Statement 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 Start Process Car UML Read car data from Vehicle Start datafile fuel: double mpg:double currentSpeed:int Process car no baseMpg:double Calculate and scaleFactor:double update mpg +update Mpg void Add car to array IS a Calculate fuel remaining Car All cars processed? make:string model string Stop +updateFuelRemaining (time Travelled: hours): void Write output Main Stop

Explanation / Answer

public class Vehicle {

   protected double fuel;
   protected double mpg;
   protected int currentSpeed;
   protected double baseMpg;
   protected double scaleFactor;

   public Vehicle(double fuel, int currentSpeed, double baseMpg,
           double scaleFactor) {
       this.fuel = fuel;
       this.currentSpeed = currentSpeed;
       this.baseMpg = baseMpg;
       this.scaleFactor = scaleFactor;
   }

   public Vehicle(double fuel, double mpg, int currentSpeed, double baseMpg,
           double scaleFactor) {
       this.fuel = fuel;
       this.mpg = mpg;
       this.currentSpeed = currentSpeed;
       this.baseMpg = baseMpg;
       this.scaleFactor = 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;
   }

   public void updateMpg() {
       setMpg(mpg);
   }

   public String toString() {
       return "Vehicle [fuel=" + fuel + ", mpg=" + mpg + ", currentSpeed="
               + currentSpeed + ", baseMpg=" + baseMpg + ", scaleFactor="
               + scaleFactor + "]";
   }

}

**************************************************************************************************************

public class Car extends Vehicle {

   public String make;
   public String model;
   public double timeTravelled;

   public Car(double fuel, double mpg, int currentSpeed, double baseMpg,
           double scaleFactor, String make, String model) {
       super(fuel, mpg, currentSpeed, baseMpg, scaleFactor);
       this.make = make;
       this.model = model;
   }

   public Car(double fuel, int currentSpeed, double baseMpg,
           double scaleFactor, String make, String model, double timeTravelled) {
       super(fuel, currentSpeed, baseMpg, scaleFactor);
       this.make = make;
       this.model = model;
       this.timeTravelled = timeTravelled;
   }

   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;
   }

   public int hashCode() {
       final int prime = 31;
       int result = super.hashCode();
       result = prime * result + ((make == null) ? 0 : make.hashCode());
       result = prime * result + ((model == null) ? 0 : model.hashCode());
       return result;
   }

   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (!super.equals(obj))
           return false;
       if (getClass() != obj.getClass())
           return false;
       Car other = (Car) obj;
       if (make == null) {
           if (other.make != null)
               return false;
       } else if (!make.equals(other.make))
           return false;
       if (model == null) {
           if (other.model != null)
               return false;
       } else if (!model.equals(other.model))
           return false;
       return true;
   }

   public String toString() {
       return "Car [make=" + make + ", model=" + model + ", fuel=" + fuel
               + ", mpg=" + mpg + ", currentSpeed=" + currentSpeed
               + ", baseMpg=" + baseMpg + ", scaleFactor=" + scaleFactor + "]";
   }

}

************************************************************************************************************************

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) throws IOException {
       List<Vehicle> cars = new ArrayList<Vehicle>();
       Scanner scanner = new Scanner(System.in);
       System.out.println("enter the file name");
       String filename = scanner.nextLine();
       BufferedReader reader = new BufferedReader(new FileReader(new File(
               filename.trim())));
       String line = "";

       while ((line = reader.readLine()) != null) {
           String[] words = line.split(",");
          

               String make = words[0];
               String model = words[1];
               int currentSpeed = Integer.parseInt(words[2]);
               double fuel = Double.parseDouble(words[3]);
               double baseMpg = Double.parseDouble(words[4]);
               double scaleFactor = Double.parseDouble(words[5]);
               double timeTravelled = Double.parseDouble(words[6]);

               Vehicle car = new Car(fuel, currentSpeed, baseMpg, scaleFactor,
                       make, model, timeTravelled);
               System.out.println(car);
               cars.add(car);

          
       }
      
       FileWriter writer=new FileWriter(new File("D:\processedCars.txt"));
      
       for(Vehicle car:cars)
       {
           writer.write(car.toString());
           writer.flush();
           writer.write(" ");
          
       }

   }

}

output

enter the file name
D:\cars.txt
Car [make=Ford, model=Mustang, fuel=20.2, mpg=0.0, currentSpeed=76, baseMpg=40.0, scaleFactor=0.02]
Car [make=BMW, model=Cooper, fuel=12.0, mpg=0.0, currentSpeed=90, baseMpg=40.0, scaleFactor=0.01]

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