Objectives

Students who complete this lab will demonstrate that they can:

In this lab, you will build a new class representing simple circle objects, each of which have a center point (x, y) and a diameter. Though simple, this class will provide useful examples of exception handling and unit testing. It also gives a preview of the sorts of graphical objects that we will be using in Chapter 11 where we reintroduce Processing in the Java context.

This lab will also introduce the Java debugger, a powerful tool for examining the execution of Java programs.

Exercise 10.1
  1. Create an appropriate package for lab 10.
  2. Create a new class named Circle. This class will not be a Java application, so it does not need a main() method.

A Circle Class

This lab uses a simple circle class as a test bed. We’d like to be able to run a console application that creates a default circle and prints out its attributes like the following:

Circle c1 = new Circle();
System.out.println(c1.getX());
System.out.println(c1.getY());
System.out.println(c1.getDiameter());

This use of a default constructor should produce a circle centered at the origin with diameter 100.

Exercise 10.2

Build a new Circle class with the following basic features:

Refer back to Chapter 9 in the text if any of these terms or features are unfamiliar to you. When finished, you should be able to run a console application with the statements listed above and receive the expected results.

Exception Handling

We’d like to maintain the integrity of each circle object by ensuring that the class invariant (that the diameter should be non-negative) is true at all times. The default constructor ensures this by setting the default diameter to be 100. You will now add an explicit-value constructor that will need to verify the explicit values it receives from the calling program before constructing a new circle object.

Exercise 10.3

Build an explicit-value constructor that implements the following algorithm.

This final step, the throwing of an exception is a departure from what you have done in the past to signal errors, which was always to print an error message and terminate execution of the program. The compiler will require that you do two things the properly handle this exception:

You can get a thrown exception’s message using the method exception.getMessage() . Try adding a call to the explicit-value constructor that violates the invariant and check to see that the appropriate error message composed in the circle class is printed appropriately.

This approach to exception handling helps maintain the separation of model and view that is valuable in object-oriented design. The Circle class maintains the integrity of each circle object, throwing exceptions when problems are found; the calling program deals with the exceptions as appropriate in the context of the application. The circle class doesn’t know (or care) how it is being used.

Unit Testing

It can be useful to build unit tests for model classes like the circle class. We start this section by building a area-computation method, specifying the behavior it should implement and then testing it according to that specification.

Exercise 10.4

Build an instance method for your circle class that returns the area of the circle. Call it getArea(). Start by planning out a couple test cases that could be used to test the correctness of the method. For example, what should the area be for a circle object constructed by the default constructor? Write these test cases in as part of the documentation for the method you are about to write. Now, design and implement the method in your circle class, and use your console program to verify that you compute the correct values according to your test cases.

JUnit tests are implemented in a separate class that constructs and exercises Circle objects. Go through the JUnit tutorial for your IDE:

You can find a complete API reference for all of the JUnit assert commands here: http://junit.org/javadoc/4.10/org/junit/Assert.html

Exercise 10.5

Using JUnit, build test cases that exercise the following aspects of your circle class:

The Java Debugger

As your programs grow in size and complexity, you’ll need to upgrade your tools for finding and removing problems, known as bugs, in your code. The Java debugger is one such tool and is commonly integrated into Java development environments. Go through the JUnit tutorial for your IDE:

Exercise 10.6

If there is time, try running the debugger on a test console program that causes an exception to be thrown in the circle class’s explicit-value constructor. You should be able to see control pass through the construction of the exception object, throwing it back to the console program and the console program’s catching of that exception.

There is nothing to turn in for this exercise.

Checking In

Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria: