CIS 180
Lab 2: Objects: Defining, Creating, and Using
In the Laboratory: The Circle Class


In the Laboratory: The Circle Class

The purpose of this laboratory project is to give you practice designing and implementing a Java class definition. The exercise is patterned after the Java program development described in Chapter 1. The objectives of this project are

The following sections provide a step-by-step framework for completing this exercise.

Problem Statement

Design and implement a class to represent a geometric circle, and then test your class definition by implementing a main() method that creates Circle instances and displays their circumferences and areas. Your program should produce the following output on the screen:

       The diameter of circle1 is 20.0
The area of circle1 is 314.0
The circumference of circle1 is 62.80
The diameter of circle2 is 30.0
The area of circle2 is 706.5
The circumference of circle2 is 94.2

Problem Decomposition

This problem can be divided into one class, the Circle class, which will implement the geometric circle. It will contain its own main() method so that it can create and test Circle instances.

Problem Design

Develop a specification for the Circle class, which identifies the purpose of the class, its instance variables to represent attributes and methods.

A Rectangle Class is provided as an example. You can download the Rectangle source code:

    http://www.cis.umassd.edu/~x2zhang/courses/CIS180/labs/lab2/Rectangle.java

Type the specification using the same text editor you use for typing the program itself. Enclose the specification within a Java comment block and save it in a file named Circle.java. This file will be the file you use for defining the Circle class. The comment will serve as documentation for the program.

A comment  block in Java begins with either ``/*'' or ``/** `` and ends with ``*/''. It can extend over multiple lines. As explained in Appendix A, comments that begin with ``/**'' are documentation comments, which can be automatically turned into documentation for the class using software that comes with the Java Development Kit (JDK). You would use documentation comments to describe a class's specification. Here's an example format that you can use:

    /** 
* Class Name: Circle
*
* ... ( the rest of your specification )
*
*/

Implementation

Use the stepwise refinement approach, to convert your specification into a Java class definition. That is, write the program in small stages, compiling and running the program after each stage is coded. This will enable you to localize any errors that are made. Remember that Java is case sensitive and very fussy about the spelling of identifiers and keywords.

Note: Rather than typing everything from scratch, another way to write this code is to copy and paste an existing program, the Rectangle source code you have donwloaded. You can then modify it using cut, copy, and paste editing.

Reasonable coding stages for this project would be