Today's tutorial is a basic overview of how to use Dr. Java to write simple classes with some interaction. We will also cover the basic standards and conventions for documentation, formatting, and testing. The material for this tutorial is related to Lecture Module 8 (see handouts).
Endpoint
In the skeleton code for today's tutorial, you will find the
Endpoint class, which represents a single endpoint for an interval
on the real number line. Already written are the three private fields
double location, boolean infinite, and
boolean closed, as well as two constructors. Read over the
documentation in the class so that you understand what these fields are for
and how the constructors are used.
As the documentation describes, the intervalNotEmpty method
tests whether the interval between two given endpoints contains any real
numbers. Write the body of this method.
Now open the class EndpointTest (also included in the skeleton
code). First inspect the tests and make sure you understand their purpose, then
run the main method in the
EndpointTest class to test your implementation of
the Endpoint class.
Interval
Create a new class (and corresponding file) called Interval
to represent a single interval on the real number line. You should have two
private fields for the left and right Endpoints, and a single
constructor which takes two endpoints as arguments. You should also create
a public method, contains, which takes one double
value and tests whether that point is contained in the interval or not.
In the same style as the EndpointTest class, write a class
IntervalTest which contains some tests for your implementation
of the Interval class. Then run your tests in Dr. Java to make
sure they pass.
Add a new method to the Endpoint class called scale
which takes one double argument, produces no value, and mutates
the Endpoint so that the location is multiplied by the given
magnitude.
Use the scale method you just wrote in Endpoint to
write an identical method in the Interval class which modifies the
corresponding interval in the same way. Note that you have to be careful when
the given magnitude is a negative number.
Now write some more tests in the two classes EndpointTest
and IntervalTest to test your new methods, and run them to make
sure they all pass.
Please take a minute to briefly answer the following questions so we can make sure these tutorials stay useful and enjoyable:
Thanks for your (anonymous) feedback, and know that we will take your comments into consideration.
Last modified on Friday, 19 August 2011, at 18:05 hours.