chapter 05
DESCRIPTION
TRANSCRIPT
![Page 1: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/1.jpg)
Java Programming, 3eConcepts and Techniques
Chapter 5Arrays, Loops, and
Layout Managers Using External Classes
![Page 2: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/2.jpg)
2Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Chapter Objectives
• Create and implement an external class
• Write code to create a constructor class method
• Construct an instance method to initialize instance variables
• Declare and construct an array using correct notation
![Page 3: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/3.jpg)
3Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Chapter Objectives
• Use layout managers with container components
• Code a counter-controlled loop using the for statement
• Correctly employ assignment and unary operators
• Use methods with Frame, Panel, TextArea and Choice components
![Page 4: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/4.jpg)
4Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Introduction
• This chapter illustrates how to create an object, complete with methods and attributes
• Objects have three key characteristics– Identity
• The object can be called and used as a single unit
– State• The object has various properties whose values might
change
– Behavior• The object can perform actions and have actions performed
upon it
![Page 5: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/5.jpg)
5Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations Program
• A stand-alone windowed application.
![Page 6: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/6.jpg)
6Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 7: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/7.jpg)
7Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Problem Analysis
• When the user enters information and clicks the Book Room button, some visual element should change in the user interface to indicate the current status
• The program should be designed to easily accommodate modifications in the number of party rooms
![Page 8: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/8.jpg)
8Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Design the Solution
![Page 9: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/9.jpg)
9Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Design the Solution
• TextArea components display larger amounts of information than TextFields
• TextFields allow user entries
• A Choice component displays a drop-down list, which ensures valid entries
• A hidden checkbox component is used to clear the checkbox options
• A button triggers an event
![Page 10: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/10.jpg)
10Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Program Design
• The Reservations class is the driver class• Create variable dictionaries
– A class list of variables with their type and purpose• Create method prototypes, headers, and flowcharts
![Page 11: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/11.jpg)
11Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 12: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/12.jpg)
12Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
• Methods in the Reservation class
• Constructor methods– Define an instance of an object – Have no return data type – Have the same name as the object– Create an instance of the object internally
![Page 13: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/13.jpg)
13Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 14: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/14.jpg)
14Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Rooms Class
![Page 15: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/15.jpg)
15Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 16: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/16.jpg)
16Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Creating an External Class
• An external class is a class that is not a driver class• The Rooms class is an external class• External classes are declared public to be accessible by
all objects and to allow for inheritance
![Page 17: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/17.jpg)
17Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Arrays
• An array stores multiple data items of the same type in a single storage location
• Declare an array with the element type, a set of square brackets, and the array identifier name– int[] ages; or int ages[];
• Construct an array with the new keyword and the array length, or number of elements– ages = new int[100];– int[] ages = new int[100];
![Page 18: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/18.jpg)
18Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Arrays
• Retrieve the array’s length using the length property– int size = arrayName.length
• Assign values and access array elements using the index number of the element– An index number is assigned to each element,
beginning at zero and progressing by integers– temperature[3] = 78;
• Declare, construct, and assign values with one statement– boolean[] results = {true, false, true, false, true};
![Page 19: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/19.jpg)
19Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Arrays
• Index numbers can be any expression that evaluates to an integer
• Array elements can be used anywhere a variable is used– overtime = (hours[6] - 40) * rate[6] * 1.5
• Two-dimensional arrays are used to create tables of values– Two index numbers represent the number of rows
and columns.– int[][] myTable = new int[4,3];
![Page 20: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/20.jpg)
20Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Constructing an Instance
• Create a constructor method for the class
• Declare a class variable and call the class constructor in the driver class
• A class may have multiple constructors, each with different arguments– The practice of defining multiple methods with the
same name is called method overloading.
![Page 21: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/21.jpg)
21Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Counter-Controlled Loops
• A counter-controlled loop executes a specific number of times
• Java uses a for loop to implement a counter-controlled loop
• Assignment and unary operators are often used to update the counter– An assignment operator performs an arithmetic and
assignment operation all with one operator, thus providing a shortcut
– A unary operator only needs one operand to perform its function
![Page 22: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/22.jpg)
22Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 23: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/23.jpg)
23Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 24: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/24.jpg)
24Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Unary operators behave differently depending on whether the operator is positioned before or after the variable
![Page 25: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/25.jpg)
25Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Unary Operators in for Loops
• A for loop often uses the increment operator to access each element of an array
• To exit a for loop prematurely, assign the counter a number outside the condition range inside the loop
![Page 26: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/26.jpg)
26Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Instance Methods
• An instance method operates or manipulates variables for an external class
• Instance variables are variables manipulated within an instance method, which are local in scope
• When called by a driver class, instance methods must use the class.method() notation
• Instance methods typically include a user-friendly active verb in the method name
![Page 27: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/27.jpg)
27Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The bookRoom() Method
![Page 28: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/28.jpg)
28Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Creating Windowed Applications
• The AWT classes are abstract and provide only essential components which are obtained from the native environment when instantiated
• The Swing components use lightweight Java implementations of standard GUI controls
• A container is an object that contains other components– A Frame is a container for a collection of graphical
AWT components.– A program using a Frame needs to use the word,
extends, in the class header.
![Page 29: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/29.jpg)
29Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 30: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/30.jpg)
30Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations Class
![Page 31: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/31.jpg)
31Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Choice Components
• A Choice component displays a restricted list through a drop-down list box with a box arrow
![Page 32: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/32.jpg)
32Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Calling Constructor Methods for the Reservations Class
![Page 33: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/33.jpg)
33Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Layout Managers
• Layout managers assist programmers organize components into predefined locations in a window
• If the user resizes the window, the size and position of the components are automatically adjusted
![Page 34: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/34.jpg)
34Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Flow Layout
• Change alignment with setLayout() and the constructor• Add components with the add() method
![Page 35: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/35.jpg)
35Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
BorderLayout
• The add() method takes a parameter specifying the desired region
• The setLayout() method takes two integers parameters to specify the number of pixels between components
![Page 36: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/36.jpg)
36Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
GridLayout
• All components must be the same size• The constructor takes the number of rows and columns
as parameters
![Page 37: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/37.jpg)
37Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
CardLayout
• Each card may have its own layout manager• Only the top card on the stack is visible
![Page 38: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/38.jpg)
38Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
GridBagLayout
• Components can be of varying size and can be added in any order
![Page 39: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/39.jpg)
39Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations() Constructor
![Page 40: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/40.jpg)
40Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Window Event-Handling Methods• A WindowListener is registered with a Frame with the
addWindowListener() method• A WindowAdapter class is used to provide the event-
handling methods for the Frame– Adapter classes implement abstract classes, providing
prewritten method for all the methods, any of which can be overridden by the programmer
![Page 41: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/41.jpg)
41Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
![Page 42: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/42.jpg)
42Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The main() Method
• Execution of a program begins with the main() method
• The main() method can be placed anywhere within the class braces
• In a windowed application, the main() method creates the instance of the Frame using a call to the constructor method
• The main() method also sets attributes for the Frame
![Page 43: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/43.jpg)
43Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The main() Method
![Page 44: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/44.jpg)
44Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The actionPerformed() Method
![Page 45: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/45.jpg)
45Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The clearFields() Method
• Clears input fields after a successful booking.
![Page 46: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/46.jpg)
46Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Testing a Windowed Application
• Compile and correct errors• Use multiple test cases to test maximum values• Test with incorrect data
![Page 47: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/47.jpg)
47Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Chapter Summary
• Create and implement an external class
• Write code to create a constructor class method
• Construct an instance method to initialize instance variables
• Declare and construct an array using correct notation
![Page 48: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/48.jpg)
48Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Chapter Summary
• Use layout managers with container components
• Code a counter-controlled loop using the for statement
• Correctly employ assignment and unary operators
• Use methods with Frame, Panel, TextArea and Choice components
![Page 49: Chapter 05](https://reader033.vdocuments.net/reader033/viewer/2022061210/548ec888b47959486e8b4569/html5/thumbnails/49.jpg)
Java Programming, 3eConcepts and Techniques
Chapter 5 Complete
Arrays, Loops, and Layout Managers Using
External Classes