Question

In: Computer Science

This is a request for Java High Scores Program with 2 files. Rewrite the high scores...

This is a request for Java High Scores Program with 2 files.

Rewrite the high scores assignment(code below) so that the names and scores are stored in an array of HighScore objects instead of parallel ArrayLists.

The new Program should have the same output as the original. Here is a sample run of the program:

Enter the name for score #1: Suzy

Enter the score for score #1: 600

Enter the name for score #2: Kim

Enter the score for score #2: 9900

Enter the name for score #3: Bob

Enter the score for score #3: 1012

Enter the name for score #4: Armando

Enter the score for score #4: 8000

Enter the name for score #5: Tim

Enter the score for score #5: 514

Top Scorers:

Kim: 9900

Armando: 8000

Bob: 1012

Suzy: 600

Tim: 514

Step 1: The HighScores class

Begin by creating the HighScore class. It should have the following design

Class HighScore

String name

int score

HighScore(String n, int s)

void setName(String n)

String getName()

void setScore(int s)

int getScore()

Step 2: The HighScoresProgram

Create a separate file named HighScoresProgram.java. This file should contain a class that has only four following static methods.

public static void main(String args[])

public static void initialize(HighScores[] scores)

public static void sort(HighScores[] scores)

public static void display(HighScores[] scores)

The main method should allocate an array of five HighScore references, and then invoke the other three methods.

What to Submit?

Submit HighScores.java HighScoresProgram.java

Here is previous code to build from:

import java.util.*;

public class HighScores{
    public static void main(String[] args){

        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> scores = new ArrayList<>();
        initialize(names, scores);
        sort(names, scores);
        System.out.println("\n\nTop Scorers:");
        display(names, scores);
    }
    // user input is done in a method named initialize
    public static void initialize(ArrayList<String> names, ArrayList<Integer> scores){
        int i=1;
        Scanner input=new Scanner(System.in);
        String choice;
        do{
            System.out.print("\nEnter the name for score #" + i + ": ");
            names.add(input.next());
            System.out.print("Enter the score for score #" + i + ": ");
            scores.add(input.nextInt());

            System.out.print("Do you want to add another score? (y/n): ");
            choice = input.next();
            i++;
        }
        while(choice.equalsIgnoreCase("Y"));
    }
    // function that sorts both array lists, based on the values in the scores array list
    public static void sort(ArrayList<String> names, ArrayList<Integer> scores){
        int count = scores.size();
        int tempScore;
        String tempName;
        for (int i = 0; i < count - 1; i++){
            for (int j = i + 1; j <  count; j++){
                if (scores.get(i) < scores.get(j)){
                    tempScore = scores.get(i);
                    scores.set(i, scores.get(j));
                    scores.set(j, tempScore);

                    tempName = names.get(i);
                    names.set(i, names.get(j));
                    names.set(j, tempName);
                }
            }
        }
    }
    // method that displays the contents of the two arraylists
    public static void display(ArrayList<String> names, ArrayList<Integer> scores){

        for (int i = 0; i < scores.size(); i++){
            System.out.println(names.get(i) + " : " + scores.get(i));
        }
    }
}

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class HighScore {
   //instance variables
   private String name;
   private int score;
  
   //constructor
   public HighScore(String name, int score) {
       this.name = name;
       this.score = score;
   }
  
   //all getters and setters
   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;
   }

   @Override//string representation of object
   public String toString() {
       return name+": "+score;
   }

}

public class Test {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.print("How many student you want to enter? ");
       int size = sc.nextInt();//getting the size of the array
       sc.nextLine();

       HighScore[] scores = new HighScore[size];

       initialize(scores, sc);
       sort(scores);
       display(scores);

       sc.close();
   }

   private static void display(HighScore[] scores) {
       System.out.println("Top Scorers: ");
       for(HighScore i: scores)
           System.out.println(i);//displaying the result
   }
  
   //performing sort
   private static void sort(HighScore[] scores) {
       //using comparator for sorting
       Comparator<HighScore> comp = (i, j) -> {//using lamda expression
           return i.getScore() > j.getScore() ? -1 : 1;
       };
      
       Arrays.sort(scores, comp);//sorting the array
   }
  
   //loading the array
   private static void initialize(HighScore[] scores, Scanner sc) {
       for(int i=0; i<scores.length; i++) {
          
           //taking input
           System.out.print("Enter the name for score #"+(i+1)+": ");
           String name = sc.nextLine();
           System.out.print("Enter the score for score #"+(i+1)+": ");
           int score = sc.nextInt(); sc.nextLine();
          
           scores[i] = new HighScore(name, score);//storing it in the array
       }
   }
}

Output


Related Solutions

Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements: The highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements,...
Rewrite your most recent high scores program in C++ so that each name/score pair is stored...
Rewrite your most recent high scores program in C++ so that each name/score pair is stored in a struct named Highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following additional requirements: The Highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements, making the maximum length...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if -...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if - else statement as an equivalent switch statement.   if (letter == 'A' | | letter == 'a')         System.out.println("Excellent");         else if (letter == 'B' | | letter == 'b')                System.out.println("You can do better");         else if (letter == 'C' | | letter == 'c')                System.out.println("Try harder");         else if (letter == 'D' | | letter == 'd')                System.out.println("Try much harder");        ...
Using files, Write a Java program to read 2 tutorials group of student id, assignment mark...
Using files, Write a Java program to read 2 tutorials group of student id, assignment mark (30), lab test mark(20) from 2 input files namely group1.txt and group 2.txt. Your program will calculate the total course work marks and write in to 3 output files with the following conditions: if the total course work marks are more than 40 out 50 write in to LIST1.TXT if the total course work marks are between 30 to 40 then write in to...
This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
Write JAVA program that finds 3 students with the best scores. The program asks users for...
Write JAVA program that finds 3 students with the best scores. The program asks users for scores of 5 students. The program prints the first, second, third place students and scores. You can assume that there is no two students with the same score. <EXAMPLE> enter the score of each student score of student 1: 50 score of student 2: 70 score of student 3: 30 score of student 4: 90 score of student 5: 40 1st place is student...
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
In java, Write a program that reads a data file containing final exam scores for a...
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined. The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT