Ch 12-13: OOD and Recursion Yonglei Tao.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Java Programming Strings Chapter 7.
C++ Classes & Data Abstraction
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Big Java Chapter 12. Software Process - Waterfall Analysis Design Implementation Testing Deployment Does not work well when rigidly applied! established.
1 Chapter 2 (Cont.) The BA’s Perspective on Object Orientation.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 12 – Object-Oriented Design.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Object Oriented Analysis and Design Class and Object Diagrams.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Slides by Rick Giles OBJECT- ORIENTED DESIGN CHAPTER 12.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
12 OBJECT-ORIENTED DESIGN CHAPTER
CSC 480 Software Engineering PSP Project 2 August 27, 2004.
C# Programming Methods.
Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,
Unified Modeling Language (UML)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 12 – Object-Oriented Design.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
5 Chapter 5: Modeling Systems Requirements: Events and Things Systems Analysis and Design in a Changing World.
Recursion Powerful Tool
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 12 – Object-Oriented Design
Objects as a programming concept
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Lecture 12 Inheritance.
Chapter 11 Object-Oriented Design
Chapter 19 Recursion.
5. Function (2) and Exercises
Lecture Notes – Basics (Ch1-6)
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design,
CS Week 14 Jim Williams, PhD.
Advanced Java Programming
Formatted and Unformatted Input/Output Functions
CSC 205 Java Programming II
Chapter 4 Unordered List.
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Object Oriented Analysis and Design
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Recursion Chapter 18.
CS2011 Introduction to Programming I Objects and Classes
Stack Frames and Functions
Module 1-10: Recursion.
Copyright 2007 Oxford Consulting, Ltd
Chapter 12 – Object-Oriented Design
Chapter 18 Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Recursion Method calling itself (circular definition)
Chapter 8 Inheritance Part 2.
Unified Modeling Language (UML)
2.1 Introduction to Object-Oriented Programming
ICS103 Programming in C Lecture 11: Recursive Functions
Types of Recursive Methods
Presentation transcript:

Ch 12-13: OOD and Recursion Yonglei Tao

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

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

Relationships between Classes Inheritance Aggregation/Composition Dependency

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

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

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

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();

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();

Dependency “Uses” Aggregation is a stronger form of dependency

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));

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

A Sample Invoice

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

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

Printing an Invoice — UML Diagrams

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() { } }

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() { } }

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() { } }

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() { } }

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

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

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)!

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

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

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); }

Call Tree for fib(6)                                                                                                                                                    

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;

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();

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

Palindrome Words Ada Anna Civic Kayak Leval Madam Mom Noon …

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; }

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.");

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) { // ?