Concept explainers
(Assign grades) Write a
Grade is A if score is ≥ best −10;
Grade is B if score is ≥ best −20;
Grade is C if score is ≥ best −30;
Grade is D if score is ≥ best − 40;
Grade is F otherwise.
The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades. Here is a sample run:
Assign Grades
Program Plan:
- Import required packages.
- Declare the main class method “Sample1”.
- In the main method.
- Create an object “input” for the scanner class.
- Get the number of students from the user and store it in a variable “numberOfStudents”.
- Create an object “scores” for the static method “double”.
- Read the corresponding student scores and find the best score.
- Declare and initialize the output string.
- Print the student score and grade.
- The grades are assigned based on the following scheme.
- Grade is A if score is >= best-10;
- Grade is B if score is >= best -20;
- Grade is C if score is >= best -30;
- Grade is D if score is >= best - 40;
- Grade is F otherwise.
- In the main method.
The below program reads the total number of students and their scores and then display the grade of each student.
Explanation of Solution
Program:
//Import required packages
import java.io.*;
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//Class name
public class sample1 {
// Main Method
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Get the number of students
System.out.print("Enter the number of students: ");
int numberOfStudents = input.nextInt();
//Create an object for the static method
double[] scores = new double[numberOfStudents]; // Array scores
double best = 0; // The best score
// Read scores and find the best score
System.out.print("Enter " + numberOfStudents + " scores: ");
for (int i = 0; i < scores.length; i++) {
scores[i] = input.nextDouble();
if (scores[i] > best)
best = scores[i];
}
// Declare and initialize output string
char grade; // The grade
// Assign and display grades
for (int i = 0; i < scores.length; i++) {
if (scores[i] >= best - 10)
grade = 'A';
else if (scores[i] >= best - 20)
grade = 'B';
else if (scores[i] >= best - 30)
grade = 'C';
else if (scores[i] >= best - 40)
grade = 'D';
else
grade = 'F';
//Print the output
System.out.println("Student " + i + " score is " + scores[i]+ " and grade is " + grade);
}
}
}
Enter the number of students: 5
Enter 5 scores: 78 92 69 45 56
Student 0 score is 78.0 and grade is B
Student 1 score is 92.0 and grade is A
Student 2 score is 69.0 and grade is C
Student 3 score is 45.0 and grade is F
Student 4 score is 56.0 and grade is D
Want to see more full solutions like this?
Chapter 7 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Experiencing MIS
Absolute Java (6th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Introduction to Programming Using Visual Basic (10th Edition)
C++ How to Program (10th Edition)
Starting Out with Java: Early Objects (6th Edition)
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,