SlideShare a Scribd company logo
1 of 49
Java Programming, 3e
Concepts and Techniques
Chapter 5
Arrays, Loops, and
Layout Managers Using
External Classes
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
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
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
5Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations Program
• A stand-alone windowed application.
6Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
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
8Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Design the Solution
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
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
11Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
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
13Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
14Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Rooms Class
15Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
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
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];
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};
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];
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.
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
22Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
23Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
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
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
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
27Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The bookRoom() Method
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.
29Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
30Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations Class
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
32Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
Calling Constructor Methods for
the Reservations Class
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
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
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
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
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
38Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
GridBagLayout
• Components can be of varying size and can be added in
any order
39Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The Reservations() Constructor
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
41Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
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
43Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The main() Method
44Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The actionPerformed() Method
45Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
The clearFields() Method
• Clears input fields after a successful booking.
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
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
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
Java Programming, 3e
Concepts and Techniques
Chapter 5 Complete
Arrays, Loops, and
Layout Managers Using
External Classes

More Related Content

What's hot

CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)Dr. Ahmed Al Zaidy
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with DataNicole Ryan
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)Dr. Ahmed Al Zaidy
 
MATLAB Basics-Part1
MATLAB Basics-Part1MATLAB Basics-Part1
MATLAB Basics-Part1Elaf A.Saeed
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOPDwight Sabio
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- Cswatisinghal
 
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Philip Goddard
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
Array sorting
Array sortingArray sorting
Array sortingALI RAZA
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 
Results for EvoSuite-MOSA at the Third Unit Testing Tool Competition
Results for EvoSuite-MOSA at the Third Unit Testing Tool CompetitionResults for EvoSuite-MOSA at the Third Unit Testing Tool Competition
Results for EvoSuite-MOSA at the Third Unit Testing Tool CompetitionAnnibale Panichella
 

What's hot (16)

Ppt chapter05
Ppt chapter05Ppt chapter05
Ppt chapter05
 
Testing part 2 bb
Testing part 2 bbTesting part 2 bb
Testing part 2 bb
 
CIS110 Computer Programming Design Chapter (10)
CIS110 Computer Programming Design Chapter  (10)CIS110 Computer Programming Design Chapter  (10)
CIS110 Computer Programming Design Chapter (10)
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with Data
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
 
MATLAB Basics-Part1
MATLAB Basics-Part1MATLAB Basics-Part1
MATLAB Basics-Part1
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOP
 
Pptchapter04
Pptchapter04Pptchapter04
Pptchapter04
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...Taking your machine learning workflow to the next level using Scikit-Learn Pi...
Taking your machine learning workflow to the next level using Scikit-Learn Pi...
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
Array sorting
Array sortingArray sorting
Array sorting
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 
Ppt chapter03
Ppt chapter03Ppt chapter03
Ppt chapter03
 
Stack&heap
Stack&heapStack&heap
Stack&heap
 
Results for EvoSuite-MOSA at the Third Unit Testing Tool Competition
Results for EvoSuite-MOSA at the Third Unit Testing Tool CompetitionResults for EvoSuite-MOSA at the Third Unit Testing Tool Competition
Results for EvoSuite-MOSA at the Third Unit Testing Tool Competition
 

Viewers also liked

Viewers also liked (18)

Heena
Heena Heena
Heena
 
Basics of statistics
Basics of statisticsBasics of statistics
Basics of statistics
 
Java
JavaJava
Java
 
Lecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loopsLecture 5 - Interaction with for each and while loops
Lecture 5 - Interaction with for each and while loops
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 
The Loops
The LoopsThe Loops
The Loops
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Oop java
Oop javaOop java
Oop java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 

Similar to Chapter 05

Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithmsiqbalphy1
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#Muhammad Younis
 
Exception Management patterns in Logic Apps
Exception Management patterns in Logic AppsException Management patterns in Logic Apps
Exception Management patterns in Logic AppsBizTalk360
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
How to Reduce Scikit-Learn Training Time
How to Reduce Scikit-Learn Training TimeHow to Reduce Scikit-Learn Training Time
How to Reduce Scikit-Learn Training TimeMichael Galarnyk
 
The Ring programming language version 1.5.1 book - Part 68 of 180
The Ring programming language version 1.5.1 book - Part 68 of 180The Ring programming language version 1.5.1 book - Part 68 of 180
The Ring programming language version 1.5.1 book - Part 68 of 180Mahmoud Samir Fayed
 
AlphaPy: A Data Science Pipeline in Python
AlphaPy: A Data Science Pipeline in PythonAlphaPy: A Data Science Pipeline in Python
AlphaPy: A Data Science Pipeline in PythonMark Conway
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objectsmcollison
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Sachintha Gunasena
 
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxDeepakJangid87
 
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...Lucidworks
 
Informatica power center online training
Informatica power center online trainingInformatica power center online training
Informatica power center online trainingSmartittrainings
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
Apache Spark Machine Learning
Apache Spark Machine LearningApache Spark Machine Learning
Apache Spark Machine LearningPraveen Devarao
 

Similar to Chapter 05 (20)

Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Constructors and Method Overloading
Constructors and Method OverloadingConstructors and Method Overloading
Constructors and Method Overloading
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Chap01
Chap01Chap01
Chap01
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
Exception Management patterns in Logic Apps
Exception Management patterns in Logic AppsException Management patterns in Logic Apps
Exception Management patterns in Logic Apps
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
How to Reduce Scikit-Learn Training Time
How to Reduce Scikit-Learn Training TimeHow to Reduce Scikit-Learn Training Time
How to Reduce Scikit-Learn Training Time
 
The Ring programming language version 1.5.1 book - Part 68 of 180
The Ring programming language version 1.5.1 book - Part 68 of 180The Ring programming language version 1.5.1 book - Part 68 of 180
The Ring programming language version 1.5.1 book - Part 68 of 180
 
AlphaPy
AlphaPyAlphaPy
AlphaPy
 
AlphaPy: A Data Science Pipeline in Python
AlphaPy: A Data Science Pipeline in PythonAlphaPy: A Data Science Pipeline in Python
AlphaPy: A Data Science Pipeline in Python
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1
 
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
 
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
 
Informatica power center online training
Informatica power center online trainingInformatica power center online training
Informatica power center online training
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Rapid Miner
Rapid MinerRapid Miner
Rapid Miner
 
Apache Spark Machine Learning
Apache Spark Machine LearningApache Spark Machine Learning
Apache Spark Machine Learning
 

More from Chaffey College (20)

Strings Objects Variables
Strings Objects VariablesStrings Objects Variables
Strings Objects Variables
 
Ruby Chapter 2
Ruby Chapter 2Ruby Chapter 2
Ruby Chapter 2
 
Social networks and games
Social networks and gamesSocial networks and games
Social networks and games
 
Serious games
Serious gamesSerious games
Serious games
 
The games factory 2 alien wars
The games factory 2 alien warsThe games factory 2 alien wars
The games factory 2 alien wars
 
Target markets
Target marketsTarget markets
Target markets
 
Ch 8 introduction to data structures
Ch 8 introduction to data structuresCh 8 introduction to data structures
Ch 8 introduction to data structures
 
Ch 8 data structures in alice
Ch 8  data structures in aliceCh 8  data structures in alice
Ch 8 data structures in alice
 
Ch 7 recursion
Ch 7 recursionCh 7 recursion
Ch 7 recursion
 
Intro to gml
Intro to gmlIntro to gml
Intro to gml
 
Power point unit d
Power point unit dPower point unit d
Power point unit d
 
Power point unit c
Power point unit cPower point unit c
Power point unit c
 
Power point unit b
Power point unit bPower point unit b
Power point unit b
 
Power point unit a
Power point unit aPower point unit a
Power point unit a
 
Gamegraphics
GamegraphicsGamegraphics
Gamegraphics
 
Gamesound
GamesoundGamesound
Gamesound
 
Ch 6 text and sound in alice
Ch 6 text and sound in aliceCh 6 text and sound in alice
Ch 6 text and sound in alice
 
Ch 5 boolean logic
Ch 5 boolean logicCh 5 boolean logic
Ch 5 boolean logic
 
Ch 5 boolean logical in alice
Ch 5  boolean logical in aliceCh 5  boolean logical in alice
Ch 5 boolean logical in alice
 
Game maker objects
Game maker objectsGame maker objects
Game maker objects
 

Chapter 05

  • 1. Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 5Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The Reservations Program • A stand-alone windowed application.
  • 6. 6Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 7. 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
  • 8. 8Chapter 5: Arrays, Loops, and Layout Managers Using External Classes Design the Solution
  • 9. 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
  • 10. 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
  • 11. 11Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 12. 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
  • 13. 13Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 14. 14Chapter 5: Arrays, Loops, and Layout Managers Using External Classes Rooms Class
  • 15. 15Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 16. 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
  • 17. 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];
  • 18. 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};
  • 19. 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];
  • 20. 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.
  • 21. 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
  • 22. 22Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 23. 23Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 27Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The bookRoom() Method
  • 28. 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.
  • 29. 29Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 30. 30Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The Reservations Class
  • 31. 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
  • 32. 32Chapter 5: Arrays, Loops, and Layout Managers Using External Classes Calling Constructor Methods for the Reservations Class
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 38Chapter 5: Arrays, Loops, and Layout Managers Using External Classes GridBagLayout • Components can be of varying size and can be added in any order
  • 39. 39Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The Reservations() Constructor
  • 40. 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
  • 41. 41Chapter 5: Arrays, Loops, and Layout Managers Using External Classes
  • 42. 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
  • 43. 43Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The main() Method
  • 44. 44Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The actionPerformed() Method
  • 45. 45Chapter 5: Arrays, Loops, and Layout Managers Using External Classes The clearFields() Method • Clears input fields after a successful booking.
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. Java Programming, 3e Concepts and Techniques Chapter 5 Complete Arrays, Loops, and Layout Managers Using External Classes