Question: Java. I need to help with Java exception handling. I try to verify all data entries (must be integer ranged from 0 to 100). //operation
Java. I need to help with Java exception handling. I try to verify all data entries (must be integer ranged from 0 to 100).
//operation class
class TestScore2
{
private int score1; //for score 1
private int score2; //for score 2
private int score3; //for score 3
//create parameterized constructor
public TestScore2(int sOne, int sTwo, int sThree)
throws IllegalArgumentException
{
score1 = sOne;
score2 = sTwo;
score3 = sThree;
}
public int validScore1(int sOne)
{
if ( sOne < 0 || sOne > 100 )
throw new IllegalArgumentException
("Score for test"+ sOne + "is out of range"
+ " Number can't be less than 0 and"
+ "and greater than 100");
return sOne;
}
public int validScore2(int sTwo)
{
if (sTwo < 0 || sTwo > 100 )
throw new IllegalArgumentException
("Score for test"+ sTwo + "is out of range"
+ " Number can't be less than 0 and"
+ "and greater than 100");
return sTwo;
}
public int validScore3(int sThree)
{
if (sThree < 0 || sThree > 100 )
throw new IllegalArgumentException
("Score for test"+ sThree + "is out of range"
+ " Number can't be less than 0 and"
+ "and greater than 100");
return sThree;
}
//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
import java.util.Scanner;
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();
try
{
TestScore2 scores = new TestScore2(s1 ,s2, s3);
System.out.println("Average of the test score:" +
scores.getAverage());
System.out.println("Your grade is: " + scores.getLetterGrade());
}
catch(IllegalArgumentException e)
{
System.out.println("You entered a invalid test score");
System.out.println(e.getMessage());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
