Java Reference
In-Depth Information
public
String toLowerCase()
{
//code to convert all the characters of a string to lowercase
}
...
}
As you can see, the
class
String
has quite a few members. It has methods to
implement operations such as compare strings and concatenation operations to join
strings. In general, to design a class you must know what data you need to manipulate
and what operations you need to manipulate the data. For example, suppose that you
want to design the
class
Circle
that implements the basic properties of a circle. Now
every circle has a radius, which can be a floating-point value. Therefore, when you create
an object of the
class
Circle
, then you must store the radius of the circle into that
object. Next, the two basic operations that are performed on a circle are to find the area
and circumference of the circle. Thus, the
class
Circle
must provide these two
operations. This class needs to provide a few other operations to effectively use this class
in a program. In skeleton form, the definition of the
class
Circle
is as follows:
public class
Circle
{
private double
radius;
8
public double
area()
{
//code to determine the area of the circle
}
public double
perimeter()
{
//code to determine the perimeter of the circle
}
//Additional methods as needed
...
}
At this point you don't need to be concerned with this definition of the
class
Circle
.
We will create such a class in Example 8-4, of this chapter.
Considering the definition of the
class
Circle
, it is apparent that to design your own
class you need to be familiar with several things. For example, in the definition of the
class
Circle
, the variable
radius
is declared with the keyword
private
and the
methods
area
and
perimeter
are declared with the keyword
public
. Also, notice that
the methods
area
and
perimeter
have no parameters. You will learn these and various
other characteristics of a class in this chapter.
Next, we give the syntax of a Java class and describe its various parts.
Search WWH ::
Custom Search