In: Computer Science
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)); } } }
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