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

Lab # 8 Exception Handling CSCI 1302 (Math tutor) Write a program that displays

ID: 3737173 • Letter: L

Question

Lab # 8 Exception Handling CSCI 1302 (Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition subtraction, multiplication, or division test. After a test is finished the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1-number2, number1 is greater than or equal to number2. For a division question such as numberl / number2, number2 is not zero. I have enclose the program below and also in D2L, what you are required to do is to include exception handler that deals with nonnumeric operands, ArithemeticException and any others you think is required by this program.

Explanation / Answer

PROGRAM

import java.util.Scanner;
import java.util.Random;

//Create class TestExceptin of Arithmatic operations

class TestException
{

public static int menu()
{
Scanner scr=new Scanner(System.in); // Declare Scanner Object for Reading Choice
//Display Menu
System.out.println(" ARITHEMATIC OPERATIONS ");

System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Quit");
System.out.print(" Enter Your Choice: ");
int ch=scr.nextInt(); // Reading choice
return ch; // return choice
}

public static void main(String args[])
{
Random rnd=new Random(); // Create Random object rnd
try{ // try block for illegal choice
while(true)
{
int ch=menu(); // call menu()
switch(ch) // create switch statement
{
case 1: int a=rnd.nextInt(9); // read "a" random integer number from 0 to 9
System.out.print("A Value: "+a);
int b=rnd.nextInt(9); // read "b" random integer number from 0 to 9
System.out.print(" B Value: "+b);
  
System.out.println(" Addition of "+a+" and "+b+" is: "+(a+b)); // Display Random integer numbers sum
break;
case 2:int x=rnd.nextInt(9); // read "x" random integer number from 0 to 9
System.out.print("A Value: "+x);
int y=rnd.nextInt(9); // read "y" random integer number from 0 to 9
System.out.print(" B Value: "+y);
int s1; // Declare variable s1 for result subtraction
if(x>=y){ // check condition x >=y
s1=x-y; // calculate x-y
System.out.println(" Subtraction of "+x+" and "+y+" is: "+s1); // Display subtraction value
} else
System.out.println(" Not Possible "+x+" < "+y); // display error message
break;
case 3: int x1=rnd.nextInt(9); // read "x1" random integer number from 0 to 9
System.out.print("A Value: "+x1);
int y1=rnd.nextInt(9); // read "y1" random integer number from 0 to 9
System.out.print(" B Value: "+y1);
System.out.println(" Multiplication of "+x1+" and "+y1+" is: "+(x1*y1)); // Display multiplication value
break;
case 4:
int x2=rnd.nextInt(9); // read "x2" random integer number from 0 to 9
System.out.print("A Value: "+x2);
int y2=rnd.nextInt(9); // read "y2" random integer number from 0 to 9
System.out.print(" B Value: "+y2);
int res; // declare integer variable res
try{ // declare try block
res=x2/y2; // calculate division
System.out.println(" Division of "+x2+" and "+y2+" is : "+res); // display division value
}catch(ArithmeticException e){ // else catch exception
System.out.println(" Division by Zero"); // and display division error exception
}
break;
case 5: System.exit(0); // Quit from this program
break;
default: System.out.println("You Enter Wrong Choice..Try Again"); // Display Wrong choice
}
}
}catch(Exception e){ // Catch Exception for choice
System.out.println("Illegal Choice: "); // Display Illegal choice and terminate this program
}
}
}

OUTPUT-1


F:>javac TestException.java

F:>java TestException

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 1
A Value: 1
B Value: 1
Addition of 1 and 1 is: 2

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 2
A Value: 5
B Value: 4
Subtraction of 5 and 4 is: 1

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 3
A Value: 1
B Value: 7
Multiplication of 1 and 7 is: 7

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 4
A Value: 0
B Value: 8
Division of 0 and 8 is : 0

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 5

OUTPUT-2


F:>java TestException

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 1
A Value: 7
B Value: 6
Addition of 7 and 6 is: 13

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 2
A Value: 1
B Value: 4
Not Possible 1 < 4

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 3
A Value: 0
B Value: 0
Multiplication of 0 and 0 is: 0

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: 4
A Value: 5
B Value: 0
Division by Zero

ARITHEMATIC OPERATIONS

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit


Enter Your Choice: DD
Illegal Choice: