Question: Program 5A: Determine which student has the highest grade Write a Java program that determines which student has the highest grade. You will ask the
Program 5A: Determine which student has the highest grade
Write a Java program that determines which student has the highest grade. You will ask the user to enter the number of students. Then you will ask for each student and their grade. You will output the names and grades of the students with the 2 highest grades. You will NOT use an array for this assignment. DO NOT USE ARRAYS.
Use comments liberally to document exactly what you are doing in the program.
Use descriptive variable names in camel-case with the first letter in lowercase.
Ask the user for the number of students and assign that to a variable of type int called numStudents using the nextInt method of Scanner.
At this point, try to compile and run your program. Dont move on until this part is working. It would be good to enter a temporary print statement just to make sure this is working. It should be commented out before turning in the program (put // in front of it) or deleted.
We need variables for highestName and highestScore of types String and int, respectively. Set highestName to the empty string and highestScore to 0.
Create a for loop: for (int i = 0; i < numStudents; i++)
In the loop, add an extra line that reads to the end of the line:
input.nextLine();
This is needed after a nextInt if you are going to then read a String. This basically burns the rest of the line, even though there is nothing there.
Ask the user for name and score, which will be String and int types, respectively, which will require nextLine and nextInt.
Compare score to highestScore. If score is greater than highestScore, then assign highestScore equal to score and highestName equal to name. There will not be an else.
This is the end of the for loop, so put the closing brace.
Outside of the for loop, print highestName and highestScore.
Let your instructor know if you have any trouble with this.
Sample Runs (Enter this data exactly and make screen-prints to paste into a Word document that you will turn in to prove that your program works correctly. On the last one, just hit the Enter key without entering anything.):
Please enter number of students:1
Enter student name:Suzy
Enter score:90
Highest score: Suzy 90
Process finished with exit code 0
Please enter number of students:4
Enter student name:Gus
Enter score:70
Enter student name:Suzy
Enter score:80
Enter student name:Robert
Enter score:90
Enter student name:Allie
Enter score:100
Highest score: Allie 100
Process finished with exit code 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
