CSci 2101 Data Structures: Lab 9

Problem 1: smallest elements in a 2D array

Write a program that initializes a two-dimensional array of integers and prints out the following: Assume that the array doesn't have any missing rows and all rows are of the same length. You may use the in-class code to get you started.

Problem 2: a checkered pattern

An array is a "checkered pattern" if every element a[i][j] is even if i + j is even, and is odd otherwise. For instance, the following array is a checkered pattern, assuming that a[0][0] is at the left upper corner:

4 3 2 7 0
9 2 5 6 3
The following array is not:

3 4 
9 2 
Write a method

public static boolean isCheckered(int [][] arr)
which returns true if the array is a checkered pattern and false otherwise. Again, you may assume that the array is a normal n-by-m matrix, without any missing elements or other anomalities.

Problem 3: Catching Java exceptions

Question 1 Run the following program, explain what happens and why.

public class CatchExceptions {
    public static void main(String [] args) {
	int [] a = {3, 1, 0, 4, 6};
	int [] b = {2, 0, 4, 8};

	for (int i = 0; i < a.length; ++i) {
	    try {
		System.out.println(a[i]/b[i]);
	    } catch (Exception  e) {
		System.out.println(e);
	    }
	}
	System.out.println("Done");
    }

}
Question 2 Suppose we put the try/catch block around the entire loop instead of just the print statement (see below). What would happen? Run the program and explain the change in the program's behavior.

public class CatchExceptions {
    public static void main(String [] args) {
	int [] a = {3, 1, 0, 4, 6};
	int [] b = {2, 0, 4, 8};

	try {
	    for (int i = 0; i < a.length; ++i) {
		System.out.println(a[i]/b[i]);
	    }
	} catch (Exception  e) {
	    System.out.println(e);
	}
	System.out.println("Done");
    }

}
Question 3 Go back to the version of this program given in Question 1 and change the statement catch (Exception e) to catch (ArithmeticException e). What do you think will happen? Run the program, explain the results.

Question 4 Now change the same statement to catch (IndexOutOfBoundsException e). Explain the results.

Question 5 What happens if I replace System.out.println(e); by each of the following:

Check out API for class Exception for information about each of these methods. Note that Exception inherits these methods from the class Throwable.

Problem 4: Throwing Java exceptions

Question 1 Run the following program. What happens? Does the program ever reach the end?

public class ThrowExceptions {
    public static void main(String [] args) throws Exception {
	int [] a = {3, 2, 1, 4, 5};

	for (int i = 0; i < a.length; ++i) {
	   System.out.println(a[i]);
	   if (a[i] % 2 == 0) { 
	       throw new Exception("I hate even numbers");
	   }
	}
	System.out.println("Done");
    }

}
Question 2 What happens if you remove throws Exception from the declaration of main?

Question 3 Consider the following variation of the same program:


public class ThrowExceptions {
    public static void main(String [] args) {
	int [] a = {3, 2, 1, 4, 5};

	for (int i = 0; i < a.length; ++i) {
	   System.out.println(a[i]);
	   try {
	       if (a[i] % 2 == 0) { 
		   throw new Exception("I hate even numbers");
	       }
	   } catch (Exception e) {
	       System.out.print("Hey, I caught an exception! ");
	       System.out.println("It says: " + e.getMessage());
	   }
	}
	System.out.println("Done");
    }

}
What will happen if you run it? Explain the results.

Question 4Now change the catch block to:


public class ThrowExceptions {
    public static void main(String [] args) throws Exception {
	int [] a = {3, 2, 1, 4, 5};

	for (int i = 0; i < a.length; ++i) {
	   System.out.println(a[i]);
	   try {
	       if (a[i] % 2 == 0) { 
		   throw new Exception("I hate even numbers");
	       }
	   } catch (Exception e) {
	       System.out.print("Hey, I caught an exception! ");
	       System.out.println("It says: " + e.getMessage());
	       if (i > 2) throw e;
	   }
	}
	System.out.println("Done");
    }

}
Explain the results.

Problem 5: Writing your own exceptions

Question 1 In a new file IHateEvenNumbersException.java write a class IHateEvenNumbersException. The class must extend the class Exception. Write a constructor that calls the constructor of the superclass with one parameter: a string "I hate even numbers!!".

Replace the line
throw new Exception("I hate even numbers"); with the line throw new IHateEvenNumbersException(); Test the program.

Question 2 Change the line catch (Exception e) to catch only IHateEvenNumbersException. Test your program to make sure that other kinds of exceptions go uncaught (you need to write code that gives some exceptions, such as ArithmeticException or NullPointerException).

Problem 6: Using exceptions to signal errors and continue

The following program compiles but throws a run-time exception:

public class Typecasting {
    public static void main(String [] args) {
	Object [] stuff = {"apples", new StringBuffer("oranges"),
			   "pineapples", new StringBuffer("grapes"),
			   "kiwi"};
	Object [] morestuff = new Object[stuff.length];
	for (int i = 0; i < stuff.length; ++i) {
	    String s = (String) stuff[i];
	    morestuff[i] = s;
	}
    }
}
Rewrite the program so that it prints out an error message for each improperly typecasted object, but continues execution. Print out the array morestuff after the loop. Explain the results.
This is a lab from CSci 2101 course.