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
MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version
Additional Engineering Textbook Solutions
Degarmo's Materials And Processes In Manufacturing
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Electric Circuits. (11th Edition)
Mechanics of Materials (10th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
- answer should avoid using AI (such as ChatGPT), do not any answer directly copied from AI would and explain codearrow_forwardWrite a c++ program that will count from 1 to 10 by 1. The default output should be: 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10 There should be only a newline after the last number. Each number except the last should be followed by a comma and a space. To make your program more functional, you should parse command line arguments and change behavior based on their values. Argument Parameter Action -f, --first yes, an integer Change place you start counting -l, --last yes, an integer Change place you end counting -s, --skip optional, an integer, 1 if not specified Change the amount you add to the counter each iteration -h, —help none Print a help message including these instructions. -j, --joke none Tell a number based joke. So, if your program is called counter, counter -f 10 --last 4 --skip 2 should produce 10, 8, 6, 4 Please use the last supplied argument. If your code is called counter, counter -f 4 -f 5 -f 6 should count from 6. You should…arrow_forwardshow workarrow_forward
- show workarrow_forwardshow work on paperarrow_forwardWhat should the next three steps be in my machine based home security system after deployment and after the following current steps: Enhancing Security & Privacy Measures User Interface (UI) and Experience (UX) Improvement Machine Learning Model Refinement & Continuous Improvementarrow_forward
- I am creating a machine learning home based security system, have completed initial deployment and in the following phases of the project: Expanding device compatibility and integration, preparing for cloud integration, and implementing system reduncancy and disaster recovery. What should the next three phases be?arrow_forwardHands-On Assignments Part II Assignment 1-5: Querying the DoGood Donor Database Review the DoGood Donor data by writing and running SQL statements to perform the following tasks: 1. List each donor who has made a pledge and indicated a single lump sum payment. Include first name, last name, pledge date, and pledge amount. 2. List each donor who has made a pledge and indicated monthly payments over one year. Include first name, last name, pledge date, and pledge amount. Also, display the monthly payment amount. (Equal monthly payments are made for all pledges paid in monthly payments.) 3. Display an unduplicated list of projects (ID and name) that have pledges committed. Don't display all projects defined; list only those that have pledges assigned. 4. Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges. 5. Display all pledges made before March 8, 2012. Include all column data from the DD PLEDGE table.arrow_forwardWrite a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…arrow_forward
- 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,
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT