Chapter 20 Assignment Requirements Create a new Eclipse project named so as to i
ID: 3698336 • Letter: C
Question
Chapter 20 Assignment Requirements Create a new Eclipse project named so as to include your name (eg smith20 or jones20). In this project, create a new package with the same name as the project. In this package, write solutions to the exercises noted below. The assignment requires three classes. Put all classes in the same package. When done, locate your project in your project workspace and zip it. Upload the zip to this drop box. Add your collaboration statement. Submit. This assigment needs to be done in Java Class Car Properties: make:String, model:String, price:double Methods: Parameterized constructor for initializing a new Car instance’s properties Getters for each property toString method to display a Car’s properties Class CarComparator Code it to compare Car objects by price from lowest to highest Class CarLists (main class) code it in this order Create an ArrayList of at least six cars, not ordered in any way by price. Create an Iterator and use it to display the entire list of cars. Display the most expensive car in the list. Use the Collections class. Create a linked list from the arraylist. Insert another Car at index 4 in the linked list. This car should now be the cheapest in the list. Sort the linked list in order of increasing price. Create and use a ListIterator to display the linked list in order of increasing price. Remove the first and last Cars from the linked list, displaying them as you do so. **JAVA ASSIGNMENT** Use the ListIterator again to iterate back through the list so cars are now shown in order of decreasing price.
Explanation / Answer
I have created eclipse project, package and created three classes under that package as mentioned above. Here package name: smith20 (used this name from question. you can change it as you wish)
We are unable to upload the zip file, so pasting my code classes here...
Car.java:
package smith20;
public class Car {
/*
* make:String, model:String, price:double Methods: Parameterized
* constructor for initializing a new Car instance’s properties Getters for
* each property toString method to display a Car’s properties
*/
String make;
String model;
double price;
public Car(String make, String model, double price) {
super();
this.make = make;
this.model = model;
this.price = price;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return make + " - " + model + " - " + price;
}
}
CarComparator.java:
package smith20;
import java.util.Comparator;
// This class is to compare Car objects by price
// from lowest to highest Class CarLists
public class CarComparator implements Comparator<Car> {
@Override
public int compare(Car o1, Car o2) {
Car c1 = (Car) o1;
Car c2 = (Car) o2;
if (c1.price == c2.price)
return 0;
else if (c1.price > c2.price)
return 1;
else
return -1;
}
}
CarOperations.java
package smith20;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class CarOperations {
public static void main(String[] args) {
/*
* (main class) code it in this order Create an ArrayList of at least
* six cars, not ordered in any way by price.
*/
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("Audi", "Cabriolet", 188000));
cars.add(new Car("Audi", "Q5 Hybrid", 250142));
cars.add(new Car("Acura", "CL", 350010));
cars.add(new Car("Ferrari", "360 Spider", 169396));
cars.add(new Car("FIAT", "124 Spider", 200500));
cars.add(new Car("BMW", "1 Series M", 164235));
// Create an Iterator and use it to display the entire list of cars.
System.out.println("Displaying entiler list of cars ");
Iterator<Car> iterator = cars.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("-------------------------------------------");
// Display the most expensive car in the list
Collections.sort(cars, new CarComparator());
System.out.println("Most expensive car: " + cars.get(cars.size() - 1));
System.out.println("-------------------------------------------");
/*
* Use the Collections class. Create a linked list from the arraylist.
* Insert another Car at index 4 in the linked list. This car should now
* be the cheapest in the list.
*/
LinkedList<Car> carsLL = new LinkedList<>(cars);
Car c = new Car("Tata", "indica", 100000);
carsLL.add(3, c);
System.out.println("Car inserted at 4th position: "+c+" ");
ListIterator<Car> listIterator = carsLL.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("-------------------------------------------");
//Sort the linked list in order of increasing price.
Collections.sort(carsLL,new CarComparator());
System.out.println("Dispalying the sorted linked list ");
// ListIterator to display the linked list in order of increasing price.
listIterator = carsLL.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("-------------------------------------------");
// Remove the first and last Cars from the linked list, displaying them
// as you do so.
carsLL.removeFirst();
carsLL.removeLast();
System.out.println("Linked list after removing first and last cars ");
listIterator = carsLL.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
System.out.println("-------------------------------------------");
/*
* **JAVA ASSIGNMENT** Use the ListIterator
* again to iterate back through the list so cars are now shown in order of
* decreasing price.
*/
System.out.println("Linked list in the order of decreasing price ");
listIterator = carsLL.listIterator(carsLL.size());
while(listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
System.out.println("-------------------------------------------");
}
}
Output:
Displaying entiler list of cars
Audi - Cabriolet - 188000.0
Audi - Q5 Hybrid - 250142.0
Acura - CL - 350010.0
Ferrari - 360 Spider - 169396.0
FIAT - 124 Spider - 200500.0
BMW - 1 Series M - 164235.0
-------------------------------------------
Most expensive car: Acura - CL - 350010.0
-------------------------------------------
Car inserted at 4th position: Tata - indica - 100000.0
BMW - 1 Series M - 164235.0
Ferrari - 360 Spider - 169396.0
Audi - Cabriolet - 188000.0
Tata - indica - 100000.0
FIAT - 124 Spider - 200500.0
Audi - Q5 Hybrid - 250142.0
Acura - CL - 350010.0
-------------------------------------------
Dispalying the sorted linked list
Tata - indica - 100000.0
BMW - 1 Series M - 164235.0
Ferrari - 360 Spider - 169396.0
Audi - Cabriolet - 188000.0
FIAT - 124 Spider - 200500.0
Audi - Q5 Hybrid - 250142.0
Acura - CL - 350010.0
-------------------------------------------
Linked list after removing first and last cars
BMW - 1 Series M - 164235.0
Ferrari - 360 Spider - 169396.0
Audi - Cabriolet - 188000.0
FIAT - 124 Spider - 200500.0
Audi - Q5 Hybrid - 250142.0
-------------------------------------------
Linked list in the order of decreasing price
Audi - Q5 Hybrid - 250142.0
FIAT - 124 Spider - 200500.0
Audi - Cabriolet - 188000.0
Ferrari - 360 Spider - 169396.0
BMW - 1 Series M - 164235.0
-------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.