complex objects how might you design a class called nestedrects of graphical objects that look like...

37
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: Like many graphical objects, takes 5 parameters: • x and y describing coordinates of upper left • width and height of outermost rectangle • canvas Spacing between rectangles is 4 pixels

Upload: leona-wright

Post on 29-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Complex objects

• How might you design a class called NestedRects of graphical objects that look like this?

• Requirements for the constructor:– Like many graphical objects, takes 5 parameters:

• x and y describing coordinates of upper left • width and height of outermost rectangle• canvas

– Spacing between rectangles is 4 pixels

Page 2: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Constructor for NestedRectspublic NestedRects (double x double y, double width, double height, DrawingCanvas canvas) {

new FramedRect(x, y, width, height, canvas);while (width >= 8 && height >= 8) {

width = width - 8;height = height - 8;x = x + 4;y = y + 4;new FramedRect(x, y, width, height, canvas);

}}

Page 3: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Making NestedRects Useful

• Say that we want NestedRects objects to behave much like other graphical objects?

• NestedRects class should define methods like– moveTo– removeFromCanvas

But our constructor just draws the object!

Need a way to keep track of entire collection of nested rectangles!

Page 4: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Challenges

• Need to keep track of the rectangles in the collection

• Instance variables for each of the rectangles won’t work:

FramedRect rectangle1, rectangle2;

We don’t know how many there will be until a user specifies parameters when constructing one!

Page 5: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

A Recursive Solution

• A recursive structure consists of– A base structure (the simplest form of the structure)– A way to describe complex structures in terms of

simpler structures of the same kind

• Let’s change the way we think about NestedRects– Rather than a series of FramedRects…– An outer FramedRect + a smaller NestedRects inside

Page 6: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

NestedRects: a recursive def’npublic class NestedRects {

private FramedRect outerRect; // outermost rectangleprivate NestedRects rest; // inner nested rects

public NestedRects(double x, double y, double width, double height,

DrawingCanvas canvas) {outerRect = new FramedRect(x, y, width, height, canvas);if (width >= 8 && height >= 8) {

rest = new NestedRects(x+4, y+4, width-8, height-8, canvas);

} else {rest = null; // nothing more to construct

}}

Page 7: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

// Move nested rects to (x, y)public void moveTo(double x, double y) {

outerRect.moveTo(x, y);if (rest != null) {

rest.moveTo(x+4, y+4);}

}

// Remove the nested rects from the canvaspublic void removeFromCanvas() {

outerRect.removeFromCanvas();if (rest != null) {

rest.removeFromCanvas();}

}}

Page 8: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Tracing the execution of new NestedRects( 50, 50, 19, 21, canvas);

Page 9: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many
Page 10: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many
Page 11: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Tracing the execution of moveTo( 84, 104 )

Page 12: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many
Page 13: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many
Page 14: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many
Page 15: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

A Better Recursive Solution?

• moveTo and removeFromCanvas require checking whether rest is null

• Missing check will cause program to crash

• Can we write NestedRects to avoid the check for null?

Page 16: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Two Kinds of NestedRects

• “Normal” recursive case– outerRect– rest

• A special “simplest” NestedRects: empty!

Define a new class, BaseRects, representing an empty collection of FramedRects

Page 17: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

A NestedRects Interface

Recursive and empty nested rectangles are different, but they’re still collections of nested rectangles

public interface NestedRectsInterface {// Move nested rectangles to (x, y)void moveTo(double x, double y);// Remove nested rectangles from canvasvoid removeFromCanvas();

}

Page 18: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

A Simple Base Class

public class BaseRects implements NestedRectsInterface {

// Constructor has nothing to initialize

public BaseRects() { }

// Move nested rectangles to (x, y)

public void moveTo(double x, double y) { }

// Remove nested rectangles from canvas

public void removeFromCanvas() { }

}

Page 19: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Revised Recursive Classpublic class NestedRects implements NestedRectsInterface {

private FramedRect outerRect; // outermost rectangleprivate NestedRectsInterface rest; // inner nested rects

public NestedRects(double x, double y, double width, double height,

DrawingCanvas canvas) {outerRect = new FramedRect(x, y, width, height, canvas);if (width >= 8 && height >= 8) {

rest = new NestedRects(x+4, y+4, width-8, height-8, canvas);

} else { // construct a base objectrest = new BaseRects();

}}

Page 20: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

// Move nested rects to (x, y)public void moveTo(double x, double y) {

outerRect.moveTo(x, y);rest.moveTo(x+4, y+4)

}

// Remove the nested rects from the canvaspublic void removeFromCanvas() {

outerRect.removeFromCanvas();rest.removeFromCanvas();

}}

Page 21: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Evaluating new NestedRects(54, 54, 11, 13, canvas)

Page 22: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

• Since objects of type BaseRects and NestedRects know how to “moveTo” and “removeFromCanvas” …

Checks for null are eliminated!

Page 23: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Data Collections as Recursive Structures: an Example

• Useful feature of some web browsers: automatic address completion

Type http://www.aBrowser responds with suggestionhttp://www.aol.com

• Browser needs a way to keep track of all URLs typed in by a user in order to provide the most useful suggestions

Page 24: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

List of URLs as a recursive structure

• What capabilities should a list of URLs have?• Define an interface:

public interface UrlListInterface {// Returns a list of all entries starting with prefixpublic UrlListInterface getmatches(String prefix);// Determines whether the collection contains urlpublic boolean contains(String url);//Convert list to a string; entries separated by new linespublic String toString();

}

Page 25: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Empty UrlList as a base classpublic class EmptyUrlList implements UrlListInterface {

public EmptyUrlList( ) { }

public UrlListInterface getMatches(String prefix) {return new EmptyUrlList( );

}

public boolean contains(String url) {return false;

}

public String toString() {return "";

}}

Page 26: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

A non-empty UrlListpublic class NonemptyUrlList implements UrlListInterface {

private String firstUrl; // The first URL in the listPrivate UrlListInterface rest; // The rest of the list of URLs

public NonemptyUrlList(String newURL, UrlListInterface existingList) {

firstUrl = newURL;rest = existingList

}

Page 27: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

public UrlListInterface getMatches(String prefix) {if ( firstUrl.startsWith(prefix) ) {

return new NonemptyUrlList(firstUrl,rest.getMatches(prefix));

} else {return rest.getMatches(prefix);

}}

public boolean contains(String url) {return firstUrl.equals( url ) || rest.contains( url );

}

public String toString() {// "\n" is a newline characterreturn firstUrl + "\n" + rest.toString( );

}

}

Page 28: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Designing recursive structures

Recursive structures built by defining classes for base and recursive cases– Both implement same interface– Base class

• No instance variable has same type as interface or class• Generally easy to write

– Recursive class• At least one instance variable has same type as interface of

class• Care needed to be sure methods terminate

Page 29: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Implementing recursive structures

• Define interface with methods that both base and recursive classes must implement

• Define one or more base classes– Convince yourself that constructor and methods work

properly

• Define constructors for recursive classes– Recursive calls of constructor only create objects

simpler than the one being built– Should eventually end up at a base class

• Write methods for recursive classes

Page 30: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Recursive Methods

• Can write recursive methods that are not part of recursive structures

• Complexity controlled by an int parameter– Recursive invocations simpler– Simpler = smaller, non-negative parameter

Page 31: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Base case replaces Base class

Recursive methods

• Must include at least one base case

• Typically contain a conditional statement– At least one case is a recursive invocation– At least one case is a base case -- i.e., no

recursive invocation of the method

Page 32: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

An example: Exponentiation• Inspiration: Fast algorithms for exponentiation

important to RSA algorithm for public key cryptography• A simple (not fast!) recursive method:

// returns base raised to exponent as long as exponent >=0public double simplePower(double base, int exponent) {

if (exponent == 0) {return 1;

} else {return base * simplePower(base, exponent-1);

}}

Page 33: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Rules for writing recursive methods

• Write the base case– No recursive call

• Write the recursive case– All recursive calls should go to simpler cases– Simpler cases must eventually reach base

case

Page 34: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Applying rules to simplePower

• Base case: exponent == 0– Returns 1– Correct answer for raising base to the 0th power– No recursive invocation

• Recursive case: uses else clause– Recursive call involves smaller value for exponent– Recursive calls eventually reach base case of 0:

exponent greater than 0 to start and always goes down by 1

Page 35: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Rules of Exponents

• Simple algorithm took advantage of these rules:– base0 = 1– baseexp+1 = base * baseexp

• New algorithm will make use of this rule:– Basem*n = (basem)n

Let m = 2, n = exp/2

Page 36: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Faster exponentiation

public double fastPower(double base, int exponent) {if (exponent == 0) {

return 1;} else if ( exponent%2 == 1 ) {

return base * fastPower(base, exponent-1);} else {

return fastPower(base* base, exponent/2);}

}

Page 37: Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many

Tracing fastPower

fastPower(3, 16) = fastPower(9, 8)= fastPower(81, 4)= fastPower(6561, 2)= fastPower(43046721, 1)= 43046721 * fastPower(43046721, 0)= 43046721 * 1= 43046721

Only 5 multiplications! Division by 2 is fast and easy for computers