Question

In: Computer Science

Convert into pseudo-code for below code =============================================== class Main {    public static void main(String args[])...

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;
   }
}

===============================================

Solutions

Expert Solution

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;
   }
}


Related Solutions

Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT