JAVA Handle exceptions Assignment : The first lab assignment is an exercise in t
ID: 3868405 • Letter: J
Question
JAVA
Handle exceptions
Assignment:
The first lab 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!
Handle exceptions
Assignment:
The first lab 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.InputMismatchException;
import java.util.Scanner;
public class calculate {
static float a=0,b=0;
double cal()
{
if(a==0||b==0)
{
System.out.println("no values found in a or b");
start();
}
double x=(a*a)+(b*b);
double h=Math.sqrt(x);
a=0;
b=0;
return h;
}
float enter()
{
float val=0;
try
{
System.out.println("Enter side");
Scanner sc1 = new Scanner(System.in);
val = sc1.nextFloat();
return val;
}
catch(InputMismatchException e)
{
System.out.println("Enter correct value");
}
return val;
}
void start()
{
calculate c=new calculate();
while(true)
{
System.out.println("Enter Command");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
switch(input)
{
case "A":
a=c.enter();
break;
case "B":
b=c.enter();
break;
case "C":
double res=c.cal();
System.out.println("Hypotenuse is : "+res);
break;
case "Q":
System.exit(0);
default:System.out.println("wrong command");
}
}
}
public static void main(String[] args) {
calculate c=new calculate();
c.start();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.