A teacher has 6 students who have taken 3 tests. The teacher uses the following
ID: 3811828 • Letter: A
Question
A teacher has 6 students who have taken 3 tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her 3 test scores. Create a class called Grades (java file called Grades.java) with the (exact) following fields and methods (these names and caps exactly): Demonstrate the class in a program/project called [YourName]-Assignment7 (replace [YourName] with your actual name) in the same project as the Grades.java. You will need to add the class Grades to the project and add your code to the project class main method. The program should allow the user to enter each student's name and his or her 3 test scores and store them in a Grades object. The program should use the values from the Grades object to display in a table format each student's name, average test score, and letter grade (one row per student), and at the end output the class average and the top score (using the class methods).Explanation / Answer
Grades.java
import java.util.ArrayList;
import java.util.Scanner;
public class Grades {
//Creating an ArrayList
ArrayList<String> Names=new ArrayList<String>();
//Creating Array which store letter Grades
char LetterGrades[]=new char[6];
//Creating Arrays which stores each student test scores
double Test1Scores[]=new double[6];
double Test2Scores[]=new double[6];
double Test3Scores[]=new double[6];
/* This method will get the data entered
* by the user and populate that data into an array
* */
public void enterData()
{
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
double score;
for(int i=0;i<6;i++)
{
System.out.print("Enter Name of Student#"+(i+1)+":");
Names.add(sc.nextLine());
while(true)
{
System.out.print("Enter Test1 Score :");
score=sc.nextDouble();
if(score<0 || score>100)
{
System.out.println("Invalid.Must be bwteen 0-100");
continue;
}
else
{
Test1Scores[i]=score;
break;
}
}
while(true)
{
System.out.print("Enter Test2 Score :");
score=sc.nextDouble();
if(score<0 || score>100)
{
System.out.println("Invalid.Must be bwteen 0-100");
continue;
}
else
{
Test2Scores[i]=score;
break;
}
}
while(true)
{
System.out.print("Enter Test3 Score :");
score=sc.nextDouble();
if(score<0 || score>100)
{
System.out.println("Invalid.Must be bwteen 0-100");
continue;
}
else
{
Test3Scores[i]=score;
break;
}
}
sc.nextLine();
}
}
//This method will display all student names
public void getNames()
{
for(String str:Names)
System.out.println(str);
}
/* This method will display the name
* of the Student based on the student number
* */
public void getName(int studentNo)
{
if(studentNo<Names.size())
System.out.println(Names.get(studentNo));
else
System.out.println("Invalid");
}
//This method will display the test scores of particular student
public void getTestScores(int studentNo)
{
System.out.println("Test#1 Score :"+Test1Scores[studentNo-1]);
System.out.println("Test#2 Score :"+Test2Scores[studentNo-1]);
System.out.println("Test#3 Score :"+Test3Scores[studentNo-1]);
}
/* This method will display the test scores
* of particular student in particular test
* */
public void getTestScore(int studentNo,int targetTest)
{
switch(targetTest)
{
case 1:{
System.out.println("Test#1 Score :"+Test1Scores[studentNo-1]);
break;
}
case 2:{
System.out.println("Test#2 Score :"+Test2Scores[studentNo-1]);
break;
}
case 3:{
System.out.println("Test#3 Score :"+Test3Scores[studentNo-1]);
break;
}
}
}
//This method will display the average test score of particular student
public void getAverageScore(int studentNo)
{
double sum=0.0,avearge=0.0;
sum+=Test1Scores[studentNo-1];
sum+=Test2Scores[studentNo-1];
sum+=Test3Scores[studentNo-1];
avearge=sum/3;
System.out.printf("The Average 3 Test's Score is : %.2f ",avearge);
}
//This method will display the letter grades of all the students
public void letterGrades()
{
double sum=0.0,avearge=0.0;
for(int i=0;i<6;i++)
{
sum+=Test1Scores[i];
sum+=Test2Scores[i];
sum+=Test3Scores[i];
avearge=sum/3;
if(avearge>=90 && avearge<=100)
LetterGrades[i]='A';
else if(avearge>=80 && avearge<=89)
LetterGrades[i]='B';
else if(avearge>=70 && avearge<=79)
LetterGrades[i]='C';
else if(avearge>=60 && avearge<=69)
LetterGrades[i]='D';
else if(avearge>=0 && avearge<=59)
LetterGrades[i]='F';
sum=0;
}
for(int i=0;i<LetterGrades.length;i++)
{
System.out.println("Student#"+(i+1)+":"+" Letter Grade ="+LetterGrades[i]);
}
}
//This method will display the letter grades of particular student
public void letterGrade(int studentNo)
{
System.out.println("Grade of Student"+studentNo+" is :"+LetterGrades[studentNo-1]);
}
//This method will display the class test score average
public void getClassAverage()
{
double sum=0,avearge=0.0,totAvg=0.0;
for(int i=0;i<6;i++)
{
sum+=Test1Scores[i];
sum+=Test2Scores[i];
sum+=Test3Scores[i];
}
totAvg=sum/18;
System.out.printf("The Average of 6 student's Test Score's is :%.2f ",totAvg);
}
//This method will display the top grade of a student
public void getTopGrade()
{
double avgArr[]=new double[6];
double sum=0.0,avearge=0.0;
double max;
int index=0;
for(int i=0;i<6;i++)
{
sum=0.0;
sum+=Test1Scores[i];
sum+=Test2Scores[i];
sum+=Test3Scores[i];
avearge=sum/3;
avgArr[i]=avearge;
}
max=avgArr[0];
for(int i=0;i<6;i++)
{
if(max<avgArr[i])
{
max=avgArr[i];
index=i;
}
}
System.out.printf("Student %d got the Highest Average Test score :%.2f ",(index+1),max);
}
}
__________________
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
//Declaring variables
String name;
int score;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Creating the Grades class object
Grades g=new Grades();
g.enterData();
//Displaying the Names of all Students
System.out.println("Displayingh the names of all students :");
g.getNames();
//Displaying al students letter grades
System.out.println();
g.letterGrades();
//Displaying class average
System.out.println("The Class Average is :");
g.getClassAverage();
//Displaying the top grade of the student
g.getTopGrade();
int studentNo;
System.out.println("Getting the Average Score of the student :");
while(true)
{
System.out.print("Enter the Student number between 1-6 :");
studentNo=sc.nextInt();
if(studentNo<1|| studentNo>6)
{
System.out.println("Invalid.Must be between 1-6");
continue;
}
else
break;
}
g.getAverageScore(studentNo);
System.out.println("Getting the Letter grade of the Student :");
while(true)
{
System.out.print("Enter the Student number between 1-6 :");
studentNo=sc.nextInt();
if(studentNo<1|| studentNo>6)
{
System.out.println("Invalid.Must be between 1-6");
continue;
}
else
break;
}
g.letterGrade(studentNo);
System.out.println("Getting the Test Scores of a Student In particular Test :");
while(true)
{
System.out.print("Enter the Student number between 1-6 :");
studentNo=sc.nextInt();
if(studentNo<1|| studentNo>6)
{
System.out.println("Invalid.Must be between 1-6");
continue;
}
else
break;
}
int targetTest;
while(true)
{
System.out.print("Enter the Target Test :");
targetTest=sc.nextInt();
if(targetTest<1 ||targetTest>3)
{
System.out.println("Invalid.Must be between 1-3");
continue;
}
else
break;
}
g.getTestScore(studentNo, targetTest);
}
}
____________________
Output:
Enter Name of Student#1:Sachin Tendulkar
Enter Test1 Score :89
Enter Test2 Score :98
Enter Test3 Score :87
Enter Name of Student#2:Johnson
Enter Test1 Score :77
Enter Test2 Score :88
Enter Test3 Score :86
Enter Name of Student#3:Billings
Enter Test1 Score :76
Enter Test2 Score :66
Enter Test3 Score :56
Enter Name of Student#4:Robes
Enter Test1 Score :66
Enter Test2 Score :89
Enter Test3 Score :74
Enter Name of Student#5:Kenny
Enter Test1 Score :82
Enter Test2 Score :83
Enter Test3 Score :84
Enter Name of Student#6:James
Enter Test1 Score :67
Enter Test2 Score :82
Enter Test3 Score :91
Displayingh the names of all students :
Sachin Tendulkar
Johnson
Billings
Robes
Kenny
James
Student#1: Letter Grade =A
Student#2: Letter Grade =B
Student#3: Letter Grade =D
Student#4: Letter Grade =C
Student#5: Letter Grade =B
Student#6: Letter Grade =B
The Class Average is :
The Average of 6 student's Test Score's is :80.06
Student 1 got the Highest Average Test score :91.33
Getting the Average Score of the student :
Enter the Student number between 1-6 :6
The Average 3 Test's Score is : 80.00
Getting the Letter grade of the Student :
Enter the Student number between 1-6 :5
The Letter grade of Students#5 is :B
Getting the Test Scores of a Student In particular Test :
Enter the Student number between 1-6 :5
Enter the Target Test :2
Test#2 Score :83.0
_____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.