A Class for Circles
Now we will develop some rather rudimentary classes to illustrate some of
the principles and issues we have worked with so far. Our first
example relates to circles. We keep it simple and don't really do
any drawing. But we will get a radius
from a user and compute relevant things such as the area and the
Whether you are aware of it or not, the radius is the most salient measure related to a circle. If you know it you should be able to find out just about anything else you want about the circle. You may remember that one classic definition is it is the set of points in a plane that are equidistant from one center point.
So, ultimately, our circles will be represented by a single number in the computer. They are not represented by a drawn circle.
Remember, it took three numbers to represent the time. When we set the time we used computer syntax
It required some extra methods to cast this into the more familiar
You should call the following file Circle.java and save it in its own directory. Whether or not you want to create a package is up to you. You will notice we sometimes make use of the this reference rather than referring to an instance of an object by its name.
public class Circle extends Object
You should compile this, likely with the more typical
We start with a discussion of the code and methods.
Now you need the following CircleTestGUI.java to construct/instantiate a Circle object and work with it.
|You do not need to specify any special path in the
You should run this code and play with the GUI some before we dig into it.
The HTML file follows and should be called CircleTestGUI.html.
<applet code="CircleTestGUI.class" width=400 height=200>
|If you try to click on the command button and don't have an appropriate input you should see something like the following.|
|With an acceptable number in the radius text fields you should see something more like the following.|
|Notice the four decimal places on the area and the
Now we will discuss the code.
We will do one more of these "simpler" classes before working some more sophisticated problems.