0% found this document useful (0 votes)
103 views2 pages

The Circle Class (Classes and Instances) : This First Exercise Shall Lead You Through All The in OOP

This document describes a Circle class with private instance variables for radius and color, and public methods for getting radius, area, color, and setting radius and color. It cannot be run directly as it lacks a main method, but is meant to be used by another class like TestCircle. The document provides exercises to modify Circle to include additional constructors, getters, setters using the 'this' keyword, and a toString method for output.

Uploaded by

Guia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views2 pages

The Circle Class (Classes and Instances) : This First Exercise Shall Lead You Through All The in OOP

This document describes a Circle class with private instance variables for radius and color, and public methods for getting radius, area, color, and setting radius and color. It cannot be run directly as it lacks a main method, but is meant to be used by another class like TestCircle. The document provides exercises to modify Circle to include additional constructors, getters, setters using the 'this' keyword, and a toString method for output.

Uploaded by

Guia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The Circle Class (Classes and Instances)

This first exercise shall lead you through all the basic concepts in OOP.

A class called circle is designed as shown in the following class diagram. It contains:

 Two private instance variables: radius (of the type double) and color (of the type
String), with default value of 1.0 and "red", respectively.
 Two overloaded constructors - a default constructor with no argument, and a
constructor which takes a double argument for radius.
 Two public methods: getRadius() and getArea(), which return the radius and area
of this instance, respectively.

Compile "Circle.java". Can you run the Circle class? Why?

This Circle class does not have a main() method. Hence, it cannot be run directly. This
Circle class is a “building block” and is meant to be used in another program.

Let us write a test program called TestCircle (in another source file called
TestCircle.java) which uses the Circle class.

More Basic OOP Concepts

1. Constructor: Modify the class Circle to include a third constructor for constructing a
Circle instance with two arguments - a double for radius and a String for color.
Modify the test program TestCircle to construct an instance of Circle using this
constructor.
2. Getter: Add a getter for variable color for retrieving the color of this instance.
3. Setter: Is there a need to change the values of radius and color of a Circle instance
after it is constructed? If so, add two public methods called setters for changing the
radius and color of a Circle instance.
Modify the TestCircle to test these methods.
4. Keyword "this": Instead of using variable names such as r (for radius) and c (for
color) in the methods' arguments, it is better to use variable names radius (for radius)
and color (for color) and use the special keyword "this" to resolve the conflict
between instance variables and methods' arguments.
Modify ALL the constructors and setters in the Circle class to use the keyword "this".
5. Method toString(): Every well-designed Java class should contain a public method
called toString() that returns a short description of the instance (in a return type of
String). The toString() method can be called explicitly (via instanceName.toString())
just like any other method; or implicitly through println(). If an instance is passed to
the println(anInstance) method, the toString() method of that instance will be invoked
implicitly. For example, include the following toString() methods to the Circle class:

The final class diagram for the Circle class is as follows:

You might also like