Design and implement a Java program for programming exercise 7.1, page 276 (name
ID: 3681149 • Letter: D
Question
Design and implement a Java program for programming exercise 7.1, page 276 (name it AssigningGrades), to computer students grades as described in the problem statement. Use an array to store the entered grades. Follow the instructions in the problem statement (and the sample run given in the textbook) to design your program. Design the main method of your program such that it allows the user to re-run the program with different sets of inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class AssigningGrades {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = null;
try {
// create scanner to readd grades
sc = new Scanner(System.in);
// prompt to user
System.out.print("Enter number of students:");
char grade;
// reading no of students
int numberOfStudents = sc.nextInt();
// create scores array with size numberOfStudents
int[] scores = new int[numberOfStudents];
System.out.println("Enter " + numberOfStudents + " scores: ");
// reading scores
for (int i = 0; i < scores.length; i++) {
scores[i] = sc.nextInt();
}
// calculating scores
for (int i = 0; i < scores.length; i++) {
if (scores[i] > 90 && scores[i] <= 100) {
grade = 'A';
} else if (scores[i] > 80 && scores[i] <= 90) {
grade = 'B';
} else if (scores[i] > 70 && scores[i] <= 80) {
grade = 'C';
} else if (scores[i] > 60 && scores[i] <= 70) {
grade = 'D';
} else {
grade = 'E';
}
// printing
System.out.println("Student " + (i + 1) + " score is "
+ scores[i] + " and grade is " + grade);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
sc.close();
}
}
}
OUTPUT:
Enter number of students:5
Enter 5 scores:
87
96
54
82
75
Student 1 score is 87 and grade is B
Student 2 score is 96 and grade is A
Student 3 score is 54 and grade is E
Student 4 score is 82 and grade is B
Student 5 score is 75 and grade is C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.