write a program to do a mathematical operation. Make sure the program asks for a
ID: 3635949 • Letter: W
Question
write a program to do a mathematical operation. Make sure the program asks for a user input to define all variables and must include the key words Try, and Catch. The program must function correctly.(ie user defined errors must be fixed)please do the whole program. I'm supposed to do two put I need an example to learn from. Thank you
Explanation / Answer
import java.io.IOException; import java.util.Scanner; public class CalculatorX { static double numAdd1=0,numAdd2=0; static double numSub1=0,numSub2=0,numMul1=0; static double numMul2=0,numDiv1=0,numDiv2=0; static double numSqr1=0,numCube1=0,numPow1=0; static double numPow2=0,numSqrt1=0; static int choice; static String myString; //makes the full user interface at start up public static int UI(Scanner scan)throws Exception { System.out.println(" **************************** Wel Come to Java Language"); System.out.println(" CALCULATOR *****************************"); System.out.println("0: EXIT()"); System.out.println("1: Add two Numbers"); System.out.println("2: Subtract two Numbers"); System.out.println("3: Multiply two Numbers"); System.out.println("4: Divide two Numbers"); System.out.println("5: Square of a number"); System.out.println("6: Cube of a number"); System.out.println("7: Find the SQUARE-ROOT of a Number"); System.out.println("8: Find the X power Y"); choice = scan.nextInt(); return choice; } //Calculate the Addition of two numbers public static double add(double numAdd1, double numAdd2){ return numAdd1 + numAdd2; } //Calculate the subtraction of two numbers public static double sub(double numSub1, double numSub2){ return numSub1 - numSub2; } //Calculate the multiplication of two numbers public static double multiply(double numMul1, double numMul2){ return numMul1 + numMul2; } //Calculate the Division of two numbers public static double divide(double numDiv1, double numDiv2)throws UserException{ if(numDiv2==0) throw new UserException(); return numDiv1 / numDiv2; } //Calculate the Square of a numbers public static double square(double numSqr1){ return numSqr1*numSqr1; } //Calculate the Cube of a numbers public static double cube(double numCube1){ return numCube1 * numCube1 * numCube1; } //Calculate the SQUARE-ROOT of a numbers public static double squareRoot(double numSqrt1){ return Math.sqrt(numSqrt1); } //Calculate the power of numbers public static double power(double numpow1, double numPow2){ return Math.pow(numPow1,numPow2); } //press any key to Goto Main Menu public static void mainMenu(){ System.out.print("Press Enter key....."); try { System.in.read(); } catch(IOException e){ return; } } //Function to check the input validation public static boolean checkInput(String str){ int stringLength = str.length(); if (stringLength>=300){ return false;} return true; } //main function public static void main(String[] args)throws Exception { boolean isValidInput; //Scanner is class desined in java.util package which is designed to scan the stream and tokinize it. //in this we are usinf Console input as Stream.IE: System.in Scanner scan = new Scanner(System.in); choice= UI(scan); while (choice!=0){ switch(choice){ case 0: return; case 1: //Add two numbers code Code //if Input is not a number then Number format exeption will be thrown by the program. try{ System.out.println(" Enter First Number"); String str = scan.next(); numAdd1= Double.parseDouble(str); System.out.println(" Enter second Number"); str = scan.next(); isValidInput = checkInput(str); if(isValidInput==true){ numAdd2 = Double.parseDouble(str); double numAddSum= add(numAdd1,numAdd2); System.out.println(" ********** The Sum is= " + numAddSum + " ********** "); } //We can Catch N number of Exceptions On after another in sequence. like below }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); }catch (Exception e) { System.out.println(" ********** invalid input Operation exits please ********** "); } mainMenu(); UI(scan); break; case 2: //Code //subtract two numbers code Code try{ System.out.println(" Enter First Number"); numSub1= Double.parseDouble(scan.next()); System.out.println(" Enter second Number"); numSub2 = Double.parseDouble(scan.next()); double numSub = sub(numSub1,numSub2); System.out.println(" ********** The Difference is= " + numSub + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); } mainMenu(); UI(scan); break; case 3: //Code //subtract two numbers code Code try{ System.out.println(" Enter First Number"); numMul1= Double.parseDouble(scan.next()); System.out.println(" Enter second Number"); numMul2 = Double.parseDouble(scan.next()); double numMul = multiply(numMul1,numMul2); System.out.println(" ********** The Multiplication is= " + numMul + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits ********** "); } mainMenu(); UI(scan); break; case 4: //Code //Divide two numbers code Code try{ System.out.println(" Enter First Number"); numDiv1= Double.parseDouble(scan.next()); System.out.println(" Enter second Number"); numDiv2 = Double.parseDouble(scan.next()); // if 2nd num numDiv2=0 then the method numDiv2 throws the UserException. double numDiv = divide(numDiv1,numDiv2); System.out.println(" ********** The Division is= " + numDiv + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); }catch (UserException e) { System.out.println(" ********** USER EXCEPTION COUGHT: invalid input Operation exits ********** "); } mainMenu(); UI(scan); break; case 5: //Code //square of a number code try{ System.out.println(" Enter a Number"); numSqr1= Double.parseDouble(scan.next()); double numSqr = square(numSqr1); System.out.println(" ********** The SQUARE is= " + numSqr + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); } mainMenu(); UI(scan); break; case 6: //Code //cube of a number code System.out.println(" Enter a Number"); try{ numCube1= Double.parseDouble(scan.next()); double numCube = cube(numCube1); System.out.println(" ********** The CUBE is= " + numCube + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); } mainMenu(); UI(scan); break; case 7: //Code //square-root of a numbver System.out.println(" Enter a Number"); try{ numSqrt1= Double.parseDouble(scan.next()); double numSqrt = squareRoot(numSqrt1); System.out.println(" ********** The SQUARE-ROOT is= " + numSqrt + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); } mainMenu(); UI(scan); break; case 8: //Code //Divide two numbers code Code try{ System.out.println(" Enter a Number"); numPow1= Double.parseDouble(scan.next()); System.out.println(" Enter a 2nd Number"); numPow2= Double.parseDouble(scan.next()); double numPow = power(numPow1,numPow2); System.out.println(" ********** The " + numPow1 + " power " + numPow2 + " is= " + numPow + " ********** "); }catch (NumberFormatException e) { System.out.println(" ********** invalid input Operation exits please ********** "); }mainMenu(); UI(scan); break; default: UI(scan); break; } } } } class UserException extends Exception{ /** * */ private static final long serialVersionUID = 1L; public UserException(){ } @Override public String getMessage() { return "This is a user created Exception.."+super.getMessage(); } @Override public void printStackTrace() { super.printStackTrace(); } @Override public String toString() { return "USER created Exception:"+super.toString(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.