Question

In: Computer Science

Objectives • To work with linked chains of nodes • To create a component of score...

Objectives
• To work with linked chains of nodes
• To create a component of score storage and update software used in sporting software, or a computer game

Instructions
For this assignment, you will construct a list of scores

ScoreNode class
The ScoreNode class represents an individual score. This requires a name of type String, and a score of type integer, and obviously a reference to the next node in the chain.

ScoresList class

Method Notes
ScoresList No-argument constructor. Sets the ScoreList's linked chain head node (front) to null
ScoresList (ScoresList otherList) A copy constructor. This constructor will perform a deep copy on the otherList, making an exact copy of it.
add(String name, int score) Add the name/score value to the linked chain in the correct order based on what is already in the chain (if anything). Remember, you must maintain the list in sorted order, descending.
print() The print method simply prints out the score list represented by the calling ScoreList object. Given a ScoreList object, mySL, the call to mySL will simply print out the scores by walking down the linked chain. Assume all printing is done to the console.


File Input
The file scores.txt will contain, on each line, the name of the player, followed by their high score, such as the following:
Bob 150
Dan 220
Samantha 70
Ksenia 175
Nathan 15

The files are, in general, not going to be in a sorted order. The ScoresList class should, as you add the names and corresponding scores from file, keep them in descending (largest to smallest) order in the linked chain of nodes.

Corresponding Console Output and User Interaction
For the above file, the output will display the high scores in sorted order from highest to lowest, and interaction with the user might look like the following:
Dan 220
Ksenia 175
Bob 150
Samantha 70
Nathan 15
Would you like to add another (1) or quit the program (2)?
1
Write the name followed by score
Ali 190
The new scores are:
Dan 220
Ali 190
Ksenia 175
Bob 150
Samantha 70
Nathan 15
Would you like to add another (1) or quit the program (2)?
2
Thanks for using the program! Goodbye!


The output, including additions made by the user, do not affect the original file. They only change the in-memory copy of the ScoreList. You continue asking the user if they’d like to add another or quit the program until they ask to quit the program.

The answer needs to be in Java.

Solutions

Expert Solution

Java code for above problem

import java.util.*;
import java.io.*;

// class for ScoreNode
class ScoreNode
{
   // class variables
   String name;
   int score;
   ScoreNode next;
  
   // constructor that initialises the class variables
   ScoreNode(String name,int score)
   {
       this.name=name;
       this.score=score;
       this.next=null;  
   }
}

// class for ScoreList
class ScoreList
{
   // class variable
   ScoreNode head=null;
  
   // no-argument constructor
   ScoreList()
   {
       this.head=null;
   }
  
   // copy constructor
   ScoreList(ScoreList list)
   {
       for(ScoreNode node=list.head;node!=null;node=node.next)
       {
           add(node.name,node.score);
       }  
   }
  
   // method that adds new ScoreNode into list
   public void add(String name,int score)
   {
       ScoreNode newNode=new ScoreNode(name,score);
       ScoreNode node=this.head;
       ScoreNode previous_node=null;
      
       // traverse list from head to end of list or a value where score is less than the node's score
       while(node!=null && node.score>score)
       {
           previous_node=node;
           node=node.next;
       }
      
       // if previous_node is null, then it means either list is empty or given score is higher than all nodes in list, so add newNode at head of list
       if(previous_node==null)
       {
           newNode.next=this.head;
           this.head=newNode;
       }
      
       // else add the newNode in middle of list on descending order
       else
       {
           newNode.next=node;
           previous_node.next=newNode;
       }
      
   }
  
   // method that prints the list
   public void print()
   {
       for(ScoreNode node=this.head;node!=null;node=node.next)
       {
           System.out.println(node.name+" "+node.score);
       }
   }
}

// Main class
class Main
{
   // tetsing main method
   public static void main(String args[]) throws IOException
   {
       // first open a file named "scores.txt"
       Scanner file=new Scanner(new File("scores.txt"));
      
       // initailise a ScoreList
       ScoreList list=new ScoreList();
      
       // add all scores into ScoreList
       while(file.hasNext())
       {
           list.add(file.next(),file.nextInt());
       }
       file.close();
      
       // print the list of scores
       System.out.println("Initially, scores list looks as follows: ");
       list.print();
      
       // now loop till user wants to quit the program
       Scanner input=new Scanner(System.in);
       while(true)
       {
           // take user choice
           System.out.println("Would you like to add another (1) or quit the program (2)?");
           int choice=Integer.parseInt(input.nextLine());
          
           // if choice is 1, then take name and score from user and add it into list and atlast print the new list
           if(choice==1)
           {
               System.out.println("Write the name followed by score");
               String line=input.nextLine();
               String [] values=line.split(" ");
               list.add(values[0],Integer.parseInt(values[1]));
               System.out.println("The new scores are:");
               list.print();
           }
          
           // else if choice is 2, then break the infinite loop by printing the message
           else if(choice==2)
           {
               System.out.println("Thanks for using the program! Goodbye!");
               break;
           }
          
           // else print the following error message
           else
           {
               System.out.println("Invalid choice");
           }
          
       }
      
   }
}

Sample output

if "scores.txt" has following data

Bob 150
Dan 220
Samantha 70
Ksenia 175
Nathan 15

then after running the above code, output looks as follows:

Mention in comments if any mistakes or errors are found. Thank you.


Related Solutions

What are the objectives in terms of good management of supply chains?
What are the objectives in terms of good management of supply chains?
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the...
Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linked list with a new node and new data. Assume that the list's head pointer is called head_ptr and the data for the new node is called entry
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
What is adaptive work and how is it linked to change?
What is adaptive work and how is it linked to change?
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods to implement. These methods (which contain TODO comments) are found in linked_list.cpp. Part 1 Implement the copy constructor; use the LinkedBag as inspiration for your solution as the technique of creating a deep copy is essentially the same. Part 2 Implement the replace method. //linked_list.cpp #include "linked_list.h" // Header file #include <cassert> template<class Object> LinkedList<Object>::LinkedList() : headPtr( nullptr ), itemCount( 0 ) { }...
Great work covering the nodes especially with plants and regions. We can use this information to...
Great work covering the nodes especially with plants and regions. We can use this information to track the products and also review processes for efficiencies. What happens to organizations when they meet the demands, but do this at the expense of quality? How can product quality affect future production and sales?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT