CPSC/DTSC 115L: Introduction to Computer Science Fall 2026

Laboratory 6: Your first Java class

Wednesday, October 14
Thursday, October 15

As done last week, you are expected to work with an assigned partner as a pair. Both you and your partner will receive the same grade. Both of you should always save your laboratory work on your own accounts.

Objectives

The objectives of this laboratory are
  1. to give practice designing a simple class to represent geometric circles, and
  2. to convert your design into a working Java class.

1. Preparation

As usual, open a Terminal window and create a subdirectory named lab6 within your cpsc115 directory. You should do all your work in the directory lab6 today.

2. The class Circle

In this laboratory, you will design and implement a class named Circle to represent geometric circles. This class should have two fields:
  1. PI — the absolute constant π, the ratio of a circle's circumference to its diameter.
  2. diameter — a variable that contains the diameter of the Circle object.
The class should also contain three methods:
  1. Circle()—a constructor to instantiate a Circle object of a given diameter in a parameter.
  2. calculateCircumference()—a method that returns the circumference of the Circle object.
  3. calculateArea()—a method that returns the area of the Circle object.

3. Design and implementation

Design. Identify what attributes and operations you need. Develop a detailed specification for the class Circle with its attributes and behaviors and draw a UML class diagram to describe the Circle class. The attribute PI for π is a constant, not a variable. Model your diagram after that of the class Riddle discussed in class. Have your diagram checked by the instructor or a TA before you implement.

Implementation. Now, followinging the five-step development cycle discussed in class, implement and test the class Circle. First, implement the class Circle in the file Circle.java as follows.
  1. Review your UML diagram.
  2. Implement the declarations for the class's fields. For π, using Java's constant Math.PI, put final double for a type and name it PI, all in uppercase letters, to indicate that it is a constant.
  3. Implement the constructor.
  4. Implement the method calculateCircumference().
Next, in a new window, define the class CircleApplication in a file named CircleApplication.java in the following way:
/**
 * Class name: CircleApplication
 * This is a Java application to test the class Circle.
 *
 * @author Takunari Miyazaki
 * @author Ewa Syta
 * @version 1.0, 10/14/2026
 */

public class CircleApplication {
  public static void main(String args[]) {
    double diameter1 = 20.0;
    Circle circle1 = new Circle(diameter1);
    System.out.println("The diameter of circle1 is " + diameter1 + " inches.");
    System.out.println("The circumference of circle1 is " + circle1.calculateCircumference() + " inches.");
  } // main()
} // CircleTester
Compile both the files Circle.java and CircleApplication.java. Upon executing CircleApplication, you should see the following output:
The diameter of circle1 is 20.0 inches.
The circumference of circle1 is 62.83185307179586 inches.
Now, complete these additional steps:
  1. Add code to the main() method to create another Circle object and test your calculateCircumference() method.
  2. Implement the second method calculateArea() in the class Circle and then add code to the main() method to test your calculateArea() method.

Documentation

As usual, document both source files Circle.java and CircleApplication.java in the Javadoc format using comment blocks as shown in given examples: Each class should begin with a comment block. Each method should also begin with a comment block; if there are any, itemize parameters using @param for each parameter and list the return value using @return. As joint work, list both your and your partner's names.

What to hand in

For completion, having all your work checked by the instructor or a TA, each partner should now submit individually both source files Circle.java and CircleApplication.java to this Moodle site:
https://moodle.trincoll.edu/mod/assign/view.php?id=663422
If you have completed this laboratory early, stay and work on Homework 5 for the rest of the time.


* CPSC/DTSC 115L home page
Valid HTML 4.01!