(JAVA) Write a program called FinalTestScores.

Write a program called FinalTestScores. Ten students have taken three tests. Use an array to store the test scores. The program should ask the user to enter each student’s first name, last name, and his or her three test scores. This program should have functions that return a specific student’s first name, last name, three test scores, average tests score, and a letter grade based on the average.

Test Score Letter Grade

90-100 A

80-89 B

70-79 C

60-69 D

0-59 F

Write a class that uses a String array to hold the ten students’ names, an array of five characters to hold the five students’ letter grades, and three doubles each to hold each student’s set of test scores. Use nested loops, in which the outer loop handles the student names, and the inner loop handles the current student’s test scores. You may find using a single 10 x 3 multi-dimensional array or parallel array easier to manage instead of a separate array for each set of test scores. 

Write the following methods in the program. Print a project description that tells the user what to do.

calTestAverage — This function should accept three test scores as an argument and return the average of the test scores.

letterGrade-This function should accept test scores as an argument and return a letter grade for the test scores, based on the following grading scale.

getName-This function should accept students’ data as an argument and return a student’s name

Although averages are often floating-point values, you should cast the average test score to an integer when comparing with the grading scale. This reduces the possibility of error. Calculate students average test scores and the letter grade.

Demonstrate the class in a program that allows the user to enter each student’s name and his or her three test scores. It should then display as a table format: each student’s name, three test scores, average, and letter grade

Sample Output 

First NameLast NameTest1Test2Test3AverageGrade 
JohnSmith90.590.585.088.67 B 
BrownSam75.087.590.084.17 B
ParksPeter97.088.598.094.50A

The class average is 89.11.

PLEASE ADD COMMENTS (INLINE COMMENTS)

1. Print a descriptive message that tells the user what to do

2. Input validation

3. Method calTestAverage working properly

4. Method letterGrade working properly

5. Method getName working properly

6. Display results working Property

Solution:

import java.util.*;

class FinalTestScores
{
public static double[] calTestAverage(double scores[][])
{
double sum;
double avg[]=new double[10];
for(int i=0;i<=9;i++)
{
sum=0;
for(int j=0;j<=2;j++)
{
sum=sum+scores[i][j];
}
avg[i]=sum/3; //calculating average
}
return avg;
}


public static char[] letterGrade(double avg[])
{
char letter[]=new char[10];

// getting letter grade

for(int i=0;i<=9;i++)
{
if(avg[i]>=90 && avg[i]<100)
{
letter[i]=’A’;
}
else if(avg[i]>=80 && avg[i]<=89)
{
letter[i]=’B’;
}
else if(avg[i]>=70 && avg[i]<=79)
{
letter[i]=’C’;
}
else if(avg[i]>=60 && avg[i]<=69)
{
letter[i]=’D’;
}
else if(avg[i]>=0 && avg[i]<=59)
{
letter[i]=’E’;
}
}
return letter;
}

public static String getName()
{
Scanner sc=new Scanner(System.in);
String name=sc.nextLine(); //accepting first name and last name
return name;
}

public static void main(String args[])
{
String names[]=new String[10];
double scores[][]=new double[10][3];

Scanner sc=new Scanner(System.in);
System.out.println(“\nEnter 10 student names and their 3 test scores: \n”);

for(int i=0;i<=9;i++)
{
System.out.print(“Enter first name and last name separted by a space: “);
names[i]=getName(); // calling getName() to accept name

for(int j=0;j<=2;j++)
{
System.out.print(“Enter test score”+(j+1)+” of “+names[i]+”: “);
scores[i][j]=sc.nextDouble(); //accepting 3 test scores
}
sc.nextLine();
}

double avg[]=new double[10];
avg=calTestAverage(scores); //calling calTestAverage() to get average

char letterGrade[]=new char[10];
letterGrade=letterGrade(avg); //calling letterGrade() to letter grade

System.out.println(“\n”);
System.out.printf(“Name\t\tTest1\tTest2\tTest3\tAverage\tGrade\n”);
for(int i=0;i<=9;i++)
{
System.out.print(names[i]+”\t”); //printing names
for(int j=0;j<=2;j++)
{
System.out.print(scores[i][j]+”\t”); //displaying 3 test scores
}
System.out.printf(“%.2f\t”,avg[i]); //using printf(“%.f”) to get 2 decimal number
System.out.println(letterGrade[i]); //printing grade
}

System.out.print(“\n”);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *