JamesMadisonUniversity

Computer Science Department

 

 

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:

  1. Make a directory for this lab on your Novell (M) drive and, if you wish, your flash drive..
  2. 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.
  3. Add appropriate javadocs and inline comments to the file.

3.   Adding useful methods to enhance a class

  1. Open Circle.java and insert appropriate javadocs and inline comments.
  2. Write a toString method for this class which returns a string containing the radius and the area of the circle properly labeled. 
  3. Add appropriate javadocs and inline comments.
  4. Compile Circle.java
  5. Show the result of the compilation.
  6. 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.
  7. Add appropriate javadocs and inline comments.
  8. Compile Circle.java
  9. Show the result of the compilation.
  10. 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.
  11. Compile Circle.java
  12. Show the result of the compilation.
  13. Open CircleTester.java.
  14. Create a circle object named myFirstCircle with radius of the user's choice.
  15. Call the toString method and print the result.
  16. Create a second circle object named mySecondCircle with radius of the user's choice.
  17. Call the toString method and print the result.
  18. Determine whether the two objects are equal and print the result.
  19. 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?