IC211 Spring AY 2020

HW Exceptions

Name (Last, First): ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________

  1. [20pts] Consider the following program:

    HW13.java
    import java.util.*;
    
    public class HW13 {
      public static void main(String[] args) {
        boolean verbose = args.length > 0 && args[0].equals("-v");
        Scanner sc      = new Scanner(System.in);
    
        if (verbose) {
          System.out.print("Enter x, k, m: ");
        }
        int x = sc.nextInt();
        int k = sc.nextInt();
        int m = sc.nextInt();
        int r = MyMath.modexp(x, k, m);
    
        if (verbose) {
          System.out.print(x + "^" + k + " % " + m + " = ");
        }
        System.out.println(r);
      }
    }
    
    MyMath.java
    public class MyMath {
      /**
       * Computes x^k mod m.
       * Note: k must be non-negative, and m must be positive
       * @param x the base
       * @param k the exponent
       * @param m the modulus
       * @return x^k mod m
       */
      public static int modexp(int x, int k, int m) {
        int r = 1;
    
        for (int i = 0; i < k; i++) {
          r = r * x % m;
        }
        return r;
      }
    }
    

    Note that this program runs by default in a "quiet" mode, where only the result gets printed, but with the -v option it runs in a "verbose" mode in which instructions and an explanation of the output get printed. Here are some runs of the program:
    ~/$ java HW13 -v
    Enter x, k, m: 2 3 5
    2^3 % 5 = 3
    
                      
    ~/$ java HW13
    2 3 5
    3
    Several things can go wrong here. Run the program for each of the following inputs (exactly as shown here!) and summarize what gets printed/happens, and explain why.
    1. 2 4 11
    2. 2 3 0
    3. 2 five 17
    4. 2,5,17
    5. 2 -3 5
  2. [60pts] Use the Java exception handling mechanism to add error handling to this program. Specifically: In non-verbose mode, when there is an error absolutely nothing should get printed out; while in verbose mode, no matter what the error, you should print out: Error in HW13! invalid input.

    Note: Check the documentation for Scanner's nextInt() method to see what it throws and when.

    Note on the rules: You may not change what arguments modexp takes, nor make it non-static, nor add new static fields to either class.

    Note on the point: The point here is to use throw and try-catch to do all this!
  3. [20pts] The article "Exception Handling in C++", Bjarne Strouptrup identifies the four different "traditional" ways to handle errors which are listed below. For each approach, explain specifically why it wouldn't work to provide the error handling required in Problem 2.
    1. Terminate the program.
    2. Return a value representing 'error'
    3. Return a legal value and leave the program in an illegal state.

      This is obviously a disaster, so you don't need to write anything!
    4. Call a function supplied to be called in case of 'error'. [e.g. make a call like handleError("k < 0 in modexp!").]
Turn in: this sheet, a screen capture showing your solution to Problem 2 running on the five example inputs from Problem 1 (in both quiet mode and vebose mode), and a codeprint of your source code.
Don't forget to add proper Javadoc formatted comments in the source code!