Sophie, Sally and Jack are about to open for business. Their CarLot class needs
ID: 3911684 • Letter: S
Question
Sophie, Sally and Jack are about to open for business. Their CarLot class needs some enhancements, however. Add to our previous CarLot the following methods:
Accessors
Car getCarHighestMilage() returns the Car with the highest mileage in the CarLot. If the CarLot is empty, return null.
int getTotalMiles() returns the total mileage of all cars on the CarLot
modify toString to include the above information, but don't include the sorted-by-MPG list.
ArrayList getSortedByMPG() returns a new ArrayList that is ordered by MPG (highest MPG first). The returned ArrayList is empty if the CarLot is empty. Note that this is an accessor; the method must not modify the instance variable in any way.
Test Code
Enhance CarLotMain to add the new information. For example, after all Cars have been entered, print the CarLot sorted by MPG .
Grading Elements
getCarHighestMilage, getTotalMiles are accessors, have correct return types and signatures.
getCarHighestMilage, getTotalMiles return the expected values
getSortedByMPG returns a copy of the CarLot inventory that is sorted appropriately; it does not change the CarLot instance variable.
toString enhanced to include new information
CarLotMain enhanced to report new information and print the sorted-by-MPG list after all cars have been entered.
Here is the code I have that needs to be enhanced:
import java.util.*;
class Car {
private String id;
private double MPG;
public Car(String tid, double tMpg) {
id = tid;
MPG = tMpg;
}
public String getID() {
return id;
}
public double getMPG() {
return MPG;
}
public String toString() {
return "Car ID: " + id + " MPG: " + MPG;
}
}
class CarLot {
private ArrayList<Car> cars=new ArrayList<Car>();
public CarLot() {
}
public Car find(String carId) {
for(int i=0; i<cars.size(); i++) {
if(cars.get(i).getID().equalsIgnoreCase(carId)) {
return cars.get(i);
}
}
return null;
}
public Car get(int position) {
if(position < cars.size()) {
return cars.get(position);
}
else {
return null;
}
}
public double getCarAverageMPG() {
if(cars.size() == 0) {
return -1;
}
else {
double avgMPG, sumMPG=0;
for(int i=0; i<cars.size(); i++) {
sumMPG += cars.get(i).getMPG();
}
avgMPG = sumMPG/(double)cars.size();
return avgMPG;
}
}
public Car getCarBestMPG() {
if(cars.size() == 0) {
return null;
}
else {
int highMPG=0;
for(int i=0; i<cars.size(); i++) {
if((cars.get(i)).getMPG() > (cars.get(highMPG)).getMPG()) {
highMPG = i;
}
}
return cars.get(highMPG);
}
}
public int size() {
return cars.size();
}
public String toString() {
String carsList = "";
for(int i=0; i<cars.size(); i++) {
carsList += cars.get(i) + " ";
}
return carsList;
}
boolean add(Car carToAdd){
try {
cars.add(carToAdd);
return true;
}
catch(Exception ex) {
return false;
}
}
}
class CarLotMain {
public static void main(String args[]) {
int carsNum;
Scanner kybd = new Scanner(System.in);
System.out.print(" Enter number of cars to be added to a CarLot: ");
carsNum = kybd.nextInt();
String cId;
double mpg;
CarLot carsLot = new CarLot();
for(int i=0; i<carsNum; i++) {
kybd.nextLine();
System.out.print(" Enter Car Id: ");
cId = kybd.nextLine();
System.out.print(" Enter Car MPG: ");
mpg = kybd.nextDouble();
Car temp = new Car(cId, mpg);
carsLot.add(temp);
}
System.out.println(" CarLot Size: " + carsLot.size() + " ");
System.out.println(carsLot);
String id;
kybd.nextLine();
System.out.print(" Enter one Car identifier: ");
id = kybd.nextLine();
System.out.println(carsLot.find(id));
}
}
Explanation / Answer
/* getCarHighestMileage() is already implemented in the code as
getCarBestMileage()
*/
import java.util.*;
class Car {
private String id;
private double MPG;
public Car(String tid, double tMpg) {
id = tid;
MPG = tMpg;
}
public String getID() {
return id;
}
public void setID(String a) {
id = a;
}
public double getMPG() {
return MPG;
}
public void setMPG(double a) {
MPG = a;
}
public String toString() {
return "Car ID: " + id + " MPG: " + MPG;
}
}
class CarLot {
private ArrayList<Car> cars=new ArrayList<Car>();
public CarLot() {
}
public Car find(String carId) {
for(int i=0; i<cars.size(); i++) {
if(cars.get(i).getID().equalsIgnoreCase(carId)) {
return cars.get(i);
}
}
return null;
}
public Car get(int position) {
if(position < cars.size()) {
return cars.get(position);
}
else {
return null;
}
}
public double getTotalMiles(){
double sum = 0;
for (int i = 0; i<cars.size(); i++){
sum = sum + cars.get(i).getMPG();
}
return sum;
}
public double getCarAverageMPG() {
if(cars.size() == 0) {
return -1;
}
else {
double avgMPG, sumMPG=0;
for(int i=0; i<cars.size(); i++) {
sumMPG += cars.get(i).getMPG();
}
avgMPG = sumMPG/(double)cars.size();
return avgMPG;
}
}
public ArrayList<Car> getSortedByMPG(){
if (cars.size() == 0)
return null;
ArrayList<Car> list = new ArrayList<Car>();
for (int i = 0; i<cars.size(); i++)
list.add(cars.get(i));
for (int i = 0; i<list.size(); i++){
for (int j = i+1; j<list.size(); j++){
if (list.get(i).getMPG() < list.get(j).getMPG()){
String tempid = list.get(i).getID();
double tempmpg = list.get(i).getMPG();
list.get(i).setID(list.get(j).getID());
list.get(i).setMPG(list.get(j).getMPG());
list.get(j).setID(tempid);
list.get(i).setMPG(tempmpg);
}
}
}
return list;
}
public Car getCarBestMPG() {
if(cars.size() == 0) {
return null;
}
else {
int highMPG=0;
for(int i=0; i<cars.size(); i++) {
if((cars.get(i)).getMPG() > (cars.get(highMPG)).getMPG()) {
highMPG = i;
}
}
return cars.get(highMPG);
}
}
public int size() {
return cars.size();
}
public String toString() {
String carsList = "";
for(int i=0; i<cars.size(); i++) {
carsList += cars.get(i) + " ";
}
String str = carsList + "/n" + "Total Mileage:" + Double.toString(getTotalMiles());
return carsList;
}
boolean add(Car carToAdd){
try {
cars.add(carToAdd);
return true;
}
catch(Exception ex) {
return false;
}
}
}
public class CarLotMain {
public static void main(String args[]) {
int carsNum;
Scanner kybd = new Scanner(System.in);
System.out.print(" Enter number of cars to be added to a CarLot: ");
carsNum = kybd.nextInt();
String cId;
double mpg;
CarLot carsLot = new CarLot();
for(int i=0; i<carsNum; i++) {
kybd.nextLine();
System.out.print(" Enter Car Id: ");
cId = kybd.nextLine();
System.out.print(" Enter Car MPG: ");
mpg = kybd.nextDouble();
Car temp = new Car(cId, mpg);
carsLot.add(temp);
}
System.out.println(" CarLot Size: " + carsLot.size() + " ");
System.out.println(carsLot);
String id;
kybd.nextLine();
System.out.print(" Enter one Car identifier: ");
id = kybd.nextLine();
System.out.println(carsLot.find(id));
ArrayList<Car> list = carsLot.getSortedByMPG();
System.out.println("Cars Sorted by MPG");
for (int i = 0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.