Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming Based on Events
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Games and Simulations O-O Programming in Java The Walker School
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 8: Writing Graphical User Interfaces
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
A First Book of ANSI C Fourth Edition
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JavaScript, Fourth Edition
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java SE 8 for Programmers, Third Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Chapter 10 Introduction to Classes
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Object Oriented Programming.  Interface  Event Handling.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Notes for Assignment #2 Assn2: GUI Database Interface Corresponds with Chapters 28, 29, 31.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays Chapter 7.
CSC 205 Programming II Lecture 5 AWT - I.
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 3: Using Methods, Classes, and Objects
Java Programming: From Problem Analysis to Program Design,
Java Programming: Guided Learning with Early Objects
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
CS360 Client/Server Programming Using Java
CIS 199 Final Review.
Presentation transcript:

Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 2 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 3 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 4 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 5 The Reservations Program A stand-alone windowed application.

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 6

7 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 8 Design the Solution

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 9 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 10 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 11

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 12 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 13

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 14 Rooms Class

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 15

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 16 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 17 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];

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 18 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};

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 19 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];

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 20 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.

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 21 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 22

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 23

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 24 Unary operators behave differently depending on whether the operator is positioned before or after the variable

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 25 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 26 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 27 The bookRoom() Method

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 28 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.

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 29

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 30 The Reservations Class

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 31 Choice Components A Choice component displays a restricted list through a drop-down list box with a box arrow

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 32 Calling Constructor Methods for the Reservations Class

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 33 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 34 Flow Layout Change alignment with setLayout() and the constructor Add components with the add() method

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 35 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 36 GridLayout All components must be the same size The constructor takes the number of rows and columns as parameters

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 37 CardLayout Each card may have its own layout manager Only the top card on the stack is visible

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 38 GridBagLayout Components can be of varying size and can be added in any order

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 39 The Reservations() Constructor

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 40 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 41

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 42 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 43 The main() Method

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 44 The actionPerformed() Method

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 45 The clearFields() Method Clears input fields after a successful booking.

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 46 Testing a Windowed Application Compile and correct errors Use multiple test cases to test maximum values Test with incorrect data

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 47 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

Chapter 5: Arrays, Loops, and Layout Managers Using External Classes 48 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