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

Help!!!!! Problem 1 ( List Client ) (30 points) : Suppose that you have a singly

ID: 3817301 • Letter: H

Question

Help!!!!!

Problem 1 (List Client) (30 points): Suppose that you have a singly linked list that is created by the following statement

ListInterface<Double> quizScores = new LList<Double>();

A professor wants to add to this list quiz scores received by a student throughout a course and find the average of these quiz scores, ignoring the lowest score. Write a Java program at the client level that will:

a. Find and remove the lowest score in the list

b. Compute the average of the scores remaining in the list

c. Print the result.

2. Problem 2 (List Implementation) (35 points): Suppose that you want an operation for the ADT List that returns the first position of a given object in the list. The header of the method is as follows:

public int getFirstPosition(T anObject)

where T is the general type of the objects in the list. Write an implementation of this method for the LList class. Include testing of the method in the main method of the LList class.

3. Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows:

public boolean removeValue(T aValue)

where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class.

What to turn in:

Three java files: a file containing the solution of Problem 1 and the files LList.java and DoublyLList.java.

Explanation / Answer

PROGRAM CODE:

ListInterface.java

package linkedlist;


public interface ListInterface<T extends Comparable<? super T>> {
   class Node<T>
   {
       T data;
       Node<T> next;
      
       Node(T data)
       {
           this.data = data;
           next = null;
       }
   }
  
  
   void add(T data);
   void remove(T data);
   T findLowestScore();
   void print();
   double computeAverage();
}

LList.java

package linkedlist;

public class LList<T extends Comparable<? super T>> implements ListInterface<T>
{
  

   Node<T> list;
   public int size;
   public LList() {
       list = null;
       size = 0;
   }
   @Override
   public void add(T data) {
       Node<T> newNode = new Node<T>(data);
       if(size == 0)
           list = newNode;
       else
       {
           Node<T> temp = list;
           while(temp.next != null)
           {
               temp = temp.next;
           }
           temp.next = newNode;
          
       }
       size++;
   }

   @Override
   public void remove(T data) {
       Node<T> temp = list;
       if(size>0)
       {
           if(list.data.compareTo(data)==0)
           {
               list = list.next;
           }
           else
           {
               while(temp.next != null)
               {
                   if(temp.next.data.compareTo(data)==0)
                   {
                       if(temp.next.next == null)
                           temp.next = null;
                       else
                       {
                           temp.next.data = temp.next.next.data;
                           temp.next.next = temp.next.next.next;
                       }
                       break;
                   }
                   else temp = temp.next;
               }
           }
           size--;
       }
   }
  
   public T findLowestScore()
   {
       if(size == 0)
       {
           System.out.println("List is empty ");
           return null;
       }
       T min = null;
       if(size>0)
           min = list.data;
      
       Node<T> temp = list;
       while(temp != null)
       {
           if(temp.data.compareTo(min)<0)
               min = temp.data;
           temp = temp.next;
       }
       return min;
   }
   @Override
   public void print() {
      
       Node<T> temp = list;
       System.out.print("[ ");
       while(temp != null)
       {
           System.out.print(temp.data + " ");
           temp = temp.next;
       }
       System.out.println("]");
   }
   @Override
   public double computeAverage() {
       double sum = 0;

       Node<T> temp = list;
       while(temp != null)
       {
           sum += (Double)temp.data;
           temp = temp.next;
       }
       return sum/size;
   }
}

Scores.java

package linkedlist;

import java.util.Scanner;

public class Scores {

   public static void main(String[] args) {
       ListInterface<Double> quizScores = new LList<Double>();
       double lowestScore, avg, score;
       char choice;
       boolean isContinued = true;
       while(isContinued)
       {
           System.out.println("MENU a. Find and remove the lowest score in the list");
           System.out.println("b. Compute the average of the scores remaining in the list");
           System.out.println("c. Print the result d. Add scores e. Exit");
           System.out.print("Enter your choice: ");
           Scanner kbd = new Scanner(System.in);
           choice = kbd.nextLine().charAt(0);
           switch(choice)
           {
               case 'a': lowestScore = quizScores.findLowestScore();
                       quizScores.remove(lowestScore);
                       System.out.println("The lowest score of " + lowestScore + " was removed.");
                   break;
               case 'b': avg = quizScores.computeAverage();
                       System.out.println("Average is " + avg);
                       break;
               case 'c': quizScores.print(); break;
               case 'd': System.out.print("Enter score: ");
                       score = Double.valueOf(kbd.nextLine());
                       quizScores.add(score);
                       break;
               case 'e':isContinued = false; break;
           }
       }
      
       DoublyLList<Double> list = new DoublyLList<>();
       list.add(13.4);
       list.add(54.6);
       list.add(23.4);
       //list.add(78.9);
       System.out.println("First index of 23.4: " + list.getFirstPosition(23.4));
       list.print();
       System.out.println("After removing value of 23.4");
       list.removeValue(23.4);
       list.print();
   }

}

DoublyLList.java

package linkedlist;


public class DoublyLList<T extends Comparable<? super T>> {

   class Node
   {
       T data;
       Node next, prev;
      
       Node(T data)
       {
           this.data = data;
           prev = null;
           next = null;
       }
      
       void setPrev(Node node)
       {
           this.prev = node;
       }
      
       void setNext(Node node)
       {
           this.next = node;
       }
   }
   Node list;
   int size;
  
   public DoublyLList() {
       list = null;
       size = 0;
   }
  
   public void add(T data)
   {
       Node newNode = new Node(data);
       if(size == 0)
       {
           list = newNode;
       }
       else
       {
           Node temp = list;
          
           while(temp.next != null)
           {
               temp = temp.next;
           }
           temp.next = newNode;
           newNode.setPrev(temp);
          
       }
       size++;
   }

  
   public int getFirstPosition(T anObject)
   {
       Node temp = list;
       int index = 0;
       while(temp != null)
       {
           if(temp.data.compareTo(anObject) == 0)
           {
               return index;
           }
           else
           {
               index++;
               temp = temp.next;
           }
       }
       return -1;
   }
  
   public boolean removeValue(T aValue)
   {
       boolean isRemoved = false;
      
       Node temp = list;
       if(list.data.compareTo(aValue)==0)
       {
           list = list.next;
           isRemoved = true;
           size--;
       }
       else
       {
           while(temp.next != null)
           {
               if(temp.next.data.compareTo(aValue)==0)
               {
                   if(temp.next.next!= null)
                   {
                       temp.next = temp.next.next;
                   }
                   else
                   {
                       temp.next = null;
                   }
                   isRemoved = true;
                   size--;
                   break;
               }
               temp = temp.next;
           }
       }
       return isRemoved;
   }
  
   public void print()
   {
       Node temp = list;
       System.out.print("[ ");
       while(temp != null)
       {
           System.out.print(temp.data + " ");
           temp = temp.next;
       }
       System.out.println("]");
   }
}

OUTPUT:

MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: e
First index of 23.4: 2
[ 13.4 54.6 23.4 ]
After removing value of 23.4
[ 13.4 54.6 ]

RUN#2:

MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: d
Enter score: 34.5
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: d
Enter score: 11.2
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: d
Enter score: 54.3
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: c
[ 34.5 11.2 54.3 ]
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: a
The lowest score of 11.2 was removed.
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: b
Average is 44.4
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: c
[ 34.5 54.3 ]
MENU
a. Find and remove the lowest score in the list
b. Compute the average of the scores remaining in the list
c. Print the result
d. Add scores
e. Exit
Enter your choice: e
First index of 23.4: 2
[ 13.4 54.6 23.4 ]
After removing value of 23.4
[ 13.4 54.6 ]

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