Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 20 Assignment Requirements Create a new Eclipse project named so as to i

ID: 3823742 • 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.

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.

Use the ListIterator again to iterate back through the list so cars are now shown in order of decreasing price.

Explanation / Answer

Hi,

Pleae see the classesin package project.package1. You can change the package name.

Please comment for any queries/feedbacks.

Thanks.

Car.java

package project.package1;

public class Car {
   private String make;
   private String model;
   private double price;
  
   //Parameterized constructor for initializing a new Car instance’s properties
   public Car(String make, String model, double price) {
       super();
       this.make = make;
       this.model = model;
       this.price = price;
   }
  
   //Getters for each property
   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;
   }

   //toString method to display a Car’s propertiesv
   public String toString() {
       return "Car [make=" + make + ", model=" + model + ", price=" + price+ "]";
   }
  
  


}

CarComparator.java

package project.package1;

import java.util.Comparator;

public class CarComparator implements Comparator<Car> {

   public CarComparator() {
       // TODO Auto-generated constructor stub
   }

   @Override
   public int compare(Car o1, Car o2) {
       if(o1.getPrice() == o2.getPrice()){
           return 0;
       }
       return Double.valueOf(o1.getPrice()).compareTo(Double.valueOf(o2.getPrice()));
   }

  
}

CarLists.java

package project.package1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class CarLists {

   public static void main(String [] args){
       //Create an ArrayList of at least six cars, not ordered in any way by price
       ArrayList<Car> carList = new ArrayList<Car>();
       Car car1= new Car("2016", "Creata", 700);
       Car car2= new Car("2015", "Cruze", 400);
       Car car3= new Car("2013", "Magna", 900);
       Car car4= new Car("2016", "City", 600);
       Car car5= new Car("2016", "Kwid",800);
       Car car6= new Car("2017", "Baleno", 450);
       carList.add(car1);
       carList.add(car2);
       carList.add(car3);
       carList.add(car4);
       carList.add(car5);
       carList.add(car6);
      
       //Create an Iterator and use it to display the entire list of cars.
       System.out.println("Display all the cars:");
       Iterator<Car> iterator = carList.iterator();
       while(iterator.hasNext()){
           Car car = iterator.next();
           System.out.println(car);
       }
      
       //Display the most expensive car in the list. Use the Collections class.
       Collections.sort(carList , new CarComparator());
       System.out.println("Most Expensive car : "+carList.get(carList.size()-1));
      
       //Create a linked list from the arraylist.
       LinkedList<Car> carLinkedList = new LinkedList<Car>(carList);
      
       //Insert another Car at index 4 in the linked list. This car should now be the cheapest in the list.
       System.out.println("Adding the cheapest Car");
       Car cheapCar=new Car("2016", "Nano", 100);
       carLinkedList.add(4, cheapCar);  
      
       //Sort the linked list in order of increasing price.
       Collections.sort(carLinkedList , new CarComparator());
      
       //Create and use a ListIterator to display the linked list in order of increasing price.
       System.out.println("Display all the cars in LinkedList:");
       ListIterator<Car> iteratorList = carLinkedList.listIterator();
       while(iteratorList.hasNext()){
           Car car = iteratorList.next();
           System.out.println(car);
       }
      
       //Remove the first and last Cars from the linked list, displaying them as you do so.
       System.out.println("Removing First and Last from Linkedlist");
       carLinkedList.remove(0); //Remove the first
       carLinkedList.remove(carLinkedList.size()-1); //Remove the last
      
      
       //Use the ListIterator again to iterate back through the list so cars are now shown in order of decreasing price.
       System.out.println("Display all the cars in LinkedList after removing:");
       Collections.sort(carLinkedList , new CarComparator());
       iteratorList = carLinkedList.listIterator();
       while(iteratorList.hasNext()){
           Car car = iteratorList.next();
           System.out.println(car);
       }
      
              
   }

}

Sample output:
Display all the cars:
Car [make=2016, model=Creata, price=700.0]
Car [make=2015, model=Cruze, price=400.0]
Car [make=2013, model=Magna, price=900.0]
Car [make=2016, model=City, price=600.0]
Car [make=2016, model=Kwid, price=800.0]
Car [make=2017, model=Baleno, price=450.0]
Most Expensive car : Car [make=2013, model=Magna, price=900.0]
Adding the cheapest Car
Display all the cars in LinkedList:
Car [make=2016, model=Nano, price=100.0]
Car [make=2015, model=Cruze, price=400.0]
Car [make=2017, model=Baleno, price=450.0]
Car [make=2016, model=City, price=600.0]
Car [make=2016, model=Creata, price=700.0]
Car [make=2016, model=Kwid, price=800.0]
Car [make=2013, model=Magna, price=900.0]
Removing First and Last from Linkedlist
Display all the cars in LinkedList after removing:
Car [make=2015, model=Cruze, price=400.0]
Car [make=2017, model=Baleno, price=450.0]
Car [make=2016, model=City, price=600.0]
Car [make=2016, model=Creata, price=700.0]
Car [make=2016, model=Kwid, price=800.0]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote