Question: Can you implement the Try, Catch, Finally block into this program and explain where and why the block was implemented? import java.util.Scanner; class TestMathOP2 {

Can you implement the Try, Catch, Finally block into this program and explain where and why the block was implemented?

import java.util.Scanner;

class TestMathOP2 {

public static void main(String anrg[]) {

//CHANGE datatype to double double firstNum, secondNum, sum; char response; char operator;

Scanner data = new Scanner(System.in);

/*********************** Purpose: Creating Object to recieve inputs from user ************************/ do // do while loop { /******************* Purpose: Beginning of do-while loop **********************/ System.out.println(" ---Week #6 Assignment --- "); System.out.println(" + Add, Subtract -, * Multiply, / Divide ");

System.out.println("Enter two numbers! ");

System.out.println("Enter first number>>"); firstNum = data.nextDouble(); /*********************** Asking the user for the first number they'd like to add *********************************/ System.out.println(" Enter second number>>"); secondNum = data.nextDouble();

// 2nd number for user input// System.out.println("Enter +, -, *, / operator>>"); operator = data.next().charAt(0); //Added and if else for the division and multiplication operators

if (operator == '+') { sum = MathAdd(firstNum, secondNum); //CHANGE : Using String.format to print output with 2 decimal places System.out.println("The answer is =" + String.format("%.2f",sum)); } else if (operator == '-') { sum = MathSub(firstNum, secondNum); //CHANGE : Using String.format to print output with 2 decimal places System.out.println("Thw answer is =" + String.format("%.2f",sum)); } else if (operator == '/') { //CHANGE : Validation for second number input //as number can not be divide by 0 if (secondNum == 0) { System.out.println("Number can not be divided by 0, Try again!!!"); response = 'Y'; } else { double div = MathDiv(firstNum, secondNum); //CHANGE : Using String.format to print output with 2 decimal places System.out.println("Thw answer is =" + String.format("%.2f", div)); } } else if (operator == '*') { sum = MathMul(firstNum, secondNum); //CHANGE : Using String.format to print output with 2 decimal places System.out.println("Thw answer is =" + String.format("%.2f",sum)); } else { System.out.println(" Sorry, you did not select either the +, -, *, / operator"); }

System.out.println(" Do you want another operation (Y/N) ?"); response = data.next().charAt(0); if (response == 'N') { System.out.println(" Thanks for using our system"); } else { System.out.println(" Lets do this again!"); continue; }

} while (response != 'N'); //do while loop }

//CHANGE : argument datatypes from int to double //return type from int to double public static double MathAdd(double num1, double num2) { double add; add = num1 + num2; return add; } // end MathAdd method

//CHANGE : argument datatypes from int to double //return type from int to double public static double MathSub(double num1, double num2) { double sub; sub = num1 - num2; return sub; }

//CHANGE : argument datatypes from int to double //return type from int to double //added methodes for dividing and multiplying public static double MathDiv(double num1, double num2) { double div; div = num1 / num2; return div; }

//CHANGE : argument datatypes from int to double //return type from int to double public static double MathMul(double num1, double num2) { double mul; mul = num1 * num2; return mul; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement a trycatchfinally block in the given program we need to identify where exceptions might occur The most common place for exceptions in thi... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!