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: 3826919 • 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 uel, double. mpg double Process car no base Mpg double Calculate and scale FactoCdouble update mpg +update Mpg0: void Add car to array IS a Calculate fuel remaining Car All cars processed? 00odel string Stop +updateEveRe00aining timeTravelled bours) void file Stop Main

Explanation / Answer


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.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Sam
*/
public class MainClass {
    public static void populate(String fileName) {
        ArrayList<Car> list = new ArrayList<>();
        String line;
        String[] params;
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
            while((line = br.readLine()) != null){
                params = line.split(",");
                Car tempCar = new Car(params[0],params[1], Integer.parseInt(params[2]), Double.parseDouble(params[3]), Double.parseDouble(params[4]), Double.parseDouble(params[5]));
                tempCar.updateMpg();
                tempCar.updateFuelRemaining(Integer.parseInt(params[6]));
                list.add(tempCar);
              
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
        }
      
        try(PrintWriter pw = new PrintWriter(new FileWriter(new File("cars.txt")))){
            list.stream().forEach((c) -> {
                pw.println(c.toString());
            });
        } catch (IOException ex) {
            Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class Vehicle{
    double fuel;
    double mpg;
    int currentSpeed;
    double baseMpg;
    double scaleFactor;
    void updateMpg(){
        mpg = baseMpg - scaleFactor * currentSpeed + 0.1*Math.pow(Math.E,currentSpeed/20.0);
    }

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

class Car extends Vehicle {
    String make;
    String model;

    public Car(String make, String model, int currentSpeed, double fuel, double baseMpg, double scaleFactor) {
        super(fuel, currentSpeed, baseMpg, scaleFactor);
        this.make = make;
        this.model = model;
    }
  
    void updateFuelRemaining(int timeTravelled){
        fuel = fuel - currentSpeed * timeTravelled / mpg;
    }

    @Override
    public String toString() {
        return "Make: " + make+" Model: "+model+" CurrentSpeed: "+currentSpeed+" Mpg: "+mpg+" Fuel: "+fuel;
    }
}

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