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

3 JAVA Programming Exercise 12.01 - Name the class and source file: Exercise12_0

ID: 3831158 • Letter: 3

Question

3 JAVA Programming

Exercise 12.01 - Name the class and source file: Exercise12_01a.java and Exercise12_01b.java

(NumberFormatException) Listing 7.9 Calculator.java, is a simple command-line calculator. Note that the program terminates if any operand is nonnumeric. Write a program with an exception handler that deals with nonnumeric operands; then write another program without using an exception handler to achieve the same objective. Your program should display a message that informs the user of the wrong operand type before exiting (see figure 12.12)

Command Prompt c exercise java Exercise 12-01 4 5 4 5 c exercise jaua Exercise 12-01 4 5 c exercise java Exercise 12-01 4x 5 wrong Input 4x c exercise

Explanation / Answer

Below is the code with and without exception handling: -

Exercise12_01a – With Exception Handling

Exercise12_01b – Without Exception Handling

Exercise12_01a.java

public class Exercise12_01a {

/** Main method */

   public static void main(String[] args) {

  

   // Check number of strings passed

      if (args.length != 3) {

         System.out.println(

            "Usage: java Calculator operand1 operator operand2");

         System.exit(0);

      }

     

   // The result of the operation

      int op1 = 0;

      int op2 = 0;

     

      try {

         op1 = Integer.parseInt(args[0]);

         op2 = Integer.parseInt(args[2]);

      }

         catch(NumberFormatException e) {

            System.out.println("One of the operands is not a valid integer");

            System.exit(0);

         }

        

   // The result of the operation

      int result = 0;

   // Determine the operator

      switch (args[1].charAt(0)) {

     

       case '+': result = op1 + op2;

            break;

           

         case '-': result = op1 - op2;

            break;

           

         case '*': result = op1 * op2;

            break;

           

         case '/': result = op1 / op2;

                    break;

      }

  

   // Display result

      System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]

         + " = " + result);

   }

}

Sample Run: -

D:projectworkspaceMLcalculin>java Exercise12_01a 4 "*" 5

4 * 5 = 20

D:projectworkspaceMLcalculin>java Exercise12_01a 4 + 5

4 + 5 = 9

D:projectworkspaceMLcalculin>java Exercise12_01a 4 + A

One of the operands is not a valid integer

D:projectworkspaceMLcalculin>

Exercise12_01b.java

public class Exercise12_01b {

/** Main method */

   public static void main(String[] args) {

  

   // Check number of strings passed

      if (args.length != 3) {

         System.out.println(

            "Usage: java Calculator operand1 operator operand2");

         System.exit(0);

      }

     

   // The result of the operation

      int op1 = 0;

      int op2 = 0;

     

    

     

      if(isNumeric(args[0]) && isNumeric(args[2])) {

         op1 = Integer.parseInt(args[0]);

          op2 = Integer.parseInt(args[2]);

      } else {

         System.out.println("One of the operands is not a valid integer");

          System.exit(0);

      }

        

   // The result of the operation

      int result = 0;

   // Determine the operator

      switch (args[1].charAt(0)) {

     

         case '+': result = op1 + op2;

            break;

           

         case '-': result = op1 - op2;

            break;

           

         case '*': result = op1 * op2;

            break;

           

         case '/': result = op1 / op2;

                    break;

      }

  

   // Display result

      System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]

         + " = " + result);

   }

  

   public static boolean isNumeric(String str) {

          // split in a char array

              char[] num = str.toCharArray();

              // scan this array of char

              for(int i = 0; i < num.length; i++) {

                     // if we encounter a num return true

                     if(!Character.isDigit(num[i]))

                        return false;

              }

              // num not found so return false

              return true;

   }

}

Sample Run: -

D:projectworkspaceMLcalculin>java Exercise12_01b 4 + A

One of the operands is not a valid integer

D:projectworkspaceMLcalculin>java Exercise12_01b 4 + 8

4 + 8 = 12

D:projectworkspaceMLcalculin>java Exercise12_01b 4 - 8

4 - 8 = -4

D:projectworkspaceMLcalculin>java Exercise12_01b 4 "*" 8

4 * 8 = 32

D:projectworkspaceMLcalculin>java Exercise12_01b 4 / 8

4 / 8 = 0

D:projectworkspaceMLcalculin>