In: Computer Science
Convert into pseudo-code for below code
===============================================
class Main
{
public static void main(String args[])
{
Scanner s=new
Scanner(System.in);
ScoresSingleLL score=new
ScoresSingleLL();
while(true)
// take continuous inputs from user till he
enters -1
{
System.out.println("1--->Enter a number\n-1--->exit");
System.out.print("Enter your choice:");
int
choice=s.nextInt();
if(choice!=-1)
{
System.out.print("Enter the score:");
int number=s.nextInt();
System.out.print("Enter the name:");
String name=s.next();
GameEntry entry=new
GameEntry(name,number);
if(number!=-1)
{
if(score.getNumberOfEntries()==10)
// if linkedlist has more than 10 nodes, remove
min score and add new score
{
int
minValue=score.getMinimumValue(); // function to get
minValue
if(minValue<number)
// if min score is greater than given score,
then dont add new node
{
int
minValueIndex=score.getMinimumValueIndex(); // function
to get minValueIndex
score.remove(minValueIndex);
// remove minValueIndex
node
score.add(entry);
// add the new node
}
}
else
{
score.add(entry);
// if
linked list has less than 10 nodes, add the current node
}
}
score.printScore();
// method to print entries in linked lists
score.printHighestToLowest();
}
else
{
break;
}
}
}
}
class ScoresSingleLL
{
SingleLinkedList head=null;
int noOfEntries=0;
public void add(GameEntry e)
{
SingleLinkedList tempNode=new
SingleLinkedList(e);
if(head==null)
// if list is empty add new
node as head
{
head=tempNode;
}
else
{
SingleLinkedList
node=head;
while(node.next!=null) // else add
new node at tail
{
node=node.next;
}
node.next=tempNode;
}
noOfEntries++;
}
public GameEntry remove(int minValueIndex)
{
SingleLinkedList node=head;
if(minValueIndex==0)
// if value to be removed is head, remove
head
{
head=head.next;
}
else
{
SingleLinkedList
prevNode=head; // else remove index
'i' element
node=head.next;
int
index=1;
while(index!=minValueIndex)
{
index++;
prevNode=node;
node=node.next;
}
prevNode.next=node.next;
}
noOfEntries--;
return node.node;
}
public int getMinimumValueIndex()
{
SingleLinkedList
node=head;
int
minValue=Integer.MAX_VALUE;
int index=0,i=0;
while(node!=null)
{
if(node.node.score<minValue)
{
minValue=node.node.score;
index=i;
}
node=node.next;
i++;
}
return index;
}
public int getMinimumValue()
{
SingleLinkedList
node=head;
int
minValue=Integer.MAX_VALUE;
while(node!=null)
{
if(node.node.score<minValue)
{
minValue=node.node.score;
}
node=node.next;
}
return minValue;
}
public void printHighestToLowest()
{
int [] arr=new
int[noOfEntries];
int i=0;
for(SingleLinkedList
node=head;node!=null;node=node.next)
{
arr[i++]=node.node.getScore();
}
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
public int getNumberOfEntries()
{
return noOfEntries;
}
public void printScore()
{
SingleLinkedList
node=head;
while(node!=null)
{
System.out.println(node.node.name+" "+node.node.score);
node=node.next;
}
}
}
class SingleLinkedList
{
GameEntry node;
SingleLinkedList next;
SingleLinkedList(GameEntry node)
{
this.node=node;
}
}
===============================================
Step 1 : Continue unitl user presses -1
Step 2 : Input user choice
Step 3 : If choice <> -1 then Go to Step 4 else exit
Step 4 : Input Score
Step 5 : Input Name
Step 6 : Create GameEntry Object
Step 7 : check if Score <> -1 then go to Step 8 else go to Step 13
Step 8 : check if Number of Game Entries in the list = 10 then go to Step 9 else go to step 12
Step 9 : Get the minimum score of all the Game entries
Step 10 : if the minimum score < Score of Step 4 then go to Step 11 else do nothing
Step 11: remove minimum score game entry rom the list and add new Game Entry created at Step 6 to the list
Step 12 : Add Game entry created at Step 6 to the list. (If the condition at Step 8 is false)
Step 13 : Print all Game Entries
Step !4 : Print Score from Highest To Lowest.
Below is the GameEnry Class that was missing
class GameEntry{
String name;
int score;
public GameEntry(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}