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

Need Java coding: You are given the following UML diagram of classes and a flow

ID: 3855926 • Letter: N

Question

Need Java coding:

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 fuel double mpg double currentSpeed int Process car no baseMpg:double Calculate and scaleFactor double update mpg +update Mpg0: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 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 "Make: " + make + " Model: " + model + " Current Speed: " + getCurrentSpeed() +
       " MPG: " + getMpg() + " Fuel: " + getFuel();
}
  
}

// CarMain.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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.csv");
input = new Scanner(file);
while (input.hasNextLine()) {
   String line = input.nextLine();
   String[] carDetails = line.split(",");
String make = carDetails[0];

String model = carDetails[1];

int currentSpeed = Integer.parseInt(carDetails[2]);

double fuel = Double.parseDouble(carDetails[3]);

double baseMpg = Double.parseDouble(carDetails[4]);

double scaleFactor = Double.parseDouble(carDetails[5]);

double timeTravelled = Double.parseDouble(carDetails[6]);

Car car = new Car(fuel, currentSpeed, baseMpg, scaleFactor, make, model);
car.updateFuelRemaining(timeTravelled);
carList.add(car);
}
input.close();
  
} catch (Exception ex) {
ex.printStackTrace();
}
  

BufferedWriter bw = null;
       FileWriter fw = null;

       try {
           fw = new FileWriter("processed_cars.txt");
           bw = new BufferedWriter(fw);
          
           for (Car car : carList)
       {
               bw.write(car.toString());
               bw.write(" ");
       }
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (bw != null)
                   bw.close();
               if (fw != null)
                   fw.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
}
}

// input

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

// output

Make: Ford   Model: Mustang   Current Speed: 76   MPG: 38.68085536923187   Fuel: 15.68096859980397
Make: BMW   Model: Cooper   Current Speed: 90   MPG: 39.645981500331445   Fuel: 11.31897259247386

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