Top Three Test Scores Implement a class that maintains a lis

Top Three Test Scores Implement a class that maintains a list of the top three test scores entered on the console. The program should prompt the user for a number (no need to test for invalid entry; zero entry will exit the program), add the number to an internally kept sorted (ascending order) linked list (if the number meets the rules below), and display the content of the linked list. Rules: The list should only allow a maximum of three elements After the list is full, an attempt to add another score that is less than or equal to an existing score should be ignored, and adding a score that is greater than the minimum score causes the minimum score to be dropped from the list to preserve its maximum size. The class should have a constructor that sets up an empty list, and all the necessary methods to insert, remove, and check for an existing score. I will leave it up to you to come up with your own method names. Here is a sample output: INPUT rightarrow Please enter a test score (0 to exit): 95 OUTPUT rightarrow Add test score 95 to list Number of elements in linked list = 1 Scores stored = 95 INPUT rightarrow Please enter a test score (0 to exit): 72 OUTPUT rightarrow Add test score 72 to list Number of elements in linked list = 2 Scores stored = 95, 72 INPUT - > Please enter a test score (0 to exit): 80 OUTPUT rightarrow Add test score 80 to list Number of elements in linked list = 3 Scores stored = 95, 80, 72 INPUT - > Please enter a test score (0 to exit): 82 OUTPUT rightarrow Add test score 82 to list Remove lowest test score 72 Number of elements in linked list = 3 Scores stored =95, 82, 80 INPUT rightarrow    Please enter a test score (0 to exit): 82 OUTPUT rightarrow Test score 82 already exists in the list, ignore entry Number of elements in linked list = 3 Scores stored =95, 82, 80 INPUT rightarrow Please enter a test score (0 to exit): 70 OUTPUT rightarrow Test score 70 is less than the minimum score In the list, ignore entry Number of elements In linked list = 3 Scores stored = 95, 82, 80

Solution

import java.util.Scanner;

public class SortedLinkedList {

   public static void add(java.util.LinkedList<Integer> list, int n) {

       if (list.isEmpty())

           list.add(n);

       else if (list.size() == 1) {

           if (list.getFirst() == n) {

               System.out.println(\"Test score \" + n + \" already exists ignore entry\");

               return;

           } else if (list.getFirst() < n) {

               list.addFirst(n);

               return;

           } else {

               list.addLast(n);

               return;

           }

       } else if (list.size() == 2) {

           if (list.getFirst() == n || list.getLast() == n) {

               System.out.println(\"Test score \" + n + \" already exists ignore entry\");

               return;

           }

           else if (list.getFirst() < n)

           {

               list.addFirst(n);

               return;

           }

           else if (list.getLast() > n)

           {

               list.addLast(n);

               return;

           }

           else {

               list.add(1, n);

               return;

           }

       } else if (list.size() == 3) {

           if (list.getFirst() == n || list.getLast() == n || list.get(1) == n) {

               System.out.println(\"Test score \" + n + \" already exists ignore entry\");

               return;

           } else if (list.getFirst() < n) {

               System.out.println(\"remove lowest score \" + list.getLast());

               list.removeLast();

               list.addFirst(n);

               return;

           } else if (list.getLast() > n) {

               System.out.println(n + \" is less than minimum score in the list ignore entry\");

               return;

           }

           else if (list.getFirst() > n && list.get(2) > n) {

               System.out.println(\"remove lowest score \" + list.getLast());

               list.removeLast();

               list.addLast(n);

               return;

           } else if (list.getFirst() > n && list.get(2) < n) {

               System.out.println(\"remove lowest score \" + list.getLast());

               list.removeLast();

               list.add(1, n);

               return;

           }

       }

   }

   public static void print(java.util.LinkedList<Integer> list) {

       if (list.isEmpty())

           System.out.println(\"Empty list\");

       else if (list.size() == 1)

           System.out.println(\"Scores stored = \" + list.getFirst());

       else if (list.size() == 2)

           System.out.println(\"Scores stored = \" + list.getFirst() + \",\" + list.getLast());

       else if (list.size() == 3)

           System.out.println(\"Scores stored = \" + list.getFirst() + \",\" + list.get(1) + \",\" + list.getLast());

   }

   public static void main(String[] args) {

       java.util.LinkedList<Integer> list = new java.util.LinkedList<Integer>();

       Scanner in = new Scanner(System.in);

       System.out.println(\"Please enter test score (0 to exit)\");

       int num = in.nextInt();

       while (num != 0) {

           System.out.println(\"Add test score \" + num + \" to the list\");

           add(list, num);

           System.out.println(\"Number of elements in the list = \" + list.size());

           print(list);

           System.out.println(\"Please enter test score (0 to exit)\");

           num = in.nextInt();

       }

       System.out.println(\"Exiting..\");

   }

}

 Top Three Test Scores Implement a class that maintains a list of the top three test scores entered on the console. The program should prompt the user for a num
 Top Three Test Scores Implement a class that maintains a list of the top three test scores entered on the console. The program should prompt the user for a num
 Top Three Test Scores Implement a class that maintains a list of the top three test scores entered on the console. The program should prompt the user for a num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site