Java Reference
In-Depth Information
Coding Constructor Methods for the Reservations Class
Figure 5-20 displays the constructor methods that are called for use in the
Reservations class.
16
Color lightRed = new Color ( 255, 90, 90 ) ;
17
Color lightGreen = new Color ( 140, 215, 40 ) ;
18
19
Rooms room = new Rooms ( 5,3 ) ;
20
21
Panel roomPanel = new Panel () ;
22
TextArea roomDisplay [] = new TextArea [ 9 ] ;
23
24
Panel buttonPanel = new Panel () ;
25
Button bookButton = new Button ( "Book Room" ) ;
26
27
Panel inputPanel = new Panel () ;
28
Label custNameLabel = new Label ( "Name:" ) ;
29
TextField nameField = new TextField ( 15 ) ;
30
Label custPhoneLabel = new Label ( "Phone number:" ) ;
31
TextField phoneField = new TextField ( 15 ) ;
32
Label numLabel = new Label ( "Number in party:" ) ;
33
Choice numberOfGuests = new Choice () ;
34
CheckboxGroup options = new CheckboxGroup () ;
35
Checkbox nonSmoking = new Checkbox ( "Nonsmoking" , false ,options ) ;
36
Checkbox smoking = new Checkbox ( "Smoking" , false ,options ) ;
37
Checkbox hidden = new Checkbox ( "" , true ,options ) ;
38
FIGURE 5-20
Lines 16 and 17 construct two new colors, lightRed and lightGreen, with
their RGB arguments. Line 19 constructs an instance of the Rooms object, which
was coded earlier in this chapter, with the integers 5 and 3 as arguments to
represent five nonsmoking rooms and three smoking rooms.
Recall that a Panel is a GUI component that serves as a container for other
components. A Panel must be declared and constructed like other components.
Lines 21, 24, and 27 declare and construct three Panels named roomPanel,
buttonPanel, and inputPanel. The indentation below each Panel constructor
serves to visually separate the code and indicate which components will be
included in each Panel.
When an array contains values other than primitive data types, such as a
reference data type or String, it is considered an object array, or control array .
Line 22 constructs an object array of TextArea components with the name
roomDisplay. Creating an array of TextArea components simplifies the code,
because you do not have to create separate names and constructors for each of
the TextArea components. It also facilitates sending data to the TextArea compo-
nents, which will be discussed later in the chapter. Even though the restaurant
has only eight rooms, the constructor creates nine rooms, numbered 0 through
8. This allows you to relate the TextArea component numbers with the room
number without having to add 1 to the index number.
You also can create an object array of programmer-defined data types or
classes. For example, you might have an external class named Employee, with
constructor and instance methods for one employee. In your driver class, the
program might create an array of type Employee as in the following code:
Employee empGroup = new Employee[16];
Search WWH ::




Custom Search