In: Computer Science
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 name and grade of the student with the highest grade. You will NOT use an array for this assignment.
Call your class Program5A, so your filename will be Program5A.java. It is essential for grading purposes that everyone have the same class name. 2. Create several lines of comments of identification and description at the top of the file (it doesn’t have to look exactly like this, in particular, if your editor wants to put * in front of every line, that’s fine):
Use comments liberally to document exactly what you are doing in the program. 4. Use descriptive variable names in camel-case with the first letter in lowercase. 5. 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. Don’t move on until this part is working. It would be good to enter a temporary print statement just to make sure this is working: System.out.printf("numStudents = %d\n", numStudents); It should be commented out before turning in the program (put // in front of it) or delete the line.
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. (Do NOT put a semi-colon after the for statement. Put an open curly brace instead, and go ahead and put the closing brace if your IDE doesn’t do it automatically, then put all the statement to be repeated BETWEEN the curly braces):
for (int i = 0; i < numStudents; i++) { a. As the first statement 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 (it ignores everything else to and including the end of the line which in this case will just be an end-of-line character '\n'), even though there is nothing there that you can see. You will need to do this any time that you have read in an int as you did for the number of students. (You could have done it right after you read the number of students, but then you would have to do it again in the loop right after reading the score. Doing it here takes care of both with one line of code.) b. Ask the user for name and score, which will be String and int types, respectively, which will require nextLine and nextInt. c. 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. (If they are equal, we are NOT going to change the highest score so that in the event of a tie, the first one wins. However, in real life we would deal with ties in a better manner.) d. This is the end of the for loop, so put the closing brace if it’s not already there.
Outside of the for loop, print highestName and highestScore
ample 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; it should sit there still waiting for integer input; if you then enter an integer, everything is fine and works like normal.):
Please enter number of students:4
Enter student name:Gus
Enter score:70
Enter student name:Suzy
Enter score:80
Enter student name:Allie
Enter score:100
Enter student name:Robert
Enter score:90
Highest score: Allie 100
Process finished with exit code 0
/*********************************Program5A.java****************************/
import java.util.Scanner;
public class Program5A {
public static void main(String[] args) {
//scanner class for reading
input
Scanner scan = new
Scanner(System.in);
int highestScore = 0;//variable for
highest score
String highestName = "";//variable
for highest scorer
System.out.print("Please enter
number of students: ");//prompt for number of student
int numStudents =
scan.nextInt();//read number of student
scan.nextLine();//skip the buffer
after integer or scan.next
/*
* for loop till the number of
students
*/
for (int i = 1; i <=
numStudents; i++) {
System.out.print("Enter Student name: ");//prompt for name
String name =
scan.nextLine();//read name
System.out.print("Enter Score: ");//prompt for score
int score =
scan.nextInt();//read score
scan.nextLine();//clear buffer
/*
* find highest
Score
*/
if (highestScore
< score) {
highestScore = score;//save highest Score
highestName = name;//save highest scorer
}
}
System.out.println("Highest
Score: " + highestName + " " + highestScore);//print detail
scan.close();
}
}
/*********************output********************/
Please enter number of students: 4
Enter Student name: Gus
Enter Score: 70
Enter Student name: Suzy
Enter Score: 80
Enter Student name: Allie
Enter Score: 100
Enter Student name: Robert
Enter Score: 90
Highest Score: Allie 100
Please let me know if you have any doubt or modify the answer, Thanks:)