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.
Circle
. This class will not be a Java application, so it does not need
a main()
method.
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.
Build a new
Circle
class with the following basic features:
myX
and
myY
represented as double
values).myDiameter
also represented as a double
value).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.
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.
Build an explicit-value constructor that implements the following algorithm.
myX
, myY
and myDiameter
have been
declared as instance variables of type double
.x
, y
and diameter
.diameter
satisfies the invariant, then
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:
throws exception
clause to the declaration of the constructor (after the parameter list,
before the opening brace, {
).
try { your console-based commands (which might throw errors)... } catch (Exception e) { print the message encapsulated in the thrown 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.
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.
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
Using JUnit, build test cases that exercise the following aspects of your circle class:
getArea()
method - Construct two or three valid circle objects and verify that they can
compute their areas correctly.
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:
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.
Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria: