Java. Here is a question below. Part I: Modify your TestScore class in Lab One s
ID: 3815649 • Letter: J
Question
Java. Here is a question below.
Part I: Modify your TestScore class in Lab One so it will verify all data entries (must be integer ranged from 0-100) with exception handling and display the average score and a letter grade. Code 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.
Save all of the source code and compiled code in a folder called Exception
//operation class
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;
}
}
// drive class below
public class TestScoreApp2
{
public static void main(String[] args)
{
//variable declaration
int s1, s2, s3;
//Create a Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//get an user's input for first score
System.out.println("What is your first score? ");
s1 = keyboard.nextInt();
//get an user's input for second score
System.out.println("What is your second score? ");
s2 = keyboard.nextInt();
//get an user's input for third score
System.out.println("What is your third score? ");
s3 = keyboard.nextInt();
//create instance to class
TestScore2 scores = new TestScore2(s1 ,s2, s3);
//output average of test scores
System.out.println("Your average is: " + scores.getAverage());
//output the final grade
System.out.println("Your grade is: " + scores.getLetterGrade());
}
}
Explanation / Answer
TestScore2.java
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;
}
}
__________________
InvalidTestScore.java
public class InvalidTestScore extends Exception {
public InvalidTestScore()
{
System.out.println("Invalid.Must between 0-100");
}
}
__________________
TestScoreApp2.java
import java.util.Scanner;
//drive class below
public class TestScoreApp2 {
public static void main(String[] args) {
// variable declaration
int s1, s2, s3;
// Create a Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
while(true)
{
// get an user's input for first score
System.out.print(" What is your first score? ");
s1 = keyboard.nextInt();
try {
if(s1<0 || s1>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
while(true)
{
// get an user's input for second score
System.out.print(" What is your second score? ");
s2 = keyboard.nextInt();
try {
if(s2<0 || s2>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
while(true)
{
// get an user's input for third score
System.out.print(" What is your third score? ");
s3 = keyboard.nextInt();
try {
if(s3<0 || s3>100)
throw new InvalidTestScore();
else
break;
} catch (Exception e) {
continue;
}
}
// create instance to class
TestScore2 scores = new TestScore2(s1, s2, s3);
// output average of test scores
System.out.println("Your average is: " + scores.getAverage());
// output the final grade
System.out.println("Your grade is: " + scores.getLetterGrade());
}
}
__________________
Output:
What is your first score? 120
Invalid.Must between 0-100
What is your first score? -50
Invalid.Must between 0-100
What is your first score? 89
What is your second score? 150
Invalid.Must between 0-100
What is your second score? 95
What is your third score? 87
Your average is: 90.0
Your grade is: A
_____________Thank You
Please rate me well.If you area satisfied.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.