I am getting errors at the class names package sullivan13; import java.text.NumberFormat; public abstract class Automobiles implements Comparable{ //Variables private String model; private String make; private int year; private double price; //Constructor public Automobiles(String make ,String model, int year, double price) { this.make = make; this.model = model; this.year = year; this.price = price; } //Getters public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public double getPrice() { return price; } //Overrider public void display() { System.out.println(this.model+" " +this.year+" "); } //Overrider @Override //CompareTO public String toString() { NumberFormat formatter = NumberFormat.getCurrencyInstance(); return " Make: "+make+ " Model: "+model+ " Year: "+year+ "Price: "+formatter.format(price); } @Override public int compareTo(Automobiles o) { if(this.yearo.year) return 1; else return 0; } } package sullivan13; import java.util.ArrayList; import java.util.Collections; public class Test{ public static void main(String[] args) //Create arraylist of Automobiles { ArrayList automobiles = new ArrayList<>(); automobiles.add(new Automobiles("Ford"," Escape ", 2022,20000)); automobiles.add(new Automobiles("Toyota","Camry", 2023,28000)); automobiles.add(new Automobiles("BMW", "Polo", 2018,113000)); automobiles.add(new Automobiles("Buick","Shar", 2023,26000)); automobiles.add(new Automobiles("Ford","Mustang", 2019,20000)); for (int i =0; i
I am getting errors at the class names
package sullivan13;
import java.text.NumberFormat;
public abstract class Automobiles implements Comparable<Automobiles>{
//Variables
private String model;
private String make;
private int year;
private double price;
//Constructor
public Automobiles(String make ,String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
//Getters
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public int getYear()
{
return year;
}
public double getPrice()
{
return price;
}
//Overrider
public void display() {
System.out.println(this.model+" " +this.year+" ");
}
//Overrider
@Override
//CompareTO
public String toString() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return " Make: "+make+ " Model: "+model+ " Year: "+year+ "Price: "+formatter.format(price);
}
@Override
public int compareTo(Automobiles o) {
if(this.year<o.year)
return-1;
else if(this.year>o.year)
return 1;
else
return 0;
}
}
package sullivan13;
import java.util.ArrayList;
import java.util.Collections;
public class Test{
public static void main(String[] args)
//Create arraylist of Automobiles
{
ArrayList<Automobiles> automobiles = new ArrayList<>();
automobiles.add(new Automobiles("Ford"," Escape ", 2022,20000));
automobiles.add(new Automobiles("Toyota","Camry", 2023,28000));
automobiles.add(new Automobiles("BMW", "Polo", 2018,113000));
automobiles.add(new Automobiles("Buick","Shar", 2023,26000));
automobiles.add(new Automobiles("Ford","Mustang", 2019,20000));
for (int i =0; i<automobiles.size();i++) {
automobiles.get(i).display();
}
//Sort the arrayList
Collections.sort(automobiles);
//Display using the loop
System.out.println("After sort the data...");
for (Automobiles a:automobiles)
{
System.out.println(a);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps