Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd.

Similar presentations


Presentation on theme: "Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd."— Presentation transcript:

1 Chapter Day 25

2 © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd Progress report OverDue Only got two  Capstones Due Dec 14 at 3 PM Quiz 3 (last one) on Dec 8 Today we will discuss more GUI components and containers along with recursion

3 Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

4 Outline Exception Handling The try-catch Statement Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

5 © 2007 Pearson Addison-Wesley. All rights reserved Combo Boxes A combo box provides a menu from which the user can choose one of several options The currently selected option is shown in the combo box A combo box shows its options only when the user presses it using the mouse Options can be established using an array of strings or using the addItem method

6 © 2007 Pearson Addison-Wesley. All rights reserved The JukeBox Program A combo box generates an action event when the user makes a selection from it See JukeBox.java (page 559)JukeBox.java See JukeBoxControls.java (page 560)JukeBoxControls.java

7 © 2007 Pearson Addison-Wesley. All rights reserved Outline Exception Handling The try-catch Statement Exception Classes I/O Exceptions Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes

8 © 2007 Pearson Addison-Wesley. All rights reserved Scroll Panes A scroll pane is useful for images or information too large to fit in a reasonably-sized area A scroll pane offers a limited view of the component it contains It provides vertical and/or horizontal scroll bars that allow the user to scroll to other areas of the component No event listener is needed for a scroll pane See TransitMap.java (page 564) TransitMap.java

9 © 2007 Pearson Addison-Wesley. All rights reserved Split Panes A split pane ( JSplitPane ) is a container that displays two components separated by a moveable divider bar The two components can be displayed side by side, or one on top of the other Moveable Divider Bar Left Component Right Component Top Component Bottom Component

10 © 2007 Pearson Addison-Wesley. All rights reserved Split Panes The orientation of the split pane is set using the HORIZONTAL_SPLIT or VERTICAL_SPLIT constants The divider bar can be set so that it can be fully expanded with one click of the mouse The components can be continuously adjusted as the divider bar is moved, or wait until it stops moving Split panes can be nested

11 © 2007 Pearson Addison-Wesley. All rights reserved Lists The Swing Jlist class represents a list of items from which the user can choose The contents of a JList object can be specified using an array of objects A JList object generates a list selection event when the current selection changes See PickImage.java (page 568) PickImage.java See ListPanel.java (page 570) ListPanel.java

12 © 2007 Pearson Addison-Wesley. All rights reserved Lists A JList object can be set so that multiple items can be selected at the same time The list selection mode can be one of three options:  single selection – only one item can be selected at a time  single interval selection – multiple, contiguous items can be selected at a time  multiple interval selection – any combination of items can be selected The list selection mode is defined by a ListSelectionModel object

13 © 2007 Pearson Addison-Wesley. All rights reserved Summary Chapter 10 has focused on: the purpose of exceptions exception messages the try-catch statement propagating exceptions the exception class hierarchy GUI mnemonics and tool tips more GUI components and containers

14 Chapter 11 Recursion 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

15 Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems Chapter 11 focuses on:  thinking in a recursive manner  programming in a recursive manner  the correct use of recursion  recursion examples

16 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

17 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself  GNU means “GNU is Not Unix” When defining an English word, a recursive definition is often not helpful But in other situations, a recursive definition can be an appropriate way to express a concept Before applying recursion to programming, it is best to practice thinking recursively

18 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Definitions Consider the following list of numbers: 24, 88, 40, 37 Such a list can be defined as follows: A LIST is a: number or a: number comma LIST That is, a LIST is defined to be a single number, or a number followed by a comma followed by a LIST The concept of a LIST is used to define itself

19 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Definitions The recursive part of the LIST definition is used several times, terminating with the non-recursive part: number comma LIST 24, 88, 40, 37 number comma LIST 88, 40, 37 number comma LIST 40, 37 number 37

20 © 2007 Pearson Addison-Wesley. All rights reserved Infinite Recursion All recursive definitions have to have a non- recursive part If they didn't, there would be no way to terminate the recursive path Such a definition would cause infinite recursion  The definition of GNU is infinite recursion This problem is similar to an infinite loop, but the non-terminating "loop" is part of the definition itself The non-recursive part is often called the base case

21 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Definitions N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! A factorial is defined in terms of another factorial Eventually, the base case of 1! is reached

22 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Definitions 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 2 6 24 120

23 © 2007 Pearson Addison-Wesley. All rights reserved Fibonacci Number Series A very famous recursively defined numbers series  F(0) = 0  F(1) = 1  F (2) = F(0)+F(1)  F(N) = F(N-1) + F(N-2) 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 91 F(10) = F(9) + F(8) The Fibonacci numbers appear with surprising regularity in nature  http://britton.disted.camosun.bc.ca/fibslide/jbfibslide.htm http://britton.disted.camosun.bc.ca/fibslide/jbfibslide.htm Pseudo Code FIB(N) { if N = 0 || N=1 return N else return (FIB (N-1) + FIB(N-2)) }

24 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

25 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Programming A method in Java can invoke itself; if set up that way, it is called a recursive method The code of a recursive method must be structured to handle both the base case and the recursive case Each call to the method sets up a new execution environment, with new parameters and local variables As with any method call, when the method completes, control returns to the method that invoked it (which may be an earlier invocation of itself)

26 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as:

27 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Programming // This method returns the sum of 1 to num public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; }

28 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6

29 © 2007 Pearson Addison-Wesley. All rights reserved Recursive Programming Note that just because we can use recursion to solve a problem, doesn't mean we should For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version You must carefully decide whether recursion is the correct technique for any problem

30 © 2007 Pearson Addison-Wesley. All rights reserved Indirect Recursion A method invoking itself is considered to be direct recursion A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again This is called indirect recursion, and requires all the same care as direct recursion It is often more difficult to trace and debug

31 © 2007 Pearson Addison-Wesley. All rights reserved Indirect Recursion m1m2m3 m1m2m3 m1m2m3

32 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

33 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi The Towers of Hanoi is a puzzle made up of three vertical pegs and several disks that slide on the pegs The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly smaller ones on top The goal is to move all of the disks from one peg to another under the following rules:  We can move only one disk at a time  We cannot move a larger disk on top of a smaller one

34 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi Original ConfigurationMove 1Move 3Move 2

35 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi Move 4Move 5Move 6Move 7 (done)

36 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi An iterative solution to the Towers of Hanoi is quite complex A recursive solution is much shorter and more elegant See SolveTowers.java (page 594) SolveTowers.java See TowersOfHanoi.java (page 595) TowersOfHanoi.java

37 © 2007 Pearson Addison-Wesley. All rights reserved Maze Traversal We can use recursion to find a path through a maze From each location, we can search in each direction Recursion keeps track of the path through the maze The base case is an invalid move or reaching the final destination See MazeSearch.java (page 587) MazeSearch.java See Maze.java (page 588) Maze.java

38 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

39 © 2007 Pearson Addison-Wesley. All rights reserved Tiled Pictures Consider the task of repeatedly displaying a set of images in a mosaic  Three quadrants contain individual images  Upper-left quadrant repeats pattern The base case is reached when the area for the images shrinks to a certain size See TiledPictures.java (page 598) TiledPictures.java

40 © 2007 Pearson Addison-Wesley. All rights reserved Tiled Pictures

41 © 2007 Pearson Addison-Wesley. All rights reserved Fractals A fractal is a geometric shape made up of the same pattern repeated in different sizes and orientations The Koch Snowflake is a particular fractal that begins with an equilateral triangle To get a higher order of the fractal, the sides of the triangle are replaced with angled line segments See KochSnowflake.java (page 601) KochSnowflake.java See KochPanel.java (page 604) KochPanel.java

42 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes Becomes

43 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes

44 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes

45 © 2007 Pearson Addison-Wesley. All rights reserved Summary Chapter 11 has focused on:  thinking in a recursive manner  programming in a recursive manner  the correct use of recursion  recursion examples


Download ppt "Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Capstones Schedule  3rd."

Similar presentations


Ads by Google