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

Assignment : The first lab assignment is an exercise in the use of the exception

ID: 3909572 • Letter: A

Question

Assignment:

The first lab assignment is an exercise in the use of the exception and handling of exceptions. You will write a main method to test the Triangle class which I have written. Triangle takes doubles as constructor arguments for the A and B sides of a right triangle and calculates the hypotenuse. However it does nothing to handle the situation where a user enters data which is negative, zero or non numeric. Your main should handle these situations.

Input Commands:

            A – enter value for side A

            B – enter value for side B

            C – calculate

            Q – quit the program

The program will repeat itself in order to make it easier to test. You will create a new object of the Triangle class for each test.

Your task today is to write that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Input command, not A, B, C or Q

The program should not terminate but should inform the user of the specific error and should re-request only the input value which was in error

Testing:

      Test your program with the following test cases;

     

A

A

A

A

B

A

B

X

Q

3

3

0

s

s

3

3

B

B

B

C

C

4

0

3

C

C

C

In all cases of invalid input the program should ask for only the input values that were invalid and should continue with the values that were correct. The program should never crash!

Files:

Main.java                                    supplied by you

Triangle.java                  supplied by blackboard

Lab Exercise 01.2

Handle exceptions

Assignment:

The next lab assignment is another exercise in the use of the exception and handling of exceptions. You will write a main method to test the Quadratic class which I have written. Quadratic takes doubles as constructor arguments for the a, b and c coefficients of a quadratic equation and calculates the two values for X. However it does nothing to handle the situation where a user enters data which either includes a value of 0.0 for a or is non numeric. The program should always display two values as the result since a quadratic equation, by definition, always has two values as the solution even when the values are identical.

The program will repeat itself in order to make it easier to test.

However it does nothing to handle the situation where a user enters data which is non-double or even non numeric. Your task today is to develop that program to handle the bad input situation with the use of exception handling. Bad inputs are:

Non-numeric values where numeric values are expected

Any input that causes the output to be “NaN” (Not a Number)

Any input that causes the output to be “INFINITY”

The program will not terminate but will inform the user of the specific error and should re-request the input value which was in error

Testing:

     Test your program with Main.java supplied by blackboard.

The program should never crash!

Files:

Main.java             supplied by blackboard

Quadratic.java       supplied by you

A

A

A

A

B

A

B

X

Q

3

3

0

s

s

3

3

B

B

B

C

C

4

0

3

C

C

C

Explanation / Answer

Below is the solution:

Triangle.java:

public class Triangle {
   double side1, side2, hypotenuse; //variable to set the value of side1 and side2 and
   //contructor
   public Triangle(double side1, double side2) {
       super();
       this.side1 = side1;
       this.side2 = side2;
   }

   //method to calculate the hypotenuse
   public void hypotenuseCalculate(double side12,double side22) {
       hypotenuse = Math.sqrt(Math.pow(side1, 2) + Math.pow(side2, 2));
       System.out.print("Hypotenuse triangle is: "+hypotenuse);
   }

  
}

Main.java:

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
  
       double side1=0,side2=0; //variable to enter two side of triangle
       char choice; //for input choice character
       Scanner in=new Scanner(System.in); //scanner for input

       //validation input choice
       do {
           //Menu for input choice
           System.out.println("");
           System.out.println("A – enter value for side A ");
           System.out.println("B – enter value for side B ");
           System.out.println("C – calculate ");
           System.out.println("Q – quit the program");
           System.out.print("Enter Choice: ");
           //enter choice
           choice =in.next().charAt(0);
          
           //validate input for A,B,C,Q
           if (!Character.toString(choice).matches("^[A-CQ]*$")) {
                 System.out.println("Invalid choice"); //error message
            }
          
           if(choice=='A'){ //if user enter A then enter the side1 value
               System.out.print("Enter a value for Side 1: ");
                side1 = in.nextDouble();
           }
          
           if(choice=='B') { //if user enter B then enter the side2 value
               System.out.print("Enter a value for Side 2: ");
                side2 = in.nextDouble();
           }
          
           if(choice=='Q') { //if user enter q then enter quit the program
               System.out.println("Bye Bye..");
               System.exit(0);
           }
           if(choice=='C') { //if user enter c then calculate the hypotenuse value of triangle
               if(side1 !=0 && side2 !=0) { //check for both side input value
                   Triangle t=new Triangle(side1,side2); //create a object of Triangle class and pass the default contructor parameter value
                   t.hypotenuseCalculate(side1, side2); //call the hypotenuseCalculate method to calculate the value of hypotenuse
                  
               }
               else {
                   System.out.println("Error! Please enter both side value.");
               }
           }
          
       }while( choice !='Q'); //quit the loop when enter Q
      
      
   }
}

sample output:


A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: w
Invalid choice

A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: 2
Invalid choice

A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: A
Enter a value for Side 1: 3

A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: c
Invalid choice

A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: B
Enter a value for Side 2: 4

A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: C
Hypotenuse triangle is: 5.0
A – enter value for side A
B – enter value for side B
C – calculate
Q – quit the program
Enter Choice: Q
Bye Bye..

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