Question: NEED HELP WITH PART II Part I: Modify your TestScore class in Lab One so it will verify all data entries (No letters and must
NEED HELP WITH PART II
Part I: Modify your TestScore class in Lab One so it will verify all data entries (No letters and must be numbers ranged from 0-100) with exception handling and display the average score and a letter grade. And code also a custom-designed exception class named InvalidTestScore to handle the exception.
Code TestScoreApp as the driver class. Run and test your code to meet the requirements. The code will continue to run until the valid test score is entered.
TestScore2.java
package lab4;
public class TestScore2 {
private int score1; //for score 1
private int score2; //for score 2
private int score3; //for score 3
//create default constructor
public TestScore2()
{
score1 = 0;
score2 = 0;
score3 = 0;
}
//create parameterized constructor
public TestScore2(int sOne, int sTwo, int sThree)
{
score1 = sOne;
score2 = sTwo;
score3 = sThree;
}
//create the set method as a mutator; accept arguments that
//are stored in the score1/2/3
//set an mutator for first score
public void setScore1(int sOne)
{
score1 = sOne;
}
//set an mutator for second score
public void setScore2(int sTwo)
{
score2 = sTwo;
}
//set an mutator for third score
public void setScore3(int sThree)
{
score3 = sThree;
}
//create the getScores method return as accessor
//function
public int getScore1()
{
return score1;
}
public int getScore2()
{
return score2;
}
public int getScore3()
{
return score3;
}
//return average of test score
public double getAverage()
{
return (score1 + score2 + score3) /3;
}
public char getLetterGrade()
{
char grade;
if (getAverage() < 60)
grade = 'F';
else if(getAverage() < 70)
grade = 'D';
else if(getAverage() < 80)
grade = 'C';
else if(getAverage() < 90)
grade = 'B';
else
grade = 'A';
return grade;
}
}
TestScoreApp2.java
package lab4;
import java.util.Scanner;
public class TestScoreApp2 {
public static void main(String[] args) {
int s1, s2, s3;
Scanner keyboard = new Scanner(System.in);
while(true)
{
System.out.print("First score: ");
s1 = keyboard.nextInt();
try {
if(s1<0 || s1>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
while(true)
{
System.out.print("Second score: ");
s2 = keyboard.nextInt();
try {
if(s2<0 || s2>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
while(true)
{
System.out.print("Third score: ");
s3 = keyboard.nextInt();
try {
if(s3<0 || s3>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
TestScore2 scores = new TestScore2(s1, s2, s3);
System.out.println("Your average is: " + scores.getAverage());
System.out.println("Your grade is: " + scores.getLetterGrade());
}
}
InvalidTestScore.java
package lab4;
public class InvalidTestScore extends Exception {
public InvalidTestScore()
{
System.out.println("Invalid.Must between 0-100");
}
}
Part II: Modify the code above so it will use proper GUI components, such as labels, text fields and buttons (Submit, Clear, and Exit) to perform the same tasks completed in Part I. Methods of JOptionPane are not considered as GUI components and cannot be used. Check examples in the text and understand how GUI components are coded. Call your operation class TestScoreGUI and the driver class TestScoreGUIApp.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
