First part For this first part start with the code in Listin
First part
For this first part, start with the code in Listing 7.5, page 265 (see below), but don\'t use the method printMax (that won\'t help you with the assignment requirement.)
Create a program that reads game scores, and then assigns these scores into the leaderboard (if they make it into the leaderboard): \"First place\" if score is best, then \"Second,\" \"Third,\" and so on. The program prompts the user to enter the total number of gamers, then prompts the user to enter all of the scores, and concludes by displaying the grades. Here is a sample (DO NOT USE THE SAME SCORES) run:
Enter the number of scores you\'d like to enter: 4
Then enter four scores: 40 55 70 58
Gamer 0 score is 40 and grade is \"Fourth.\"
Gamer 1 score is 55 and grade is \"Third.\"
Gamer 2 score is 70 and grade is \"First.\"
Gamer 3 score is 58 and grade is \"Second\".
LISTING 7.5 VarArgsDemo.java
1 public class VarArgsDemo {
2 public static void main(String[] args) {
3 printMax(34, 3, 3, 2, 56.5);
4 printMax(new double[]{1, 2, 3});
5 }
6
7 public static void printMax(double... numbers) {
8 if (numbers.length == 0) {
9 System.out.println(\"No argument passed\");
10 return;
11 }
12
13 double result = numbers[0];
14
15 for (int i = 1; i < numbers.length; i++)
16 if (numbers[i] > result)
17 result = numbers[i];
18
19 System.out.println(\"The max value is \" + result);
20 }
21 }
Solution
//import statements
import java.util.Scanner;
import java.io.BufferedReader;
import java.util.ArrayList;
public class MaxScore{
//main method
public static void main(String[] args) {
//output to enter scores
System.out.print(\"Enter the number of scores you\'d like to enter: \");
//taking in input
Scanner sc = new Scanner(System.in);
int numScores = sc.nextInt();
//creating an array to store scores
int scores[] = new int[numScores];
System.out.println(\"\ Enter the scores: \");
//taking in scores for all the number of scores
for (int i=0; i<numScores; i++){
scores[i] = sc.nextInt();
}
//creating a copy of the scores array
int scoresOriginal[] = scores.clone();
//creating an array to store ranks
int ranks[] = new int[numScores];
//for loop to sort the array
for (int j=0; j<numScores; j++){
int max = -1;
int max_index = -1;
for (int i=0; i<numScores; i++){
if (scores[i]>max){
max = scores[i];
max_index = i;
}
}
ranks[max_index] = j;
scores[max_index] = -1;
}
//output the ranks of scores
for (int i=0; i<numScores; i++){
System.out.println(\"Gamer \"+i+\" score is \"+scoresOriginal[i]+\"and grade is \"+(ranks[i]+1));
}
}
}

