Its Java program class and using arrays and methods Concepts: Methods and one di
ID: 3889885 • Letter: I
Question
Its Java program class and using arrays and methods
Concepts: Methods and one dimensional Arrays
Write a Java Class that reads students grades and assigns grades based on the following grading “curve”:
Grade is A if the score is greater than or equal to the highest score – 10.
Grade is B if the score is greater than or equal to the highest score – 20.
Grade is C if the score is greater than or equal to the highest score – 30.
Grade is D if the score is greater than or equal to the highest score – 40.
Grade is F otherwise.
The program will do the following:
Prompt the user for the number of grades to be entered. The grades will all be integers.
Read in the grades and store them in an array of the correct size.
Call the method below (which you will create) to calculate and return the highest value in the Array. public static int findMax(int[] theArray) Call the method below (which you will create) to print a line for each grade in the format displayed in the sample output: public static void printGrades (int[] theArray, int highestGrade)
Sample output:
Enter the number of grades
4
Enter 4 grades separated by a space
40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
Explanation / Answer
Create a file named app.java and paste given code into it! filename must be app.java
app.java
import java.util.Scanner;
public class app {
public static int findMax(int[] array)
{
int max = Integer.MIN_VALUE;
for(int i = 0; i<array.length; i++)
{
max = Math.max(max,array[i]);
}
return max;
}
public static void printGrades (int[] array, int max)
{
for(int i = 0; i< array.length;i++)
{
if(array[i] >= max)
{
System.out.println("Student " + i + " score is " + array[i] + " and grade is A");
}
else if(array[i] >= max - 20 )
{
System.out.println("Student " + i + " score is " + array[i] + " and grade is B");
}
else if(array[i] >= max - 30)
{
System.out.println("Student " + i + " score is " + array[i] + " and grade is C");
}
else if(array[i] >= max - 40)
{
System.out.println("Student " + i + " score is " + array[i] + " and grade is D");
}
else
{
System.out.println("Student " + i + " score is " + array[i] + " and grade is F");
}
}
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of students in the class!" );
String tmp;
tmp = scanner.nextLine();
int n_students = Integer.valueOf(tmp);
int array[]=new int[n_students];
System.out.println("Enter " + n_students + " grades separated by a space");
String line = scanner.nextLine();
String marks[] = line.split(" ");
System.out.println(line);
for(int i = 0; i<n_students;i++)
{
array[i] = Integer.valueOf(marks[i]);
}
int max = findMax(array);
printGrades(array,max);
}
}
Sample Output:
Enter number of students in the class!
4
Enter 4 grades separated by a space
40 55 70 58
40 55 70 58
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.