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

Java LinkedList - Need to change this arraylist to linkedlist, but don\'t know h

ID: 3829891 • Letter: J

Question

Java LinkedList - Need to change this arraylist to linkedlist, but don't know how. It says to create my own linked list, not to use the LinkedList that is built into java.

//Driver.java

import java.util.*;


public class Driver {
  
   static ArrayList<Students> studentList = new ArrayList<Students>();
  
   public static void main(String[] args) {

       new Driver();
   }
  
   public Driver() {
       Scanner input = new Scanner(System.in);
      
       System.out.println("Select an option below");
   System.out.println("");
  
   while(true) {
       System.out.println("1: Add Student");
       System.out.println("2: Display Student");
       System.out.println("3: Remove Student");
       System.out.println("4: Exit");
  
   int userChoice = input.nextInt();   
   switch(userChoice) {
   case 1:
   addStudent();
   break;
   case 2:
       displayStudent();
   break;
   case 3:
       removeStudent();
       break;
   case 4:
   System.out.println("Thank you! See you again!");
   System.exit(4);
   }
   }
   }
  
   private void addStudent() {
       Students newStudents = new Students();
       Scanner input = new Scanner(System.in);
       String FirstName, LastName;
       Students students;
      
       if(studentList.size() >= 10) {
           System.out.println("Your list is full!");
       } else {
       System.out.print("Enter First Name of student to want to add : ");
       FirstName = input.nextLine();
       System.out.print("Enter Last Name of student to want to add : ");
       LastName = input.nextLine();
       newStudents.setFirstName(FirstName);
       newStudents.setLastName(LastName);
       studentList.add(studentList.size(), newStudents);

   }
   }
  
   private void removeStudent() {
       String FirstName, LastName;
       int i=0,studentfind=0;
       Scanner input = new Scanner(System.in);
       System.out.print("Enter First Name of student to want to remove : ");
       FirstName = input.nextLine();
       System.out.print("Enter Last Name of student to want to remove : ");
       LastName = input.nextLine();
      
       for (i = 0; i < studentList.size(); i++ ){
       if(studentList.get(i).getFristName().equals(FirstName) && studentList.get(i).getLastName().equals(LastName))
       {System.out.println("Student removed ");studentList.remove(i); studentfind=1;break;}
       }
       if(studentfind==0) {System.out.println("No such student in list ");}
   }

   private void displayStudent() {
       int i=0;
       System.out.println(" Students are : ");
       for(i=0;i<studentList.size();i++)
       {System.out.print(studentList.get(i).toString());}
       System.out.println(" ");
       if(studentList.isEmpty()) System.out.println(" List is empty ");
      
      
   }
   }

//Students.java

public class Students {
  
   private String FirstName;
   private String LastName;

  
   public Students(String FirstName, String LastName) {
       this.FirstName = FirstName;
       this.LastName = LastName;
   }
  
   public Students() {
      
   }
  
   public String getFristName() {
       return FirstName;
   }
  
   public void setFirstName(String FirstName) {
       this.FirstName = FirstName;
   }
  
   public String getLastName() {
       return LastName;
   }
  
   public void setLastName(String LastName) {
       this.LastName = LastName;
   }
  
   public String toString() {
       return " First Name : " + FirstName + " Last Name : " + LastName + " ";
   }

}

Explanation / Answer

Hi, I have cjanged ArrayList to LinkedList.

import java.util.LinkedList;

import java.util.Scanner;

public class Driver {

   static LinkedList<Students> studentList = new LinkedList<Students>();

   public static void main(String[] args) {

       new Driver();

   }

   public Driver() {

       Scanner input = new Scanner(System.in);

       System.out.println("Select an option below");

       System.out.println("");

       while(true) {

           System.out.println("1: Add Student");

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

           System.out.println("3: Remove Student");

           System.out.println("4: Exit");

           int userChoice = input.nextInt();

           switch(userChoice) {

           case 1:

               addStudent();

               break;

           case 2:

               displayStudent();

               break;

           case 3:

               removeStudent();

               break;

           case 4:

               System.out.println("Thank you! See you again!");

               input.close();

               System.exit(4);

           }

       }

   }

   private void addStudent() {

       Students newStudents = new Students();

       Scanner input = new Scanner(System.in);

       String FirstName, LastName;

       if(studentList.size() >= 10) {

           System.out.println("Your list is full!");

       } else {

           System.out.print("Enter First Name of student to want to add : ");

           FirstName = input.nextLine();

           System.out.print("Enter Last Name of student to want to add : ");

           LastName = input.nextLine();

           newStudents.setFirstName(FirstName);

           newStudents.setLastName(LastName);

           studentList.add(studentList.size(), newStudents);

       }

       input.close();

   }

   private void removeStudent() {

       String FirstName, LastName;

       int i=0,studentfind=0;

       Scanner input = new Scanner(System.in);

       System.out.print("Enter First Name of student to want to remove : ");

       FirstName = input.nextLine();

       System.out.print("Enter Last Name of student to want to remove : ");

       LastName = input.nextLine();

       for (i = 0; i < studentList.size(); i++ ){

           if(studentList.get(i).getFristName().equals(FirstName) && studentList.get(i).getLastName().equals(LastName))

           {System.out.println("Student removed ");studentList.remove(i); studentfind=1;break;}

       }

       if(studentfind==0) {

           System.out.println("No such student in list ");

       }

       input.close();

   }

   private void displayStudent() {

       int i=0;

       System.out.println(" Students are : ");

       for(i=0;i<studentList.size();i++)

       {System.out.print(studentList.get(i).toString());}

       System.out.println(" ");

       if(studentList.isEmpty()) System.out.println(" List is empty ");

   }

}

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