In particular, between NumberGuessers and GridWriters, which need to throw exceptions, and which need to catch exceptions?

In this assignment, we will revisit assignments from previous modules and add exception handling. There are three classes that we have written that can be significantly improved by adding exceptions. The Circle class, for example, should probably throw an IllegalArgumentException if setRadius is invoked with a negative argument. 

This week we will work on improving the GridWriter class and both of the NumberGuesser classes. You can download a simplified copy of the GridWriter code Here: GridWriter.zip.  If you are not fully satisfied with your NumberGuesser and RandomNumberGuesser classes then you can download working code here: NumberGuessers.zip

Refresh yourself on the details of these two projects, then read about the required modifications for the assignment below.

GridWriter Class: You will modify the GridWriter class by adding additional collection style functionality. The GridWriter class should get two new methods:

  • public int size()  should return the number of GridItems stored in the GridWriter
  • public GridItem get(int index)  should return the stored GridItems by index.


Consider the following code. The first line creates a GridWriter object.  Then two items are added to the GridWriter. The index of the items will be 0 and 1. Notice how the for loop uses the size and get methods to print out the areas of the two items

GridWriter gw = new GridWriter(40, 50);

gw.add(new MyCircle(10, 10, 9));

gw.add(new MyRectangle(40, 0, 10, 10));

for (int i = 0; i < gw.size(); i++)
{
        System.out.println(gw.get(i).getArea());
}

Once you have these two methods working you should add exception logic to the get method. The following code should cause your GridWriter to throw an IndexOutOfBoundsException. 

GridWriter gw = new GridWriter(40, 50);

gw.add(new MyCircle(10, 10, 9));

gw.add(new MyRectangle(40, 0, 10, 10));

GridItem i = gw.get(2);

Although the array inside the Gridwriter has a capacity of 4, it only stores two GridItems. '2' is not a valid index. Add a throw statement to your get method that will throw an IndexOutOfBoundsException for any invalid index. You do not need to catch the exception anywhere. 

Submit your modified GridWriter.java. 

NumberGuesser classes: When you worked on the NumberGuesser classes you might have noticed that the higher() and lower() methods can be called until there are no possible values left for the guesser to guess. In this case, it seems fair for a NumberGuesser to throw an IllegalStateException.

If the NumberGuesser classes were upgraded in this matter then it would be possible to add some exception handling code to the number guessing game so that the users could be notified when the guesser has caught them cheating. 

The higher and lower methods of both the NumberGuesser and RandomNumberGuesser should throw the exception if there are no more remaining numbers to guess. You might be able to achieve this by adding the logic to your NumberGuesser and letting RandomNumberGuesser inherit the behavior. Or you might need to add the logic to both classes. It will depend on your implementation.

Add a try-catch block to your number guessing game so that the user is notified if the sequence of 'h' and 'l' responses is untenable. Feel free to use the code in NumberGuessers.zip as the starting point for your modifications. 

What to Submit

Submit a zipped directory that contains all of the code for both projects. The zipped directory should have two subdirectories. One named "GridWriter" and one named "NumberGuesser".  Those directories should contain all of the .java files required for each project, both the files that you modified and the files that you did not.

Solved
Programming in Java 3 Answer Anonymous(s) Post