Please implement the following. Problem Statement You are given the following UM
ID: 3843159 • Letter: P
Question
Please implement the following.
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 fuel double mpg double currentSpeed:int Process car no baseMpg:double Calculate and scale Factor double update mpg +updateMpg():void Add car to array is a Calculate fuel remaining Car All cars processed? make:string model string Stop +updateFuelRemaining(time Travelledhours):void Write output file Main StopExplanation / Answer
Here is the code with comments for the question. Please do rate if it helped. Thanks
-------------------
Vehicle.java
----------
//The base class vehicle. This class is abstract since we dont want to create the objects of
//this directly rather create inherited classes
public abstract class Vehicle {
//protected data members
protected double fuel;
protected double mpg;
protected int currentSpeed;
protected double baseMpg;
protected double scaleFactor;
// the MPG calculated on current speed and base mpg and scale factor
public void calculateMPG()
{
mpg=baseMpg-(scaleFactor*currentSpeed)+0.01*Math.exp(currentSpeed/20.0);
}
/**
* Updates the fuel based on time travelled.
* @param timeTravelled in hours
*/
public void updateFuelRemaining(double timeTravelled)
{
//calculate distance travelled at current speed
double distance=currentSpeed*timeTravelled;
//calculate fuel consumed based on the total distance travelled and miles per gallon
double usedFuel=distance/mpg;
//updating the fuel
fuel=fuel-usedFuel;
}
//getters and setter for data members
public double getFuel() {
return fuel;
}
public void setFuel(double fuel) {
this.fuel = fuel;
}
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 double getMpg() {
return mpg;
}
}
-------------
Car.java
-----
//A car class inheriting from vehicle class
public class Car extends Vehicle {
//data members
private String model;
private String make;
public Car(String carMake,String carModel)
{
make=carMake;
model=carModel;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
}
------------
CarsTest.java
-------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class CarsTest {
private static void processCar(Car car,double timeTravelled)
{
car.calculateMPG();
car.updateFuelRemaining(timeTravelled);
}
//reads a file named cars.txt and processes the cars and write output in processed_cars.txt
public static void main(String[] args) {
String filename="C:\test\cars.txt"; //input filename containing car data, CHANGE PATH
try {
//create a scanner to read through the file
Scanner fileScanner=new Scanner(new File(filename)); //scanner with File object
ArrayList<Car> list=new ArrayList<Car>(); //a list to store all cars created
Car car;
String line;
while(fileScanner.hasNext()) //while there is more data
{
line=fileScanner.nextLine();
Scanner lineScanner=new Scanner(line); //passing only string to scanner
lineScanner.useDelimiter(","); //set the delimiter to break tokens
//file format - make,model,speed,fuel,basempg,scalefactor,timetravelled
car=new Car(lineScanner.next(),lineScanner.next()); //get make and model from file
car.setCurrentSpeed(lineScanner.nextInt()); //read speed from file and set in car
car.setFuel(lineScanner.nextDouble()); //read fuel from file and set in car
car.setBaseMpg(lineScanner.nextDouble()); //read basempg from file and set in car
car.setScaleFactor(lineScanner.nextDouble()); //read scalefactor from file and set in car
processCar(car,lineScanner.nextDouble()); //read time travelled from file and pass to process
lineScanner.close();
list.add(car); //store it in the list
}
fileScanner.close();
//Now write out the cars from list to output file
PrintWriter writer=new PrintWriter(new File("processed_cars.txt"));
for(int i=0;i<list.size();i++)
{
car=list.get(i); //fetch the car from list
//write out all the details of the car
writer.write("Make: "+car.getMake()+" ");
writer.write("Mode: "+car.getModel()+" ");
writer.write("Current Speed: "+car.getCurrentSpeed()+" ");
writer.write("Mpg: "+String.format("%1$.6f",car.getMpg())+" "); //use string.format() to format to 6 decimal places
writer.write("Fuel: "+String.format("%1$.6f",car.getFuel())+" ");
}
//close the file
writer.close();
System.out.println("Processed "+list.size()+" cars.");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
---------
Sample input cars.txt
-----------
Ford,Mustang,76,20.2,40,0.02,2.3
BMW,Cooper,90,12,40,0.01,0.3
--------
Sample output
------
Make: Ford Mode: Mustang Current Speed: 76 Mpg: 38.927012 Fuel: 15.709545
Make: BMW Mode: Cooper Current Speed: 90 Mpg: 40.000171 Fuel: 11.325003
-------
Please note: In the question the the MPGs calculated in output seem to be incorrect for the input given in question. For the input file, the MPGs are 38.92 and 40.000 as shown in output of this program. I cross checked the program output MPG by manually calculating MPG using the formula in a calculator.