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

**** JAVA ( CIS44 ) Create 3 Classes: 1. Linked List| 2. ListTest 3. Car Write a

ID: 3810490 • Letter: #

Question

**** JAVA ( CIS44 )

Create 3 Classes: 1. Linked List| 2. ListTest 3. Car Write a generic class called LinkedList. This class should have a method called add and a method called remove. Also it should have two methods called display and recursiveDisplay. All these methods are public methods. Both methods display and recursive display must display the values in the linked list. The method recursiveDisplay must recursively display the values in the link list. In main create three different LinkedList objects. One of them should hold integer values. The other one should hold string values and the last one should hold Car objects. Add different values to each object and test both display and recursiveDisplay methods

Explanation / Answer

HI, Please find my implemetation.

Please let me know in case of any issue.

###################################

public class LinkedList<T> {

   static class Node<T>{

       T data;

       Node<T> next;

       Node(T d){

           data = d;

       }

   }

   private Node<T> head;

   public LinkedList() {

       head = null;

   }

   // This method add element at front

   public void add(T obj){

       Node<T> newNode = new Node<T>(obj);

       newNode.next = head;

       head = newNode;

   }

   // this method remove the node from front

   public void remove(){

       if(head != null)

           head = head.next;

   }

   public void display(){

      

       Node<T> temp = head;  

       while(temp != null){

           System.out.println(temp.data);

           temp = temp.next;

       }

       System.out.println();

   }

  

   public void recursiveDisplay(){

       recursiveDisplay(head);

       System.out.println();

   }

   private void recursiveDisplay(Node<T> node){

      

       if(node == null)

           return;

      

       System.out.println(node.data);

      

       recursiveDisplay(node.next);

   }

}

###############################################

public class Car {

   // instance variable

   private double price;

   private String model;

   private String make;

   public Car(double price, String model, String make) {

       this.price = price;

       this.model = model;

       this.make = make;

   }

  

   @Override

   public String toString() {

       return "make: "+make+", model: "+model+", price: "+price;

   }

}

#######################################

public class ListTest {

  

   public static void main(String[] args) {

      

       LinkedList<Integer> listInt = new LinkedList<>();

      

       listInt.add(4);

       listInt.add(2);

       listInt.add(12);

       listInt.add(55);

       listInt.add(65);

      

       System.out.println("Display: ");

       listInt.display();

       System.out.println(" Recursive Display: ");

       listInt.recursiveDisplay();

      

       listInt.remove();

      

       System.out.println("Display: ");

       listInt.display();

       System.out.println(" Recursive Display: ");

       listInt.recursiveDisplay();

      

      

       LinkedList<Car> listCar = new LinkedList<Car>();

      

       Car c1 = new Car(3214, "M1", "Hundai");

       Car c2 = new Car(5432.56, "M2", "Tata");

       Car c3 = new Car(9876.45, "M3", "Fortune");

       Car c4 = new Car(65244, "M4", "BMW");

      

       listCar.add(c1);

       listCar.add(c2);

       listCar.add(c3);

       listCar.add(c4);

      

       System.out.println("Display: ");

       listCar.display();

       System.out.println(" Recursive Display: ");

       listCar.recursiveDisplay();

   }

}

/*

Sample run:

Display:

65

55

12

2

4

Recursive Display:

65

55

12

2

4

Display:

55

12

2

4

Recursive Display:

55

12

2

4

Display:

make: BMW, model: M4, price: 65244.0

make: Fortune, model: M3, price: 9876.45

make: Tata, model: M2, price: 5432.56

make: Hundai, model: M1, price: 3214.0

Recursive Display:

make: BMW, model: M4, price: 65244.0

make: Fortune, model: M3, price: 9876.45

make: Tata, model: M2, price: 5432.56

make: Hundai, model: M1, price: 3214.0

*/