Introduction to Java Programming and Data Structures  Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134700144
Author: Liang
Publisher: PEARSON
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
(String-Terminating Null Character) Write a program to show that the getline and three-argument get istream member functions both end the input string with a stringterminating null character. Also, show that get leaves the delimiter character on the input stream, whereas getline extracts the delimiter character and discards it. What happens to the unread characters in the stream?
(True/False): A link library is added to a program just before producing an Executable file.
(True/False): A link library is included in a program right before it generates an executable file.

Chapter 30 Solutions

Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education