Chapter 14—Looking Ahead

Slides:



Advertisements
Similar presentations
Procedural Recursion Eric Roberts CS 106B April 15, 2009.
Advertisements

Recursive Strategies Eric Roberts CS 106B January 23, 2013.
Methods Java 5.1 A quick overview of methods
40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Sequences Lesson: SEQ-L3 Drawing Fractal Patterns Drawing Fractal Patterns Learning Outcome.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
RECURSION Recursion Towers of Hanoi To Recurse or to Loop? Fractals Do something!
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Chapter 4—Statement Forms The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Statement Forms C H A P T E R 4 The statements.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
1 Recursion. Objectives To define the concept of recursion as a programming strategy distinct from other forms of algorithmic decomposition. To recognize.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
Five-Minute Review 1.What are classes and objects? What is a class hierarchy? 2.What is an expression? A term? 3.What is a variable declaration? 4.What.
Five-Minute Review 1.What are the three data structures realized in the Java Collections Framework? 2.What is hashing used for, how does it work? 3.What.
Looking Ahead Eric Roberts CS 106A March 7, 2016.
Programming Paradigms, Software Architectural Patterns, and MVC CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 14 Recursion.
Five-Minute Review What are the three data structures realized in the Java Collections Framework? What are checked exceptions? Example? What do iterators.
Chapter Topics Chapter 16 discusses the following main topics:
COMP 51 Week Fourteen Recursion.
Algorithms and Problem Solving
Visit for more Learning Resources
Topic 6 Recursion.
Chapter 15 Recursion.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Lesson #6 Modular Programming and Functions.
Introduction To Repetition The for loop
Lesson #6 Modular Programming and Functions.
Chapter 15 Recursion.
Iterative Mathematics
Methods Chapter 6.
Chapter 19 Recursion.
Java 4/4/2017 Recursion.
Functions in C++ Eric Roberts CS 106B January 7, 2015.
( Iteration / Repetition / Looping )
Chapter 8: Recursion Java Software Solutions
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 8: Recursion Java Software Solutions
Recursion Chapter 18.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
slides courtesy of Eric Roberts
Multithreaded Programming
Lecture Notes – Week 4 Chapter 5 (Loops).
Chapter 11 Recursion.
Algorithms and Problem Solving
Chapter 8: Recursion Java Software Solutions
Lesson #6 Modular Programming and Functions.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Software Solutions Foundations of Program Design Sixth Edition
CMSC 202 Exceptions.
Presentation transcript:

Chapter 14—Looking Ahead The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java C H A P T E R 1 4 Looking Ahead The best way to predict the future is to invent it. —Alan Kay, Xerox Palo Alto Research Center, 1972 14.1 Recursion 14.2 Concurrency 14.3 Using the network 14.4 Programming patterns Chapter 14—Looking Ahead

Recursion This chapter offers a brief introduction to four topics that are sometimes included an introductory computer science course. The first of these topics is recursion, which is the process of solving a problem by dividing it into smaller subproblems of the same form. The italicized phrase is the essential characteristic of recursion; without it, all you have is a description of stepwise refinement as discussed in Chapter 5. The fact that recursive decomposition generates subproblems that have the same form as the original problem means that recursive programs will use the same method to solve subproblems at different levels of the solution. In terms of the structure of the code, the defining characteristic of recursion is having methods that call themselves, directly or indirectly, as the decomposition process proceeds.

A Simple Illustration of Recursion Suppose that you are the national fundraising director for a charitable organization and need to raise $1,000,000. One possible approach is to find a wealthy donor and ask for a single $1,000,000 contribution. The problem with that strategy is that individuals with the necessary combination of means and generosity are difficult to find. Donors are much more likely to make contributions in the $10 range. Another strategy would be to ask 100,000 friends for $10 each. Unfortunately, most of us don’t have 100,000 friends. There are, however, more promising strategies. You could, for example, find ten regional coordinators and charge each one with raising $100,000. Those regional coordinators could in turn delegate the task to local coordinators, each with a goal of $10,000, continuing the process reached a manageable contribution level.

A Simple Illustration of Recursion The following diagram illustrates the recursive strategy for raising $1,000,000 described on the previous slide: Goal: $1,000,000 Goal: $100,000 Goal: $10,000 Goal: $1000 Goal: $100

A Pseudocode Fundraising Strategy If you were to implement the fundraising strategy in the form of a Java method, it would look something like this: private void collectContributions(int n) { if (n <= 100) { Collect the money from a single donor. } else { Find 10 volunteers. Get each volunteer to collect n/10 dollars. Combine the money raised by the volunteers. } What makes this strategy recursive is that the line Get each volunteer to collect n/10 dollars. will be implemented using the following recursive call: collectContributions(n / 10);

Recursive Functions The easiest examples of recursion to understand are functions in which the recursion is clear from the definition. As an example, consider the factorial function from Chapter 5, which can be defined in either of the following ways: n! = n x (n - 1) x (n - 2) x . . . x 3 x 2 x 1 1 if n is 0 n! = n x (n - 1)! otherwise The second definition leads directly to the following code, which is shown in simulated execution on the next slide: private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); }

Simulating the factorial Method public void run() { int n = readInt("Enter n: "); println(n + "! = " + factorial(n) ); } private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 5 private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 4 120 n private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 3 5 private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 2 private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 1 24 private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n private int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } n 6 2 1 1 Factorial Enter n: 5 5! = 120 skip simulation

The Recursive “Leap of Faith” The purpose of going through the complete decomposition of the calculation of factorial(5) is to convince you that the process works and that recursive calls are in fact no different from other method calls, at least in their internal operation. The danger with going through these details is that it might encourage you to do the same when you write your own recursive programs. As it happens, tracing through the details of a recursive program almost always makes such programs harder to write. Writing recursive programs becomes natural only after you have enough confidence in the process that you don’t need to trace them fully. As you write a recursive program, it is important to believe that any recursive call will return the correct answer as long as the arguments define a simpler subproblem. Believing that to be true—even before you have completed the code—is called the recursive leap of faith.

The Recursive Paradigm Most recursive methods you encounter in an introductory course have bodies that fit the following general pattern: if (test for a simple case) { Compute and return the simple solution without using recursion. } else { Divide the problem into one or more subproblems that have the same form. Solve each of the problems by calling this method recursively. Return the solution from the results of the various subproblems. } Finding a recursive solution is mostly a matter of figuring out how to break it down so that it fits the paradigm. When you do so, you must do two things: Identify simple cases that can be solved without recursion. 1. Find a recursive decomposition that breaks each instance of the problem into simpler subproblems of the same type, which you can then solve by applying the method recursively. 2.

Exercise: A Recursive gcd Function Solution: A Recursive gcd Function In the discussion of algorithmic methods in Chapter 5, one of the primary examples was Euclid’s algorithm for computing the greatest common divisor of two integers, x and y. Euclid’s algorithm can be implemented using the following code: public int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; public int gcd(int x, int y) { if (y == 0) { return x; } else { return gcd(y, x % y); } Rewrite this method so that it uses recursion instead of iteration, taking advantage of Euclid’s insight that the greatest common divisor of x and y is also the greatest common divisor of the y and the remainder of x divided by y. As always, the key to solving this problem lies in identifying the recursive decomposition and defining appropriate simple cases.

Graphical Recursion Recursion comes up in certain graphical applications, most notably in the creation of fractals, which are mathematical structures that consist of similar figures repeated at various different scales. Fractals were popularized in a 1982 book by Benoit Mandelbrot entitled The Fractal Geometry of Nature. One of the simplest fractal patterns to draw is the Koch fractal, named after its inventor, the Swedish mathematician Helge von Koch (1870-1924). The Koch fractal is sometimes called a snowflake fractal because of the beautiful, six-sided symmetries it displays as the figure becomes more detailed. as illustrated in the following diagram:

Drawing Koch Fractals The process of drawing a Koch fractal begins with an equilateral triangle, as shown in the diagram on the lower left. order 1 From the initial position (which is called a fractal of order 0), each higher fractal order is created by replacing each line segment in the figure by four segments that connect the same endpoints but include an equilateral wedge in the middle. order 2 The figure on the previous slide is the Koch fractal of order 4. order 0

Simulating the Snowflake Program public void run() { SnowflakeFractal fractal = new SnowflakeFractal(100, 2) ; fractal.setFilled(true); fractal.setFillColor(Color.MAGENTA); add(fractal, getWidth() / 2, getHeight() / 2); } public SnowflakeFractal(double edge, int order) { addVertex(-edge / 2, -edge / (2 * Math.sqrt(3))); addFractalLine(edge, 0, order); addFractalLine(edge, -120, order); addFractalLine(edge, +120, order); this edge order 100.0 2 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); public void run() { SnowflakeFractal fractal = new SnowflakeFractal(100, 2) ; fractal.setFilled(true); fractal.setFillColor(Color.MAGENTA); add(fractal, getWidth() / 2, getHeight() / 2); } public SnowflakeFractal(double edge, int order) { addVertex(-edge / 2, -edge / (2 * Math.sqrt(3))); addFractalLine(edge, 0, order); addFractalLine(edge, -120, order); addFractalLine(edge, +120, order); } this edge order 100.0 2 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order 2 r 100.0 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order 1 r 33.33 fractal private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order r 11.11 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order –60 r 11.11 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order r 11.11 private void addFractalLine(double r, int theta, int order) { if (order == 0) { addPolarEdge(r, theta); } else { addFractalLine(r / 3, theta, order - 1); addFractalLine(r / 3, theta + 60, order - 1); addFractalLine(r / 3, theta - 60, order - 1); } this theta order 60 r 11.11 Snowflake skip simulation

Concurrency One of the initial design criteria for Java was that it be able to support concurrency, which is simply the ability to carry on several activities in parallel, even on computers that have only one processor. The classical approach to supporting concurrency is called multiprogramming, in which a computer with a single processor runs multiple programs by sharing that processor among each of the individual processes. The computer runs one process for a short period and then switches over to one of the other processes, cycling around the different tasks to ensure that they all get a fair share of processor time. Java supports concurrency at a lower level by allowing users to create new threads, which are independent activities that coexist when the same program and share access to the same memory space. As in multiprogramming, a multithreaded system shares the processor among the active threads.

A Simple Concurrent Application The TestAnimatedSquare program on the next slide offers a simple illustration of multithreaded programming. Each of the squares shown in the sample run below is an instance of the AnimatedSquare class, which implements the Runnable interface which allows it to support a thread. The run method for each square causes it to move in a random direction, which changes every 50 steps. TestAnimatedSquare

The TestAnimatedSquare Program /** * This program tests the AnimatedSquare class by putting two squares * on the screen and having them move independently. */ public class TestAnimatedSquare extends GraphicsProgram { public void run() { double x1 = getWidth() / 3 - SQUARE_SIZE / 2; double x2 = 2 * getWidth() / 3 - SQUARE_SIZE / 2; double y = (getHeight() - SQUARE_SIZE) / 2; AnimatedSquare redSquare = new AnimatedSquare(SQUARE_SIZE); redSquare.setFilled(true); redSquare.setColor(Color.RED); add(redSquare, x1, y); AnimatedSquare greenSquare = new AnimatedSquare(SQUARE_SIZE); greenSquare.setFilled(true); greenSquare.setColor(Color.GREEN); add(greenSquare, x2, y); Thread redSquareThread = new Thread(redSquare); Thread greenSquareThread = new Thread(greenSquare); waitForClick(); redSquareThread.start(); greenSquareThread.start(); } /* Private constants */ private static final double SQUARE_SIZE = 75;

The AnimatedSquare Class /** * This class creates an animated square that has its own thread of control. * Once started, the square moves in a random direction every time step. * After CHANGE_TIME time steps, the square picks a new random direction. */ public class AnimatedSquare extends GRect implements Runnable { /* Creates a new AnimatedSquare of the specified size */ public AnimatedSquare(double size) { super(size, size); } /* Runs when this object is started to animate the square */ public void run() { for (int t = 0; true; t++) { if (t % CHANGE_TIME == 0) { direction = rgen.nextDouble(0, 360); movePolar(DELTA, direction); pause(PAUSE_TIME); /* Private constants */ private static final double DELTA = 2; /* Pixels to move each cycle */ private static final int PAUSE_TIME = 20; /* Length of time step */ private static final int CHANGE_TIME = 50; /* Steps before changing direction */ /* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance(); private double direction;

Using the Network Because Java got its start during the early days of the World Wide Web, it is not at all surprising to find that its libraries have good support for networking, most of which is available through the classes in the java.net package. Although detailed coverage of the facilities in java.net is beyond the scope of this text, the following example shows how it is possible to open a reader on a web resource: public BufferedReader openNetworkReader(String prompt) { BufferedReader rd = null; while (rd == null) { try { URL url = new URL(readLine(prompt)); InputStream in = url.openStream(); rd = new BufferedReader(new InputStreamReader(in)); } catch (IOException ex) { println("Can't open that URL."); } return rd;

Programming Patterns In 1994, Addison-Wesley published Design Patterns, a groundbreaking book that called attention to the fact that many programming problems are most easily solved by applying certain common software patterns. Although the patterns described in the book are typically implemented using classes and methods, each pattern tends to represent more of a general solution strategy for some class of problems and not a ready-made solution.

The Model/View/Controller Pattern The user interacts with a MVC-based user interface through the controller, which is the part of the system capable of accepting user commands. It is typically a collection of Swing interactors. The controller never updates the displayed data directly. It instead sends requests to the model, which keeps track of the state of the system but is separate from the user interface. One of the most important patterns in terms of its applicability to user-interface design is the model/view/controller pattern, which is often abbreviated as MVC. When changes occur in the model, it sends messages to one or more views, which are responsible for updating the information that the user sees on the display. Controller Model View

The HousePoints Program The HousePoints program described in section 14.4 uses the model/view/controller pattern to present two views of the house points assigned to the Hogwarts houses at the end of J. K. Rowling’s Harry Potter and the Philosopher’s Stone. The first appears as a bar graph, the second as a pie chart. In this example, the role of the controller is taken by the interactors at the bottom of the window. The controller sends messages to the model, which in turn updates both views. HousePoints Points 48, 352, 426, 472 4, 352, 426, 472 312, 352, 426, 472 312, 352, 426, 472 482, 352, 426, 472 Graph

The End