Java High Scores with OOP High scores program for reference
Java High Scores with OOP
?-------------------------?-------------------------?-------------------------?-------------------------?-------------------------
High scores program for reference -
The data must be stored in two ArrayLists: one ArrayList of strings named names, and one ArrayList of Integers named scores. These ArrayLists must be declared in the main method.
All of the user input should be done in a method named initializeArrays. It should have the following signature:
public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name “Kim” should also be moved to the top of the names array list. The function should have the following signature:
public static void sort(ArrayList<String> names, ArrayList<Integer> scores)
Finally you should write a method that displays the contents of the two arraylists. It should have the following signature:
public static void display(ArrayList<String> names, ArrayList<Integer> scores)
The main method should be very short. It should just declare and initialize the two arraylists and then invoke these three methods.
Solution
HighScoreTest.java
import java.util.Scanner;
public class HighScoreTest {
public static void main(String[] args) {
HighScore scores[]= new HighScore[5];
//reading names and scores
initialize(scores);
//sorting the lists based on score.
sort(scores);
//displaying the contents of both arrayist
display(scores);
}
public static void initialize( HighScore scores[]){
Scanner scan = new Scanner(System.in);
for(int i=1; i<=5; i++){
System.out.println(\"Enter the name for score #\"+i+\":\");
String name = scan.next();
System.out.println(\"Enter the score for score #\"+i+\":\");
int score = scan.nextInt();
HighScore h = new HighScore(name, score);
scores[i-1] = h;
}
}
public static void sort(HighScore scores[]){
int n = scores.length;
String tempStr = \"\";
HighScore temp = null;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
//checking max score and sorting based on it.
if(scores[j-1].getScore() < scores[j].getScore()){
//swap the elements!
//swapping scores
temp = scores[j-1];
scores[j-1] = scores[j];
scores[j] = temp;
}
}
}
}
public static void display(HighScore scores[]){
System.out.println(\"Top Scorers:\");
//printing the elements up to the size of both arrays.
for(HighScore score: scores){
System.out.println(score.getName()+\": \"+score.getScore());
}
}
}
HighScore.java
public class HighScore {
private String name;
private int score;
public HighScore(String name, int score){
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;
}
}
Output:
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


