In: Computer Science
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.
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.