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
Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
- I need help with this: (Address book) Write a program that stores, retrieves, adds, and updates addresses as shown in Figure 17.20. Use a fixed-length string for storing each attribute in the address. Use random access file for reading and writing an address. Assume that the size of name, street, city, and zip is 32, 32, 20, 2, 5 chars, respectively.arrow_forward(True/False): A link library is included in a program right before it generates an executable file.arrow_forwardneed help: (Address book) Write a program that stores, retrieves, adds, and updates addresses as shown in Figure 17.20. Use a fixed-length string for storing each attribute in the address. Use random access file for reading and writing an address. Assume that the size of name, street, city, and zip is 32, 32, 20, 2, 5 chars, respectivelyarrow_forward
- (True/False): The ReadConsole function reads mouse information from the input bufferarrow_forward(True/False): A segment descriptor contains the base location of a segment.arrow_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
- (java programming language) Write a program to read and store four student’s name and their CGPA, sort them into an order name, based on CGPA from the smallest to the largest, then print them out. The student’s name and their CGPA values should be typed in by the user in response to a prompt message. Save the file as SortName_yourID.javaarrow_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(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
- (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. (True/False): A segment selector points to an entry in a segment descriptor tablearrow_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
- 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