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

Assignment : The assignment is an exercise in the use of the exception and handl

ID: 3857899 • Letter: A

Question

Assignment:

The assignment is an exercise in the use of the exception and handling of exceptions. There is a simple calculation program listed below, which takes integers as input 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 non-integer or even non numeric.

Input Commands:

          A – enter value for side A

          B – enter value for side B

          C – calculate

          Q – quit the program

The program should repeat itself if the user wishes; this makes it easier to test. Values for A and B will be cleared to 0 (zero) after each calculation.

Your task today is to modify 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

Numeric values where non-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      X     X     3     3

B     B     B                     C     C

4      0      3

C     C     C

In all cases 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!

Explanation / Answer

import java.util.Scanner;

public class Assignment {

//method to check wHether a valid command is entered

static boolean checkCommand(String command)

{

boolean check = false;

String[] strings = {"A","B","C","Q"};

for(int i=0;i<strings.length;i++)

{

//checking whether the entered command is from expected charecters

if(strings[i].equals(command))

{

check = true;

}

}

return check;

}

//Getting the value of hypotenuse

static double hypotenuse(int A, int B)

{

double squareRoot = 0;

A = A*A;

B= B*B;

int c = A+B;

squareRoot = Math.sqrt(c);

return squareRoot;

}

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

boolean quit =false;

int A = 0;

int B = 0;

//this loop runs untill Q commond is given

//as we are setting quit = true in the loop if(command.equals("Q"))

while(quit == false)

{

boolean check = false;

boolean checkA = false;

boolean checkB = false;

System.out.println("Enter a command");

String command = sc.nextLine();

//passing the entered string to checkcommand method to check valid string is entered

check = checkCommand(command);

while(check == false)

{

System.out.println("Enter a valid Command");

command = sc.nextLine();

check = checkCommand(command);

}

//loop for the command A

if(command.equals("A"))

{

while(checkA==false)

{

try{

System.out.println("enter value for side A");

//If anything other than integers are given an exception is thrown

A = Integer.parseInt(sc.nextLine());

System.out.println(A+"A");

checkA = true;

}catch (Exception e) {

System.out.println("Enter a Valid number");

}

}

}

//loop for the command B

if(command.equals("B"))

{

while(checkB==false)

{

try{

System.out.println("enter value for side B");

//If anything other than integers are given an exception is thrown

B = Integer.parseInt(sc.nextLine());

checkB = true;

}catch (Exception e) {

System.out.println("Enter a Valid number");

}

}

}

if(command.equals("C"))

{

//passing A and B to get hypotenuse value

double hypo = hypotenuse(A, B);

System.out.println("Hypotenuse : "+hypo);

//setting A and B as 0 since they hold the previous loop values inside them

A = 0;

B = 0;

}

if(command.equals("Q"))

{

quit=true;

System.out.println("Exit successful");

}

}

}

}

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