Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java. I need to help with Java exception handling. I try to verify all data entr

ID: 3816387 • Letter: J

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 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());

   }

   }

}

Explanation / Answer

import java.util.InputMismatchException;
import java.util.Scanner;

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 score1, int score2, int score3) {
       super();
       this.score1 = score1;
       this.score2 = score2;
       this.score3 = score3;
   }

   public static void validateScore(int score) throws IllegalArgumentException

   {
       if (score < 0 || score > 100) {
           throw new IllegalArgumentException("Score " + score
                   + "is out of range" + " Number can't be less than 0 and"
                   + "and greater than 100");
       }

   }

   // return average of test score

   public double getAverage()

   {

       return (score1 + score2 + score3) / 3;

   }

   public String getLetterGrade()

   {
       String 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

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

       try

       {
           System.out.println("What is your first score? ");
           s1 = keyboard.nextInt();
           TestScore2.validateScore(s1);
           // get an user's input for second score
           System.out.println("What is your second score? ");
           s2 = keyboard.nextInt();
           TestScore2.validateScore(s2);
           // get an user's input for third score
           System.out.println("What is your third score? ");
           s3 = keyboard.nextInt();
           TestScore2.validateScore(s3);
           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());
       } catch (InputMismatchException ex) {
           System.out.println("you should enter only Integer Score");
       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote