In: Computer Science
I asked this question before, but I'm wondering if there are any other ways to do this problem. My exam will be similar to this, so I'm looking for more examples to study with. Thank you!
For this Java program, you will write two classes: GradeCalculator and GradeCalculatorDriver.
In the GradeCalculator class, compute the final average and letter grade for a particular student.
The final average is calculated according to the following rules:
1) There are ten exams scored out of 100 points
2) The lowest exam score is not included in the calculation of the final average
3) The highest exam score is not included in the calculation of the final average
4) An average of 91-100 is an A
5) An average of 81-90 is a B
6) An average of 71-80 is a C
7) An average of 61-70 is a D
8) An average of 0-60 is an E
The GradeCalculator class must include the following methods:
1) A method to compute and return the highest score
2) A method to compute and return the lowest score
3) A method to compute the sum of all the scores
4) A method to compute the final average
5) A method to determine the letter grade
In the GradeCalculatorDriver class, include a main method that asks the user for the student’s name and their ten exam grades, reads the grades, calculates the letter grade and prints it. This process should be repeated until the user enters N.
For example, a run of the program might look something like this:
Welcome to Score Calculator
Want to compute a final average? Y
Please enter the student’s name: Sara Smith
Please enter test scores all on one line separated by one or more spaces.
The final score for Sara Smith is 100.
The letter grade for Sara Smith is A.
Want to compute another final average? N
Paste both classes in same package
Code:-
GradeCalculator class
public class GradeCalculator {
public double highest_score(double arr[])
{
/*
* to compute highest score
*/
double max=arr[0];
for(int
i=0;i<arr.length;i++)
{
if(max<arr[i])
{
max=arr[i];
}
}
return max;
}
public double lowest_score(double arr[])
{
/*
* to compute lowest score
*/
double min=arr[0];
for(int
i=0;i<arr.length;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
return min;
}
public double sum(double arr[])
{
/*
* to compute sum of scores
*/
double sum=0;
for(int
i=0;i<arr.length;i++)
{
sum=sum+arr[i];
}
return sum;
}
public double final_average(double arr[])
{
/*
* to compute final average by
subtracting lowest and highest score from sum
* and finding the average
*/
double average=0;
average=(sum(arr)-highest_score(arr)-lowest_score(arr))/(arr.length-2);
return average;
}
public char letter_grade(double final_score)
{
/*
* computing the grade based on the
final score
*/
if(0<=final_score &&
final_score<=60)
{
return
'E';
}
else if(61<=final_score
&& final_score<=70)
{
return
'D';
}
else if(71<=final_score
&& final_score<=80)
{
return
'C';
}
else if(81<=final_score
&& final_score<=90)
{
return
'B';
}
else if(91<=final_score
&& final_score<=100)
{
return
'A';
}
return 0;
}
}
GradeCalculator class Snippet:


GradeCalculatorDriver class
import java.util.Scanner;
public class GradeCalculatorDriver {
public static void main(String[] args) {
char ans,grade;
String name="";
int final_score;
GradeCalculator obj=new GradeCalculator();
//Gradecalculator object
double arr[]=new double[10];
System.out.println("Welcome to Score
Calculator");
System.out.print("Want to compute a final
average?");
Scanner sc=new Scanner(System.in);
ans=sc.next().charAt(0);
sc.nextLine();
if(ans=='N')
{
System.exit(0);
}else
{ do
{
System.out.print("Please enter the student’s name:");
name=sc.nextLine();
System.out.println("Please enter test scores all on one line
separated by one or more spaces.");
for(int
i=0;i<10;i++)
{
arr[i]=sc.nextDouble();
}
final_score=(int) obj.final_average(arr);
/*
* calling method
final score from GradeCalculator class to
* compute
finalscore
*/
System.out.println("The final score for "+name+" is
"+final_score);
grade=obj.letter_grade(final_score);
/*
* pass
finalscore to find out grade
*/
System.out.println("The final grade for "+name+" is "+grade);
System.out.print("Want to compute another final average?");
ans=sc.next().charAt(0);
sc.nextLine();
}while(ans=='Y' || ans=='y');
}}}
GradeCalculatorDriver class Snippet:

OUTPUT
