1

So i have been having trouble on how to setup my try catch finally blocks on the program i am writing. I created three classes called MathOP, MathOP2 and TestMathOP. Now I want to add try catch blocks to manipulate error in case the user enters letter(s) as an input and try to divide the number to zero. below are the classes i have created.

MathOP.java

public class MathOP {

    double MathAdd(double num1, double num2) {
        return num1 + num2;
    }

    /**
     * **********************
     * This is the command for adding the first and second number.
     *
     * @param num1
     * @param num2
     * @return 
*************************
     */
    double MathSub(double num1, double num2) {
        return num1 - num2;
    }
}

MathOP2 .java

public class MathOP2 extends MathOP {

    /**
     *
     * @param num1
     * @param num2
     * @return
     */
    double MathMultiply(double num1, double num2) {
        return num1 * num2;
    }

    /**
     * **********************
     * This is the command for multiplying the first and second number.
     *
     * @param num1
     * @param num2
*************************
     */

    double MathDivide(double num1, double num2) {
        return num1 / num2;
    }
}

TestMathOP .java

import java.util.Scanner;

class TestMathOP {

    public static void main(String args[]) {
        MathOP2 MathOP2Object = new MathOP2();

        double firstNum, secondNum, sum;
        char response;
        char operator;
        /**
         * **********************
         * Declare main class and set data types for double and char
         */

        Scanner data = new Scanner(System.in);

        do // do while loop
        {
            try {
                /**
                 * **********************
                 * do while loop starts like this.
                 */
                System.out.print("Week #6 Assignment\n");
                System.out.print("Test math operations for addition,"
                        + "subtraction, multiplication and division\n");

                System.out.print("Enter two numbers!\n");
                System.out.print("Enter the First number >>\r\n");
                firstNum = data.nextDouble();

                /**
                 * **********************
                 * Here states that the user needs to enter two numbers. Asking
                 * the user to enter the first number to add
                 */
                System.out.print("\nEnter the Second number >>\r\n");
                secondNum = data.nextDouble();

                /**
                 * **********************
                 * Here the user needs to enter the second number.
                 */
                System.out.print("Enter operator >> +,-,* or /\r\n");
                operator = data.next().charAt(0);

                /**
                 * **********************
                 * Here the user has the choice which operation he/she wanter to
                 * use.
                 */
                switch (operator) {
                    case '+':
                        sum = MathOP2Object.MathAdd(firstNum, secondNum);
                        System.out.println("The answer is " + sum);
                        break;
                    /**
                     * **********************
                     * This is the command for adding the first and second
                     * number.
                     */
                    case '-':
                        sum = MathOP2Object.MathSub(firstNum, secondNum);
                        System.out.println("The answer is " + sum);
                        break;
                    /**
                     * **********************
                     * This is the command for subtracting the first and second
                     * number.
                     */
                    case '*':
                        sum = MathOP2Object.MathMultiply(firstNum, secondNum);
                        System.out.println("The answer is " + sum);
                        break;
                    /**
                     * **********************
                     * This is the command for multiplying the first and second
                     * number.
                     */
                    case '/':
                        sum = MathOP2Object.MathDivide(firstNum, secondNum);
                        System.out.println("The answer is " + sum);
                        break;
                    /**
                     * **********************
                     * This is the command for dividing the first and second
                     * number.
                     */
                }
            } catch (ArithmeticException e) {
                System.out.println("You can't do that!" + e);
            } catch (Exception e) {
                System.out.println("You cannot enter letter(s)");
            }
            System.out.println("Do you want to exit (Y/N)?");
            response = data.next().charAt(0);
            /**
             * **********************
             * Asking the user to choose Y(yes) or N(no). Yes to exit and no to
             * run the program again.
             */

            if (response == 'N')// This is case sensitive!
            {
                System.out.print("\nRun the program again\r\n\n");
            }
        } while (response != 'Y'); // This is case sensitive!
        System.out.print("\nThanks for using our system");

    }
}

Somehow the program is not working the way i want it to be.

Thank you so much in advance!

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Cyrus
  • 47
  • 6
  • 1
    what is not working in this code? I see it is working because it shown the user a warning if he divide by 0 or enter letters – Jad Chahine Mar 22 '18 at 19:05
  • What specific case is not working? I ran your code, divided by 0, and got an answer of infinity which is correct. – user2023608 Mar 22 '18 at 19:09
  • @ Jad Chahine @ user203608 Well i want that if the user divide the number by zero, it will print out "You can't do that" instead of "The answer is Infinity". The second problem is that when i run the code and enter letter(s), it shows "You cannot enter letter(s)" which is correct. But the part of the code "Do you want to exit (Y/N)?" also runs. – Cyrus Mar 22 '18 at 19:23
  • [See here](https://stackoverflow.com/questions/12954193/why-does-division-by-zero-with-floating-point-or-double-precision-numbers-not) about the division by 0 part of your question. Also, consider validating the input instead of relying on an exception to handle program flow. – Andrew S Mar 22 '18 at 19:33

0 Answers0