Question
Book Icon
Chapter 30, Problem 30.1PE
Program Plan Intro

Assign grades

Program Plan:

  • Import necessary packages into program.
  • Define the class named “Exercise30_01”.
    • Define main method.
    • Define the “Scanner” object “obj” for input.
    • Prompt the user and get the number of students “N” from user.
    • Declare the array variable “marks[]” in type of “double”.
    • Prompt and get scores using “for” loop.
    • Using “DoubleStream” class assign maximum value into “best” variable.
    • Declare the “grade” in type of “character”.
    • Using “for” loop which is execute from “0” to “marks.length”.
      • Using “if..elseif..else” condition, check the score value.
        • If the value greater than “best-10”, assign “A” to “grade”.
        • If the value greater than “best-20”, assign “B” to “grade”.
        • If the value greater than “best-30”, assign “C” to “grade”.
        • If the value greater than “best-40”, assign “D” to “grade”.
        • Otherwise, assign “E” to “grade”.
        • Print appropriate grade with statement on screen.

Expert Solution & Answer
Check Mark
Program Description Answer

The following JAVA code is to calculate the grade of student scores using “DoubleStream”.

Explanation of Solution

Program:

/*Include necessary packages*/

import java.util.Scanner;

//Include Stream package

import java.util.stream.DoubleStream;

//Class definition

class Exercise30_01

{

//Main method

public static void main(String[] args)

{

/*Definition of "Scanner" object*/

Scanner obj = new Scanner(System.in);

/*Prompt the user for number of students*/

System.out.print("Enter number of students: ");

//Get input from user

int N = obj.nextInt();

/*Declaration and definition of array variable*/

double[] marks = new double[N];

//Prompt the user for marks

System.out.print("Enter " + N + " scores: ");

//Loop

for (int i = 0; i < marks.length; i++)

{

/*Get scores and store into "marks[]" variable*/

marks[i] = obj.nextDouble();

}

/*Get maximum value using stream*/

double best = DoubleStream.of(marks).max().getAsDouble();

//Declaration of variable

char grade;

//Loop

for (int i = 0; i < marks.length; i++)

{

/*Condition to check marks*/

if (marks[i] >= best - 10)

//Assign grade

grade = 'A';

/*Condition to check marks*/

else if (marks[i] >= best - 20)

//Assign grade

grade = 'B';

/*Condition to check marks*/

else if (marks[i] >= best - 30)

//Assign grade

grade = 'C';

/*Condition to check marks*/

else if (marks[i] >= best - 40)

//Assign grade

grade = 'D';

//Else statement

else

//Assign grade

grade = 'F';

//Print statement

System.out.println("Student " + i + " score is " +marks[i] + " and grade is " + grade);

}

}

}

Sample Output

Enter number of students: 4

Enter 4 scores: 40 55 70 58

Student 0 score is 40.0 and grade is C

Student 1 score is 55.0 and grade is B

Student 2 score is 70.0 and grade is A

Student 3 score is 58.0 and grade is B

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!
Students have asked these similar questions
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

Chapter 30 Solutions

MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version

Knowledge Booster
Background pattern image
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
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage