Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package
bartleby

Concept explainers

Question
Book Icon
Chapter 7, Problem 7.1PE
Expert Solution & Answer
Check Mark
Program Plan Intro

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.
Program Description Answer

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);

}

}

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
07:37
Students have asked these similar questions
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI

Chapter 7 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version Plus MyProgrammingLab with Pearson EText -- Access Card Package

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Some of the characteristics of an ideal adhesive.

Degarmo's Materials And Processes In Manufacturing

The “for” loop is a repetition control structure that is executed for the specified number of times. “for” loop...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Find the area of the surface in units of square meters.

Thinking Like an Engineer: An Active Learning Approach (4th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT