Add a method adjustScore to the student class to allow for a
Add a method adjustScore to the student class to allow for a single score to be changed by the user. The prototype for the method is as follows:
Note that examNumber will be a value between one and three, as might be entered by a user of the program; do not confuse this with the notion of an exam array index, which would have a value between zero and two.
It should be pointed out here that it is the obligation of any method to maintain the integrity of its object. Does changing a score logically affect any of the other class member data? The answer in this case is yes. Changing a score will definitely change the student\'s average and it could change the letter grade; conversely, there is no interaction between the score and the student\'s name or identification number. The constructor already contains program instructions to determine the average and grade from the scores. Move these instructions to a new method calculateAverageAndGrade which can then be called by both the constructor and by adjustScore.
Observe that nothing is accomplished by the user invoking this method on its own; since the integrity of the object data is maintained by every method, the average and score are already always consistent with the scores. For this reason, this method should be made private; to leave it public suggests a reason for doing so, and there simply is none.
Test the program thoroughly to verify that it will work correctly for any input data.
Solution
CalGrade.java
import java.util.Scanner;
public class CalGrade {
//declaring integer array
private int examNumbers[];
private int examscores[];
//creating the reference of Scanner class Object
Scanner sc=null;
//Default cosntructor
public CalGrade() {
super();
//Creating the arrays
this.examNumbers = new int[3];
this.examscores = new int[3];
/* calling gettingExamScores() method gets the exam number
* and marks in each exam from the user
*/
gettingExamScores();
//Calling the method which will calculate average and grade
calculateAverageAndGrade();
}
/* This gettingExamScores() method gets the exam number
* and marks in each exam from the user
* Params:void
* Return:void
*/
public void gettingExamScores()
{
//Creating the Scanner class object
sc=new Scanner(System.in);
for(int i=0;i<3;i++)
{
//getting the exam number from the user
System.out.print(\"\ Enter Exam number :\");
examNumbers[i]=sc.nextInt();
//Getting the Exam score from the user
System.out.print(\"Enter Exam Score :\");
examscores[i]=sc.nextInt();
}
}
/* this method is used to calculate the average
* of all exams and allocate grade based on that average
* Params:void
* return:void
*/
private void calculateAverageAndGrade ()
{
//Declaring variables
int sum=0;
double average=0.0;
char grade=0;
//This loop is used to calculate the sum of all tests
for(int i=0;i<3;i++)
{
sum+=examscores[i];
}
//Calculating the average
average=sum/3;
//based on the average calculating the grade .
if(average>=90)
grade=\'A\';
else if(average>=80 & average<89)
grade=\'B\';
else if(average>=70 & average<79)
grade=\'C\';
else if(average>=60 & average<69)
grade=\'D\';
else if(average<60)
grade=\'F\';
//Displaying the average of the 3 exams
System.out.println(\"The Average of the 3 Tests is :\"+average);
//Displaying the grade of the student
System.out.println(\"\ The Grade of the Student is :\"+grade);
}
/* This method is is used to modify the exam score
* based on the exam number
* Params:exam number,new score
* Return:void
*/
public void adjustScore (int examNumber, int newScore)
{
/*This for loop will find the exam number and
* modify the old exam score with new exam score
*/
for(int i=0;i<3;i++)
{
if(examNumbers[i]==examNumber)
examscores[i]=newScore;
}
//Calling the method which will calculate average and grade
calculateAverageAndGrade();
}
}
_______________________________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
//Declaring variables
int examnum,newscore;
//Scanner class Object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Creating the calgGrade Object
CalGrade cg=new CalGrade();
System.out.println(\"\ _____Modifying Exam Score ____\");
//Getting the Exam number entered by the user
System.out.print(\"Enter Exam Number :\");
examnum=sc.nextInt();
//getting the New Exam Score entered by the user
System.out.print(\"Enter New Score :\");
newscore=sc.nextInt();
/* calling the adjustScore() method to adjust
* the average and grade by passing the exam number
* and new score as arguments
*/
cg.adjustScore(examnum, newscore);
}
}
________________________________________
Output:
Enter Exam number :123
Enter Exam Score :85
Enter Exam number :234
Enter Exam Score :88
Enter Exam number :345
Enter Exam Score :86
The Average of the 3 Tests is :86.0
The Grade of the Student is :B
_____Modifying Exam Score ____
Enter Exam Number :123
Enter New Score :97
The Average of the 3 Tests is :90.0
The Grade of the Student is :A
___________________________________Thank You



