1 / 49

Chapter 5

Chapter 5. Arrays, Loops, and Layout Managers Using External Classes. Objectives. Create and implement an external class Write code to create a constructor class method Construct an instance method to initialize instance variables

brina
Télécharger la présentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Arrays, Loops, and Layout Managers Using External Classes

  2. 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. 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, Choice, and Text Area components

  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

  5. The Reservations Program • A stand-alone windowed application.

  6. 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.

  7. Design the Solution - Storyboard

  8. Design the Solution - Components • TextArea components display larger amounts of information than TextFields. • A choice component displays a drop-down list, which ensures valid entries. • TextFields allow user entries. • A hidden checkbox component is used to clear the checkbox options. • A button triggers an event.

  9. Program Design • The Reservations class is the driver class. • The Rooms class is an external class. • Create variable dictionaries. • A class list of variables with their type and purpose. • Create method prototypes, headers, and flowcharts.

  10. Methods in the Reservation class • Constructor methods • Define an instance of an object • Have no return type • Have the same name as the object • Create an instance of the object internally

  11. Rooms Class

  12. Creating an External Class • An external class is a class that is not a driver class • External classes are declared public to be accessible by all objects and to allow for inheritance

  13. 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];

  14. Arrays - cont. • 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};

  15. Arrays - cont. • 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];

  16. 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.

  17. 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.

  18. Unary operators behave differently depending on whether the operator is positioned before or after the variable.

  19. 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.

  20. 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.

  21. The bookRoom() Method

  22. Windowed Applications • The AWT classes are abstract and provide only essential components which are obtained from the native environment when instantiated. • The Swing classes are lightweight Java implementations of standard GUI controls. • A Container is an object that holds other components. • A Frame is a container for a collection of graphical AWT components. • A program using a Frame extends the Frame class.

  23. The Reservations Class Header

  24. Choice Components • A Choice component displays a restricted list through a drop-down list box with a box arrow.

  25. Calling Constructors in the Reservations Class

  26. 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.

  27. Flow Layout • Change alignment with setLayout() and the constructor. • Add components with the add() method.

  28. BorderLayout • The add() method takes a parameter specifying the desired region. • The constructor() method takes two integers parameters to specify the number of pixels between components.

  29. GridLayout • All components must be the same size. • The constructor takes the number of rows and columns as parameters.

  30. CardLayout • Each card may have its own layout manager. • Only the top card on the stack is visible.

  31. GridBagLayout • Components can be of varying size and can be added in any order.

  32. The Reservations() Constructor

  33. 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.

  34. 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.

  35. The main() Method

  36. The actionPerformed() Method

  37. The clearFields() Method • Clears input fields and resets the insertion point and hidden CheckBox.

  38. Testing • Compile and correct errors • Use multiple test cases to test limits • Test with incorrect data • Document the solution

  39. Summary • Coded instance methods • Created constructor class methods • Declared, constructed, and manipulated arrays • Coded a counter-controlled loop using the for statement • Employed assignment and unary operators

  40. Summary • Developed a stand-alone windowed application • Implemented a driver class and an external class • Used AWT components • Used a layout manager to precisely place components

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

More Related