CS 136 Tutorials - Spring 2007

Tutorial 9: Hello Java (June 29)

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).

  1. A single class: Endpoint
  2. 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.

    • Writing a method
    • 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.

    • Testing your implementation
    • 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.

  3. A dependent class: Interval
    • Writing the class
    • 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.

    • Writing the tests
    • 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.

  4. Adding some new methods
    • Scaling an endpoint
    • 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.

    • Scaling an interval
    • 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.

    • More tests!
    • 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.

  5. Tutorial feedback
  6. Please take a minute to briefly answer the following questions so we can make sure these tutorials stay useful and enjoyable:

    1. What do you like about the tutorials themselves?
    2. What don't you like about the tutorials (what could be improved)?
    3. Is the pace too slow, too fast, or just right?
    4. Is the material covered relevant, or should we be focusing on some other topics?
    5. Any comments on the (in)effectiveness of your tutorial instructor?
    6. Any additional comments about anything relating to CS136?

    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.