Java Reference
In-Depth Information
The following step illustrates entering the beginning code for the Rooms class.
To Enter Beginning Code for the Rooms Class
1. Enter the code as shown in Figure 5-7 on the previous page, replacing the
programmer name and date shown with your name and the current date.
The TextPad window displays the beginning code for the Rooms class
(Figure 5-8).
opening
brace
block comment
class header
insertion point
FIGURE 5-8
As shown in line 12, the Rooms class is declared public. As you have learned,
public classes are accessible by all objects, which means that public classes can be
extended, or used, as a basis for any other class. If you develop a viable Rooms class
as a public class, you can use it to create additional, more specific classes, which
keeps you from having to start over from scratch. Each new class can become an
extension of the original Rooms class, inheriting its data and methods.
The next step in coding the program is to declare variables for the Rooms
class. These variables will be used by the Rooms() constructor method to create
an array that represents empty rooms.
Arrays
A single piece of data usually is stored as a variable with a unique identifier
name and a predetermined data type. When you manipulate that data, you refer-
ence its identifier name. Using this approach, a list of related data items, stored
in individual locations, each would require a separate line of code to manipulate
the values.
Java and other programming languages use a data structure called an array
to store lists of related data items and manipulate data more efficiently. As previ-
ously noted, Java thus allows programmers to use an array to store multiple data
items of the same type in a single storage location. An array stores these multiple
data items in a contiguous block of memory, divided into a number of slots.
Think of an array as a stretched variable — a location that still has one identifier
name, but is larger in that it can hold more than one value. Each item in the
array is referred to as a member , or element , of the array; each element can
Search WWH ::




Custom Search