UCL Logo

Problem Class Questions 2006
for COMP1008 (Object-Oriented Programming)

Week 5 (Starting 20th February)

These questions are about classes and exceptions.

Q1. Consider a Library class that provides the following services:

  • Add and remove books (there may be multiple copies of the same book).
  • Search for books.
  • Maintain a list of members of the library.
  • Add and remove members.
  • Record each book borrowed and returned by a member. Each member can hold a maximum of six books at any given time.
  • Check that a book is returned on time, otherwise issue a fine.
  • Provide a reservation system for a member to request a book already on loan.

Note that this Library class provides the services as a set of public methods but does not handle any of the screen/keyboard input and output. That is done by other classes that use the Library by calling its methods.

Assume that Library has been designed so that each method will throw an exception if it cannot complete normally. For example if a call to remove a book fails because the library doesn't have the book, then the remove method should throw an exception.

a) Identify each method the Library class needs, giving the method name, return type, parameter types and any exceptions that should be thrown. For each method that throws exceptions write a pseudocode implementation of the method body showing why the exceptions are thrown.

b) Class Library is a big class and is not very cohesive as it combines a range of different services. Show how it can be split up into a collection of smaller, more cohesive classes.

Q2. What does this method print out?

  public void f()
  {

    for (int i = 0; i < 10; i++)
    {
      try
      {
        if (i % 3 == 0)
        {
          throw new Exception("A");
        }

        try
        {
          if (i % 3 == 1)
          {
            throw new Exception("B");
          }
          System.out.println(i);
        }
        catch (Exception e1)
        {
          i *= 2;
        }
        finally
        {
          i++;
        }

      }
      catch (Exception e2)
      {
        i += 3;
      }
      finally
      {
        i++;
      }
    }
  }

Q3. Design a java class DeQueue - a double ended queue. Items can be added and removed from both the start and end of the queue. The class should have one constructor that takes one argument giving the maximum length of the dequeue. Exceptions should be thrown if an attempt is made to remove items from an empty queue or add items to a full queue.

Q4. Determine what this program prints out and explain why.

class Exception1
{
  public void a()
  {
    int array[] = new int[5];
    try
    {
      System.out.println("Try");
      array[10] = 1;
    }
    catch (Exception e)
    {
      System.out.println("Exception");
      throw e;
    }
    finally
    {
      System.out.println("Final");
      return;
    }
  }


  public static void main(String[] args)
  {
    Exception1 e1 = new Exception1();
    try
    {
      e1.a();
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
      System.out.println("Catch");
    }
    System.out.println("End of main");
  }
}

Hint: This program is correct and does compile and run. But why does method 'a' not have a throws declaration?

Last updated: September 2, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google