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.
- Using “if..elseif..else” condition, check the score value.
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);
}
}
}
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?
Chapter 30 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
- (True/False): The ReadConsole function reads mouse information from the input bufferarrow_forwardModified Programming ). (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop: • Read one line from the input file and store it in a string; Count the number of vowels and consonants (using either while-loop or for-loop) in the file string. The while-loop will terminate when the end-of-file is reached. After the loop is finished, the program displays the total number of vowels and consonants in the text file. [A text file, named “ass4_Q6_input.txt", is provided as your testing input file.]arrow_forward(True/False): When the source code of a program is amended, it must be reassembled and linked before the updated code may be run.arrow_forward
- (Class Average: Reading Student Records from a CSV File) Use Python Use the csv module to read the grades.csv file from the previous exercise (exercise 9.3). Display the data in tabular format, including an additional column showing each student’s average to the right of that student’s three exam grades and an additional row showing the class average on each exam below that exam’s column. This is exercise 9.3 # Importing csv moduleimport csv# empty list to store datadata = []columns = ["firstname", "lastname", "grade1", "grade2", "grade3"]filename = "grades.csv"for i in range(3):firstname = input("Enter First Name : ")lastname = input("Enter Last Name : ")grade1 = float(input("Enter Grade 1 : "))grade2 = float(input("Enter Grade 2 : "))grade3 = float(input("Enter Grade 3 : "))data.append([firstname, lastname, grade1, grade2, grade3])print()# write data and columns as csv filewith open(filename, 'w') as csvfile:# creating a csv writer objectcsvwriter = csv.writer(csvfile)# writing the…arrow_forward(Write a C++ program) This homework is from our textbook page 459 # 16. The Dodgers recently won the World Series vs the Tampa Bay Rays. Attached are two files: Teams.txt - This file contains a list of serveral Major League baseball teams in alphabetical order. Each team is listed in the file has won the World Series at least once. WorldSeries Winners.txt - This file contains a chronological list of the World Series' winning teams from 1903-2020. (The first line of the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2020. Note the World Series was not played in 1904 or 1994.) Write a program that displays the contents of the Teams.txt file on the screen and prompts the user to enter the name of one of the teams. The program should then display the number of times that team has won the World Series in the time period from 1903 to 2020. In the program, 2 arrays are needed: TeamList array and Winners array. TeamList array will…arrow_forward(Online Address Book revisited) Programming Exercise 5 in Chapter 11 could handle a maximum of only 500 entries. Using linked lists, redo the program to handle as many entries as required. Add the following operations to your program: Add or delete a new entry to the address book. Allow the user to save the data in the address book.arrow_forward
- . (True/False): A segment selector points to an entry in a segment descriptor tablearrow_forward(Duplicate Elimination) Write a program that reads in a series of first names and eliminates duplicates by storing them in a Set. Allow the user to search for a first name. Add a name to set, use end to terminate input: Search a name, use end to terminate searching: Sample output as follows:arrow_forward(Evaluate expression) Modify Listing 20.12, EvaluateExpression.java, to addoperators ^ for exponent and % for remainder. For example, 3 ^ 2 is 9 and 3% 2 is 1. The ^ operator has the highest precedence and the % operator has thesame precedence as the * and / operators. Your program should prompt theuser to enter an expression. Here is a sample run of the program: Enter an expression: (5 * 2 ^ 3 + 2 * 3 % 2) * 4(5 * 2 ^ 3 + 2 * 3 % 2) * 4 = 160 Here is the Listing 20.12 to modify: import java.util.Stack; public class EvaluateExpression { public static void main(String[] args) { // Check number of arguments passed if (args.length != 1) { System.out.println( "Usage: java EvaluateExpression \"expression\""); System.exit(1); } try { System.out.println(evaluateExpression(args[0])); } catch (Exception ex) { System.out.println("Wrong expression: " + args[0]); } } /** Evaluate an expression */ public static int evaluateExpression(String expression) { // Create operandStack to store operands…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education