Need java coding for this: You are given the following UML diagram of classes an
ID: 3860959 • Letter: N
Question
Need java coding for this:
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 StopExplanation / Answer
package Vehicle;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static ArrayList<Car> cars = new ArrayList<>();
public static void main(String[] args) {
readCarFromFile("car.csv");
writeToFile("processed_car.txt");
}
public static void writeToFile(String fileName) {
try (PrintWriter pr = new PrintWriter(fileName)) {
for (Car car : cars) {
pr.append(car.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void readCarFromFile(String file) {
try (Scanner sc = new Scanner(new FileReader(file))) {
String[] line;
String make;
String model;
int speed;
double fuel;
double baseMpg;
double scaleFactor;
double timeTraveled;
while (sc.hasNext()) {
line = sc.nextLine().split(",");
make = line[0];
model = line[1];
speed = Integer.parseInt(line[2]);
fuel = Double.parseDouble(line[3]);
baseMpg = Double.parseDouble(line[4]);
scaleFactor = Double.parseDouble(line[5]);
timeTraveled = Double.parseDouble(line[6]);
Car car = new Car(fuel, speed, baseMpg, scaleFactor, make, model);
car.updateMpg();
car.updateFuelRemaining(timeTraveled);
System.out.println(car);
cars.add(car);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package driver;
/**
* FileName:DrivingData.java
*/
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
public class DrivingData {
public static void main(String[] args) throws IOException {
// Open diving_data.txt
File file = new File("diving_data.txt");
Scanner inputFile = new Scanner(file);
List<Driver> drivers = new ArrayList<Driver>();
//Declare Variables
String firstName = null;
String lastName = null;
double[] scores = new double[8];
int count = 0;
// Read lines from the file until no more are left and store names and scores in arrays.
while (inputFile.hasNext() && count < scores.length) {
firstName = inputFile.next();
lastName = inputFile.next();
ArrayList<Double> score = new ArrayList<Double>();
for (int i = 0; i < 8; i++) {
score.add(inputFile.nextDouble());
}
drivers.add(new Driver(firstName, lastName, score));
}
inputFile.close();
for (Driver driver : drivers) {
System.out.println(driver);
}
}
}
class Driver {
private String firstName;
private String lastName;
private List<Double> scores;
public static DecimalFormat twoDecPl = new DecimalFormat("0.00");
/**
*this parametrized constructor which will intialize the driver object
* @param firstName
* @param lastName
* @param scores
*/
public Driver(String firstName, String lastName, ArrayList<Double> scores) {
this.firstName = firstName;
this.lastName = lastName;
this.scores = scores;
}
/**
* This is default constructor
*/
public Driver() {
this.scores = new ArrayList<Double>();
}
/**
* This function calcuate total score. it will drop max and min then add other value to give result
* @return
*/
public Double getTotalScore() {
Collections.sort(this.scores);
double sum = 0;
for (int i = 1; i < this.scores.size() - 1; i++) {
sum += this.scores.get(i);
}
return sum;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setScores(double score) {
this.scores.add(score);
}
public List<Double> getScores() {
return this.scores;
}
/**
* overriding the to string method because we want to print object value not refrence
* @return
*/
@Override
public String toString() {
return "" + this.firstName + " " + this.lastName + "-" + twoDecPl.format(getTotalScore());
}
}
package Vehicle;
public class Vehicle {
private double fuel;
private double mpg;
private int currentSpeed;
private double baseMpg;
private 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 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() {
mpg = baseMpg - scaleFactor * currentSpeed + 0.01 * Math.exp((double) currentSpeed / 20);
}
@Override
public String toString() {
return " currentSpeed: " + currentSpeed + " mpg: " + String.format("%.2f", mpg) + " fuel :" + String.format("%.2f", fuel) + " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.