Can you implement the Try, Catch, Finally block into this program and explain where and why the
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 {
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; } }
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch