Calculation Applet! Arrays! Sorting! Java Examples! D0010E! Lecture 11! The GUI Containment Hierarchy! Mergesort! News and info Lab 4 is due this week. It should be easy to change the size of the game grid. Use (public final) constants in your code. You can assume that both programs playing have the same grid size. 2 Feedback Anonymous classes..? Immediate creation of a tailor-made dynamic object based on an existing class or interface. Example in SpringLayoutExample from Lecture 9. Based on the interface ActionListener. Eck and Pilay, p. 96 (Section 5.2.1 in Part 2 of the course compendium). Another example today. Array Declaration: <Type> [] <variable>; Example: Room[] rooms;! Creation: <variable> = new <Type> [ <size> ]! Example:"!!rooms = new Room[10]; Use: <variable> (rooms) <variable> [ <index> ]!(rooms[3])! <index> must be in the range [0..<size>-1]. <variable>. length (rooms.length) 3 4
Multi-dimensional arrays "Arrays of arrays". <Type> [] [] [] <variable>; The number of pairs of brackets is the dimension. Example: ChessPiece[][][] chessboard3d;! A d-dimensional array = a 1-dimensional containing (d-1)-dimensional arrays. Ragged arrays. All arrays in some dimension need not be of equal length. It is possible to create a multi-dimensional array in steps. Fig 4.19 d-dimensional arrays 3 declarations on page 153 5 6 Fig 4.23 GUI Components Ragged arrays Java s library contains classes with readymade, commonly used graphical components. JPanel JButton JTextField JLabel, etc They all have JComponent as super class. These are to a large extent configurable. They support quite a few methods for this. As shown at the last lecture, they can also be extended (specialized via inheritance) like classes in general. 7 8
GUI Containers A GUI container can hold components and (other) containers. The various containers and their contents define a containment hierarchy. Highest up in the containment hierarchy is always a top-level container. JFrame, JApplet, or JDialog. JPanel is a both a container and a component. Layouts The relative sizes and positions of GUI components and containers with respect to each other is governed by LayoutManagers. BorderLayout, GridLayout, GridBagLayout, SpringLayout etc. Each layout-manager positions and re-sizes components according to a strategy of its own. Each container is assigned a default layoutmanager when created. setlayout( ) can be used to change the layoutmanager of a container. Every container in the containment hierarchy has a layout-manager of its own working within the premises of its parent layoutmanager. 9 10 Containment Hierarchy CalculatorApplet 2 containers! 4 components! A layout-manager! per container! 11 12
CalculatorApplet Eclipse/handout BinaryOperatorButton and" Concrete Subclasses Separate paper 13 14 UML for Binary Operator Buttons MyCalculatorApplet1 Separate slide 15 16
MyCalculatorApplet2, Power, and a new Plus (MyPlus) Separate slide MyCalculatorApplet3 Separate slide 17 18 Sorting Comparable The problem of sorting is fundamental in computing as (very) large parts of what computers do today is to "sort". By sorting is meant the re-arrangement of the elements in a sequence so they occur in order. Sorting 3,5,1,2,4 results in 1,2,3,4,5. All sequences containing comparable elements can be sorted. In Java, a sequence can be an array or vector, for example. 19 20
Example: Mergesort Mergesort is a algorithm to sort based on divide and conquer. Sorts lists of comparable items. Used (invented?) by John von Neumann to program ENIAC in 1945. Informally, Mergesort works as follows: 1. Divide the unsorted list into two sublists of about half the size. 2. Sort each of the two sublists. Use Mergesort twice here. 3. Merge the two sorted sublists back into one sorted list, and we are done. 21 22 mergesort in Eclipse 23