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

**JAVA ASSIGMENT** Chapter 20 Assignment Requirements Create a new Eclipse proje

ID: 3698337 • Letter: #

Question

**JAVA ASSIGMENT**

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.

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

Explanation / Answer

CarLists.java

package smith20;

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

public class CarLists {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       ArrayList<Car> list = new ArrayList<Car>();
       list.add(new Car("Make1", "Model1", 5000));
       list.add(new Car("Make2", "Model2", 6000.2));
       list.add(new Car("Make3", "Model3", 3000.6));
       list.add(new Car("Make4", "Model4", 2000.5));
       list.add(new Car("Make5", "Model5", 8000.8));
       list.add(new Car("Make6", "Model6", 7000.7));
       CarComparator comp = new CarComparator();
       System.out.println(Collections.max(list, comp));
       System.out.println("-------------------------------------");
       System.out.println("Sort the linked list in order of increasing price");
       LinkedList<Car> linkedList = new LinkedList<Car>(list);
       linkedList.set(4, new Car("Make7", "Model7", 1000.9));
       Collections.sort(linkedList,comp);
       ListIterator itr = linkedList.listIterator();
       while(itr.hasNext())
   {
   System.out.println(itr.next());
   }      
       System.out.println("-------------------------------------");
       System.out.println("Sort the linked list in order of decreasing price");
       linkedList.removeFirst();
       linkedList.removeLast();
       itr = linkedList.listIterator(linkedList.size());

       // Iterate in reverse.
       while(itr.hasPrevious()) {
       System.out.println(itr.previous());
       }      
      
   }

}

Car.java

package smith20;

public class Car {
   private String make;
   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;
   }
   private String model;
   private double price;
   public Car(String make, String model, double price){
       this.make =make;
       this.model =model;
       this.price = price;
   }
   public String toString(){
       return "Make : "+getMake()+" Model : "+getModel()+" Price : "+getPrice();
   }
  
}

CarComparator.java

package smith20;

import java.util.Comparator;
public class CarComparator implements Comparator<Car> {
public int compare(Car c1, Car c2){
return (int) (c1.getPrice() - c2.getPrice());
}
}

Output:

Make : Make5 Model : Model5 Price : 8000.8
-------------------------------------
Sort the linked list in order of increasing price
Make : Make7 Model : Model7 Price : 1000.9
Make : Make4 Model : Model4 Price : 2000.5
Make : Make3 Model : Model3 Price : 3000.6
Make : Make1 Model : Model1 Price : 5000.0
Make : Make2 Model : Model2 Price : 6000.2
Make : Make6 Model : Model6 Price : 7000.7
-------------------------------------
Sort the linked list in order of decreasing price
Make : Make2 Model : Model2 Price : 6000.2
Make : Make1 Model : Model1 Price : 5000.0
Make : Make3 Model : Model3 Price : 3000.6
Make : Make4 Model : Model4 Price : 2000.5