CSCD 226                                                                   SOLUTION

Exam 2, Spring 2005                                                    100 points possible

High Score: 101, Low Score: 60, Average: 82.6

## 2D Array Section

1. (20 points) Write the swapDiagonals method.  It swaps the contents of the upper-left to lower-right diagonal with the lower-left to upper-right diagonal of a two-dimensional array.   The array has the same number of rows as columns - guaranteed.  The array type is Comparable.  Don’t forget to fill in the parameter list to the method.
void swapDiagonals(Comparable [][] array)
{

Comparable temp;
int n;

for (n = 0; n < array.length; n++)
{
temp = array[n][n];
array[n][n] = array[n][array[n].length – 1 – n];
array[n][array[n].length – 1 – n] = temp;
}

}

## Exception Section

(5 points each) Explain each of the following (what its significance is/ what it is used for/ when it should be used).  A sentence or two should suffice in describing each.  Citing examples as part of your explanation is a good way illustrate your understanding (and thus ensure full credit!)

1. catch
A clause that follows a try block and is used to handle an exception of some type.  The catch clause will only execute if an exception is thrown and it is designated to catch that exception.  Once a catch clause executes, normal program execution continues at the next regular line of code following all the catch clauses.

2. throws
Used at the end of a method header to propagate an exception.  It is used when you don’t want to handle an exception in the current method.  It places the onus on the calling method to handle the exception.

3. Exception
One of the base classes in the Java’s exception handling hierarchy.  It is useful to derive your own exception class.  It provides some basic error reporting mechanisms through methods such as printStackTrace() and getMessage().

1. finally
A clause that follows a try block (and any catches) that will execute regardless of whether an exception is thrown.  It is a way to guarantee that no matter what happens with program execution, the code (in the finally clause) will execute.

Inheritance section

Use the code that follows for questions 6 and 7.  Implement these problems so that the program output shown below is produced.

public class Exam2

{

public static void main(String [] args)

{

int i;

Shape [] shapes = new Shape;

shapes = new Circle(0, 0, 2.5);//center at 0,0, radius of 2.5

shapes = new Point(1, 1);

shapes = new Circle(1, 1, 1.0);//center at 1,1, radius is 1.0

for (i = 0; i < shapes.length; i++)

System.out.println(shapes[i]);

}//end main method

}//end class Exam2

/***************** PROGRAM OUTPUT: *******************************

CIRCLE: [0, 0] is center, radius is 2.5

POINT: [1, 1]

CIRCLE: [1, 1] is center, radius is 1.0

******************************************************************/

public abstract class Shape

{

private String name;

public Shape(String name)
{
this.name=name;
}

public String toString()

{

return this.name + ":";

}//end toString method

}//end Shape class

1. (15 points) Write the Point class given the above code.  It should be derived from Shape, have a constructor which legally sets the name to “POINT”, and have a toString() method which calls the base toString() method.  You may include other methods, but none are expected.

public class Point extends Shape
{
private int x, y;

public Point(int x, int y)
{
super(“POINT”);
this.x = x;
this.y = y;
}

//for use by Circle
public Point(String name, int x, int y)
{
super(name);
this.x = x;
this.y = y;
}

public String toString()
{
return super.toString() + “[“ + this.x + “, “ + this.y + “]”;
}
}//end class Point

2. (15 points) Write the Circle class given the above code.  It should be derived from Point, have a constructor which calls the base constructor, and have a toString() method which calls the base toString() method – note what is printed in the output when toString() is called on a Circle object (be sure you represent everything necessary in the Circle class.  You may include other methods, but none are expected.  Don’t forget to legally set the name of the shape to “CIRCLE”.  Use the back of this page for your code if you don’t have enough room on the front.

public class Circle extends Point
{
private double radius;

public Circle(int x, int y, int r)
{
super(“CIRCLE”, x, y);
this.radius = r;
}

public String toString()
{
return super.toString() + “ is center, radius is “ + this.radius;
}

}//end class Circle

Recursion Section

1. (15 points) Given the following recursive code, show the recursive calls that will be made to fib (a stack or tree is fine – tree probably works best) with a value of 5.  The first call in the tree is given for you below.

int fib(int n )

{

if (n<2)

return n;

else

return fib(n-1) + fib(n-2);

}// end fib

fib (5) – first call

/             \

For solution: see class notes

1. (15 points) Show the total number of calls that will be made to the method twelve based on an initial call of: twelve(8).  Use the space to the right for your answer.

void twelve(int n)

{

if (n > 2)

{

twelve(n-3);

twelve(n-2);

twelve(n-1);

}

System.out.println(n);

}

 N Number of calls 0 1 1 1 2 1 3 1 + 1 + 1 + 1 -> 4 (the call to 3, plus how many for 2, 1, and 0) 4 1 + 4 + 1 + 1 -> 7 5 1 + 7 + 4 + 1 -> 13 6 1 + 13 + 7 + 4 -> 25 7 1 + 25 + 13 + 7 -> 46 8 1 + 46 + 25 + 13 -> 85

Extra credit (5 points possible) Write a method that recursively prints the contents of an array of Strings in reverse order.

public void print(String [] array, int elementToPrint)

{

if (elementToPrint >=0)

{

System.out.println(array[elementToPrint]);

print(array, elementToPrint – 1);

}

}

The above assumes an initial call like this:

print(array, array.length – 1);

You could also implement this way:

public void printReverse(String [] array, int elementToPrint)

{

if (elementToPrint < array.length)

{

printReverse(array, elementToPrint + 1);

System.out.println(array[elementToPrint]);

}

}

The above method assumes an initial call like this:

printReverse(array, 0);

NOTE: the iterative version is trivial.  The first recursive solution is modeled after this:

public void printReverseIterative(String [] array)

{

int elementToPrint;

for (elementToPrint = array.length – 1; elementToPrint >=0; elementToPrint--)

System.out.println(array[elementToPrint]);

}