Question
Goal: You are to write a GUI program in Java that will allow a user to enter the cars for an automobile fleet and display
Goal: You are to write a GUI program in Java that will allow a user to enter the cars for an automobile fleet and display them. You might add new fields to the Car objectsmake, model, year. Or you might add more sort capabilities to Fleet, allowing the GUI to display cars sorted by mileage. Be creative; find something that stinks about the minimum program, and improve it.
You may, under no circumstances, use a code generator.
Like all WidgetView programs, we have no control over the layout of our widgets. The grading will notpenalize a submission for an awkward layout. Objectives By the end of this project will student will be able to
Write a GUI program that maintains a set of Cars in an automobile fleet, identify various specific carsthose with the highest mileage or best miles per gallon,
Demonstrate the ability to assemble already-written classes into a larger, more complicated program.
Demonstrate the ability to segregate business logic from user interface code.
Sort collections of objects Capabilities
At a minimum, the program should Allow a user to add a car with a given identifier, initial mileage and fuel consumption to the fleet. Display the fleet both in the order that the cars were entered, and in the order of worst-MPG to best-MPG.
Display the cars with the best MPG and the highest mileage. Display the miles per gallon for the entire fleet. Sample Execution Then initial panel has fields for adding a new car to the fleetan identification field, current mileage and fuel consumed. It also has (empty) best MPG, highest mileage, and fleet MPG fields. There are also two text areas, one for the fleet with the cars in the order they were entered, the other with cars listed from worst to best MPG.
CODE:
import java.util.ArrayList; import java.util.Arrays;
public class Fleet { private ArrayList myCars; public Fleet() { myCars = new ArrayList<>(); } public Car find(String identifier) { for (Car c: myCars) { if (c.getIdentifier().equals(identifier)) { return c; } } return null; } public boolean add(Car carToAdd) { return myCars.add(carToAdd); } public int size() { return myCars.size(); } public Car get(int position) { return myCars.get(position); } public int getTotalMiles() { int totalMiles = 0; for (Car c: myCars) { totalMiles += c.getMiles(); } return totalMiles; } public int getTotalFuel() { int totalFuel = 0; for (Car c: myCars) { totalFuel += c.getFuelUsed(); } return totalFuel; } public double getCarAverageMPG() { if (size() == 0) return -1; double totMPG = 0; for (Car c: myCars) { totMPG += c.getMPG(); } return totMPG / myCars.size(); } public double getFleetAverageMPG() { if (getTotalFuel() == 0) return -1; return (double)getTotalMiles() / getTotalFuel(); } @Override public String toString() { String s = "Fleet [ "; for (Car c: myCars) { s += c.toString() + " "; } s += "]"; return s; } }
public class Car { private String identifier; private int milage; private int fuelUsed; public Car() { identifier = ""; milage = 0; fuelUsed = 0; }
public Car(String identifier) { this.identifier = identifier; milage = 0; fuelUsed = 0; } public Car(String identifier, int milage, int fuelUsed) { this.identifier = identifier; this.milage = milage; this.fuelUsed = fuelUsed; } public void addFuel(int fuel) { fuelUsed += fuel; } public void addMiles(int miles) { milage += miles; } public double getMPG() { return (double)milage / fuelUsed; }
public String getIdentifier() { return identifier; }
public void setIdentifier(String identifier) { this.identifier = identifier; }
public int getMiles() { return milage; }
public void setMiles(int milage) { this.milage = milage; }
public int getFuelUsed() { return fuelUsed; }
public void setFuelUsed(int fuelUsed) { this.fuelUsed = fuelUsed; } public int compareMPG(Car otherCar) { double myMPG = getMPG(); double otherMPG = otherCar.getMPG(); if (myMPG == otherMPG) return 0; if (myMPG - otherMPG > 0) return 1; return -1; }
public boolean equals(Object obj){ if (this==obj) return true; if (obj==null) return false; if (this.getClass()!=obj.getClass()) return false; final Car other = (Car)obj; if ((this.getIdentifier().equals(other.getIdentifier()) && this.getMPG()==other.getMPG())) return true; return false; } public String toString() { return "Car [identifier=" + identifier + ", milage=" + milage + ", fuelUsed=" + fuelUsed + "]"; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started