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

Project 2 -Linked List 1. Design a java interface called IDedobject which has fo

ID: 3789234 • Letter: P

Question

Project 2 -Linked List 1. Design a java interface called IDedobject which has following abstract functions. int getID() //Returns the ID of the object //Prints the details of the ID void printID0 Design a java class Magazine that implements IDedobject interface and has the following class variables: int magazinelD String magazineName String publisherName Implement suitable constructors, a printID function that prints all the variable values in separate lines and getID0 function that returns the magazinelD. Add any access and other functions necessary HNote all your data should be private and methods public inside the class) 3. Design a generic singly linked list java class (Give an appropriate name) to hold objects of the generic type KAnytype This AnyType should extend IDedobject. The linked list class must implement following member functions: A constructor which generates an empty list //empties the linked list void makeEmpty(); Any Type findID(int ID) Get the generic type to get the particular id and returns AnyType. Don't remove the object from the list. returns null if the list is empty or ID not found. boolean insertAtFront( x); insert at front of list or return false if that ID already exists Any Type deleteFromFront(); delete and return the record at the front of the list or return null if the list is empty find and delete the record with the given ID or returns Any Type delete (int ID); null if it isn't found void printAllRecords(); print all elements in the order they appear in the linked list. if list is empty print appropriate message.

Explanation / Answer

IDedObject.java

/**
* Interface
* @author Anonymous
*
*/
public interface IDedObject {
  
   int getID();
   void printID();
}

Magazine.java

/**
* Magazine Class implements interface IDedObject
* @author Anonymous
*
*/
public class Magazine implements IDedObject{
  
   private int magazineID;
   String magazineName;
   String publisherName;
  
   /**
   * Parameterized Constructor
   * @param magazineID
   * @param magazineName
   * @param publisherName
   */
   public Magazine(int magazineID, String magazineName, String publisherName){
       this.magazineID = magazineID;
       this.magazineName = magazineName;
       this.publisherName = publisherName;
   }
  
   /**
   * Returns magazineID
   */
   @Override
   public int getID() {
      
       return magazineID;
   }

   /**
   * Prints magazine details
   */
   @Override
   public void printID() {

       System.out.println("Magazine ID: "+magazineID);
       System.out.println("Magazine Name: "+magazineName);
       System.out.println("Publisher Name: "+publisherName);
      
   }

}


LinkedList.java

/**
* Custom Linked List class
* @author Anonymous
*
* @param <AnyType>
*/
public class LinkedList<AnyType extends IDedObject> {
  
   //Header Node
   private Node start;
   //Size of list
   private int size;
  
   /**
   * Overloaded Constructor
   */
   public LinkedList(){
       start = null;
       size = 0;
   }
  
   /**
   * Method to empty the list
   */
   public void makeEmpty(){
       start = null;
       size = 0;
   }
  
   /**
   * Method to insert AnyType object at the front of the list
   * @param x
   * @return
   */
   public boolean insertAtFront(AnyType x){
      
       Node node = new Node(x, null);
      
       //If list empty, insert x and return true
       if(start == null){
           start = node;
           size++;
           return true;
       }
      
       Node nptr = start;
      
       while(nptr!=null){
           //If ID already present return false
           if(nptr.getData().getID() == x.getID()){
               return false;
           }
           else{
               nptr = nptr.getLink();
           }
       }
      
       //ID not present in the list. Insert x at the front of the list and return true;
       node.setLink(start);
       start = node;
       size++;
      
       return true;
   }
  
   /**
   * Method to find and return AnyType object
   */
   public AnyType findID(int ID){
       Node nptr = start;
      
       while(nptr!=null){
           if(nptr.getData().getID() == ID){
               return nptr.getData();
           }
           else{
               nptr = nptr.getLink();
           }
       }
      
       return null;
   }
  
   /**
   * Method to delete from the front of the list
   * and return the deleted record
   * @return AnyType object
   */
   public AnyType deleteFromFront(){
      
       if(start == null){
           return null;
       }
      
       AnyType x = start.getData();
       start = start.getLink();
       size--;
       return x;
   }
  
   /**
   * Method to delete record which contains ID
   * and return the deleted record
   * @param ID
   * @return
   */
   public AnyType delete(int ID){
      
       if(start == null)
           return null;
      
       if(start.getData().getID() == ID){
           AnyType x = start.getData();
           start = start.getLink();
           size--;
           return x;
       }
      
       AnyType x = null;
       Node ptr = start;
       Node nptr = start.getLink();
      
       while(nptr!=null){
           if(nptr.getData().getID() == ID){
               x=nptr.getData();
               ptr.setLink(nptr.getLink());
               size--;
               break;
           }
           else{
               ptr = nptr;
               nptr = nptr.getLink();
           }
          
       }
       return x;
   }
  
   /**
   * Print all records in the list
   */
   public void printAllRecords(){
      
       if(start==null){
           System.out.println("List is empty");
       }
      
       Node nptr = start;
      
       while(nptr!=null){
           nptr.getData().printID();
           nptr = nptr.getLink();
       }
   }
  
   /**
   * Inner Node class
   * @author Anonymous
   *
   */
   class Node{
      
       private AnyType data;
       //Link to next node
       private Node link;
      
       Node(AnyType data, Node link){
           this.data = data;
           this.link = link;
       }

       public AnyType getData() {
           return data;
       }

       public void setData(AnyType data) {
           this.data = data;
       }

       public Node getLink() {
           return link;
       }

       public void setLink(Node link) {
           this.link = link;
       }
      
      
   }
}


Main.java

import java.util.Scanner;

/**
* Main Class
* @author Anonymous
*
*/
public class Main {
  
   public static void main(String args[]){
      
       LinkedList<Magazine> magazineList = new LinkedList<Magazine>();
      
       String str = "Operations on List "+
               "1. Make Empty "+
               "2. Find ID "+
               "3. Insert At Front "+
               "4. Delete From Front "+
               "5. Delete ID "+
               "6. Print All Records "+
               "7. Done "+
               "Your Choice: ";
       System.out.print(str);
      
       Scanner sc = new Scanner(System.in);
      
       int choice;
       choice=sc.nextInt();
      
       //Iterate while user is not done
       while(choice!=7){
          
           int magazineID;
           String magazineName, publisherName;
           IDedObject object;
           Magazine magazine;
           // Switch case for choices
           switch(choice){
           case 1:
               //Empty the list
               magazineList.makeEmpty();
               break;
              
           case 2:
               //Find magazine ID in the list
               System.out.print("Enter Magazine ID: ");
               magazineID = sc.nextInt();
               Magazine m = magazineList.findID(magazineID);
               if(m!=null){
                   //Print found magazine details
                   m.printID();
               }
               else{
                   //Print magazine not found
                   System.out.println("No magazine found");
               }
               break;
              
           case 3:
               //Enter magazine into the list
               System.out.print("Enter Magazine ID: ");
               magazineID = sc.nextInt();
               System.out.print("Enter Magazine Name: ");
               magazineName = sc.next();
               System.out.print("Enter publisher Name: ");
               publisherName = sc.next();
              
               magazine = new Magazine(magazineID, magazineName, publisherName);
               //Insert magazine
               if(magazineList.insertAtFront(magazine)){
                   System.out.println("Magazine Added");
               }
               else{
                   //If magazine already present
                   System.out.println("Magazine Already Present");
               }
               break;
              
           case 4:
               //Delete record from front of the list
               object = magazineList.deleteFromFront();
               if(object!=null){
                   System.out.println("Magazine Deleted");
                   object.printID();
               }
               else{
                   System.out.println("List is empty");
               }
               break;
              
           case 5:
               //Delete record with ID
               System.out.print("Enter Magazine ID to delete: ");
               magazineID = sc.nextInt();
               object = magazineList.delete(magazineID);
               if(object!=null){
                   System.out.println("Magazine Deleted");
                   object.printID();
               }
               else{
                   //No such record found
                   System.out.println("Magazine Not Found");
               }
               break;
              
           case 6:
               //Print all records
               magazineList.printAllRecords();
               break;
           default:
               System.out.println("Invalid Choice");
               break;
           }
          
           //Input choice again
           System.out.println(str);
           choice = sc.nextInt();
       }
   }

}