Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 12-13: OOD and Recursion Yonglei Tao.

Similar presentations


Presentation on theme: "Ch 12-13: OOD and Recursion Yonglei Tao."— Presentation transcript:

1 Ch 12-13: OOD and Recursion Yonglei Tao

2 Object-Oriented Design
Identify classes Attributes Determine relationships Inheritance … Assign responsibilities Methods

3 Identify Classes A class represents some useful concept
Tangible or abstract Entities with multiple occurrences Information that we need to know Find classes by looking for nouns in requirements Not all classes can be discovered in analysis phase

4 Relationships between Classes
Inheritance Aggregation/Composition Dependency

5 Inheritance “Is-a”, generalization and specialization
public class Account { ... } public class SavingsAccount extends Account {

6 Aggregation/Composition
“Has a”, “whole and part” A class has references to objects of other classes as its instance variables A way of organizing objects as a group to hide complexity

7 Example public class PartA { private int x; public PartA ( int a ) { ... } public String toString ( ) { ... } } public class PartB { private float t; public PartB ( float b ) { ... }

8 Example (Cont.) public class Whole { private partA p; private partB q; public Whole ( partA a, partB b ) { p = a; q = b; } public String toString () { return p.toString() + q.toString();

9 Example (Cont.) public class Whole { private partA p; private partB q; public Whole ( int a, float b ) { p = new PartA (a); q = new partB (b); } public String toString () { return p.toString() + q.toString();

10 Dependency “Uses” Aggregation is a stronger form of dependency

11 Example public class TravelAsistant { public void bar ( Country s ) {
public void bar ( Country s ) { Country dest = s; System.out.println ( dest.getArea() ); } public void foo () { Country dest = new Country (“Belgium”, 30510));

12 Case Study: Printing an Invoice — Requirements
Print out an invoice that describes the charges for a set of products in certain quantities Omit complexities Dates, taxes, and invoice and customer numbers Print invoice Billing address, all line items, amount due Line item Description, unit price, quantity ordered, total price For simplicity, do not provide a user interface

13 A Sample Invoice

14 Nouns are possible classes
Discover Classes Nouns are possible classes Invoice Address LineItem Product Description Price Quantity Total AmountDue

15 Discover Classes (Cont.)
Analyze classes Invoice Address LineItem // Records the product and quantity Product Description // variable of the Product class Price // variable of the Product class Quantity // Not an attribute of a Product Total // Computed – not stored anywhere Amount Due // Computed – not stored anywhere Classes after a process of elimination Invoice Address LineItem Product

16 Printing an Invoice — UML Diagrams

17 Method Documentation — Invoice Class
// Describes an invoice for a set of purchased products public class Invoice { /** Adds a charge for a product to this invoice @param aProduct the product the customer ordered @param quantity the quantity of the product */ public void add(Product aProduct, int quantity){ } /** Formats the invoice @return the formatted invoice */ public String format() { } }

18 Method Documentation – LineItem Class
// Describes a quantity of an item and its price public class LineItem { /** Computes the total cost of this line item @return the total price */ public double getTotalPrice() { } /** Formats this item @return a formatted string of this line item */ public String format() { } }

19 Method Documentation — Product Class
// A product with a description and a price public class Product { /** Gets the product description @return the description */ public String getDescription() { } /** Gets the product price @return the unit price */ public double getPrice() { } }

20 Method Documentation — Address Class
/** Describes a mailing address. */ public class Address { /** Formats the address @return the address as a string with three lines */ public String format() { } }

21 Accessibility for Classes
Public Accessible from all packages Package (no access modifier) Accessible in the same package

22 Accessibility for Methods of Class A
Public Accessible to classes in all packages through a reference to an A Private Accessible inside class A - only to A’s methods Package (no access modifier) Accessible to classes in the same package via a reference to an A Protected Accessible to classes in the same package, and also subclasses of A everywhere

23 Recursive Methods A method that is either directly or indirectly makes a call to itself An example – the factorial function n! = 1 x 2 x 3 … x n where n >= 0 Its recursive definition 1! = 1 n! = n x (n-1)!

24 A Recursive Method public long factorial (int n) { if ( n <= 1) return 1; else return n * factorial(n-1); }

25 The Fibonacci Sequence
A sequence of numbers defined as follows f1 = 1 f2 = 1 fn = fn-1 + fn-2 First ten terms 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

26 Recursive Version /** Computes a Fibonacci number. @param n an integer
@return the nth Fibonacci number */ public long fib(int n) { if (n <= 2) return 1; else return fib(n - 1) + fib(n - 2); }

27 Call Tree for fib(6)                                                                                                                                                    

28 Iterative Version /** Computes a Fibonacci number. @param n an integer
@return the nth Fibonacci number */ public long fib(int n) { if (n <= 2) { return 1; } long olderValue = 1; long oldValue = 1; long newValue = 1; for (int i = 3; i <= n; i++) { newValue = oldValue + olderValue; olderValue = oldValue; oldValue = newValue; } return newValue;

29 Tail Recursion Create a method that displays all digits in an integer value in reverse order public void reverse (int num) { if (num > 0) System.out.print(num%10); num = num / 10; reverse(num); } System.out.println();

30 Tail Recursion (Cont.) public void reverse (int num) {
while (num > 0) System.out.print(num%10); num = num / 10; } System.out.println();

31 Palindrome Words Ada Anna Civic Kayak Leval Madam Mom Noon

32 Check if a String is a Palindrome
public boolean isPalindrome(String s) { s = s.toUpperCase(); if (s.length() <= 1) return true; else if (s.charAt(0) == s.charAt(s.length() - 1)) return isPalindrome(s.substring(1, s.length()-1)); else return false; }

33 An Iterative Version String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); original = in.nextLine(); for ( int i = original.length() - 1; i >= 0; i-- ) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Entered string is not a palindrome.");

34 Discussion How to find the largest in an array recursively?
public class ScoreSheet { int[] scores; // constructors and some methods public int getMax() { return getMax(0, scores.length - 1); } private int getMax(int first, int last) { // ?


Download ppt "Ch 12-13: OOD and Recursion Yonglei Tao."

Similar presentations


Ads by Google