CS 239 Lab 8:
Playing with Classes
Objectives:
|
The student will:
- modify existing code
by adding methods to it
- write a driver to test
modified code
- add javadocs and inline comments as appropriate to code
- demonstrate
understanding of class methods and objects
|
Background:
|
A static class member belongs to the class, not to objects
instantiated from the class (Gaddis, p. 523)
|
New Terms:
|
- static class member
- static fields
|
Materials:
|
Circle.java
Thing.java
Worksheet.doc
|
Acknowledgment
|
Lab based on Gaddis text - devised by Liz Adams
|
1. General Instructions
As usual, this lab is to be submitted to Blackboard by
midnight tonight if you don't finish it during the lab period.
2. Getting Ready:
Before going any further you should:
- Make a directory for
this lab on your Novell (M) drive and, if you wish, your flash drive..
- Create a java file CircleTester.java
which will contain a single main method that will be used to exercise each
component of the lab and demonstrate that you have done that step correctly.
- Add appropriate javadocs and inline comments to the file.
3. Adding useful methods to enhance
a class
- Open Circle.java and insert appropriate javadocs and inline comments.
- Write
a toString
method for this class which returns a string containing the radius and the area of the circle properly
labeled.
- Add
appropriate javadocs and inline comments.
- Compile
Circle.java
- Show
the result of the compilation.
- Write
an equals method for this
class. The method should accept a Circle object as an
argument. It should return true if the argument objet
contains the same data as the calling object, or false otherwise.
- Add
appropriate javadocs and inline comments.
- Compile
Circle.java
- Show
the result of the compilation.
- Write
a greaterThan
method for this class. The method
should accept a Circle object as an argument. It should return true if the argument object has an area that is
greater than the area of the calling object, or false otherwise.
- Compile
Circle.java
- Show
the result of the compilation.
- Open CircleTester.java.
- Create
a circle object named myFirstCircle with radius
of the user's choice.
- Call
the toString method and print the result.
- Create
a second circle object named mySecondCircle with
radius of the user's choice.
- Call
the toString method and print the result.
- Determine
whether the two objects are equal and print the result.
- Determine
whether the second object is greater than the first object and print the
result.
4.
Understanding static fields
1. Download
Thing.java
2. Open
Thing.java
and add javadocs and inline comments
3. Create
a program named ThingDriver.java.
4. Add
javadocs and inline comments
5. Declare
three Thing objects named one, two
and three.
6. What
did you write?
7. Instantiate
one and then call the putThing
method with 15.
8. Instantiate two and then call the putThing
method with 27.
9. Instantiate
three and then call the putThing
method with 35.
10. How many
separate instances of the x member exist?
11. How many
separate instances of the y member exist?
12. How many
separate instances of the z member exist.
13. Write code
to prove your answers to 10, 11, and 12 are correct.
14. Write
statements to show the results of the
code that proved that your answers to 10, 11, and 12 were correct
15. Compile and
run your program.
16. What was
output?