© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

Interfaces A Java interface is a collection
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 6 Object-Oriented Design 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Chapter Day 16. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 16 Problem set 3 posted  10 problems from chapters 5 & 6  Due in one.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Miscellaneous Java … a collection of short topics that we need to get to at some point …
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Chapter Day 15. © 2007 Pearson Addison-Wesley. All rights reserved6-2 Agenda Day 15 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 7.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 6 Object-Oriented Design Part 3. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Outline Enumerated Types Revisited Method Design Testing.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Programming in Java (COP 2250) Lecture 14 & 15 Chengyong Yang Fall, 2005.
ELC 312 DAY 11 & 12. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Agenda Questions? Problem set 2 Corrected  2 A’s and 2 B’s Problem set 3 Posted.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Object-Oriented Design. Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Object-Oriented Design Part 2
Chapter 4: Writing Classes
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Mystery Recursion on HW8 Any questions?

© 2004 Pearson Addison-Wesley. All rights reserved Think of recursion as a tree …

© 2004 Pearson Addison-Wesley. All rights reserved … an upside down tree

© 2004 Pearson Addison-Wesley. All rights reserved

(a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4)

© 2004 Pearson Addison-Wesley. All rights reserved (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) 2 m=(a+b)/2 => print 2

© 2004 Pearson Addison-Wesley. All rights reserved (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4)

© 2004 Pearson Addison-Wesley. All rights reserved (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) Traversal Order

© 2004 Pearson Addison-Wesley. All rights reserved Example: Recursion_Debug.java

© 2004 Pearson Addison-Wesley. All rights reserved Quick Review of Last Lecture

© 2004 Pearson Addison-Wesley. All rights reserved The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference is used in a method called tryMe, which is invoked as follows: obj1.tryMe(); obj2.tryMe(); In the first invocation, the this reference refers to obj1 ; in the second it refers to obj2

© 2004 Pearson Addison-Wesley. All rights reserved The this reference The this reference can be used to distinguish the instance variables of a class from corresponding method parameters with the same names The constructor of the Account class (from Chapter 4) could have been written as follows: public Account (Sring name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; }

© 2004 Pearson Addison-Wesley. All rights reserved The this reference public Account (Sring name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; } public Account (Sring owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; }

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish a set of methods that a class will implement

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by:  stating so in the class header  providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces A class that implements an interface can implement other methods as well See Complexity.java (page 310)Complexity.java See Question.java (page 311)Question.java See MiniQuiz.java (page 313)MiniQuiz.java In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

© 2004 Pearson Addison-Wesley. All rights reserved Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareTo, which is used to compare two objects We discussed the compareTo method of the String class in Chapter 5 The String class implements Comparable, giving us the ability to put strings in lexicographic order

© 2004 Pearson Addison-Wesley. All rights reserved Where ca you find the standard Java interfaces C:\Program Files\Java\jdk1.5.0\src.zip

© 2004 Pearson Addison-Wesley. All rights reserved Interface Example: Sortable.java SortableIntArray.java SortableStrigArray.java SortingTest.java

Chapter 6 Section 6.6

© 2004 Pearson Addison-Wesley. All rights reserved Enumerated Types (read Section 6.6 on your own)

Chapter 6 Section 6.7

© 2004 Pearson Addison-Wesley. All rights reserved Passing Arguments Another important issue related to method design involves parameter passing Parameters in a Java method are passed by value A copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Therefore passing parameters is similar to an assignment statement

© 2004 Pearson Addison-Wesley. All rights reserved Passing Arguments Always done using “Pass By Value”

© 2004 Pearson Addison-Wesley. All rights reserved Example: PassByValue.java

© 2004 Pearson Addison-Wesley. All rights reserved Objects as Parameters When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

© 2004 Pearson Addison-Wesley. All rights reserved Passing Objects to Methods What a method does with a parameter may or may not have a permanent effect (outside the method) See ParameterTester.java (page 327)ParameterTester.java See ParameterModifier.java (page 329)ParameterModifier.java See Num.java (page 330)Num.java Note the difference between changing the internal state of an object versus changing which object a reference points to

© 2004 Pearson Addison-Wesley. All rights reserved Figure 6.5

© 2004 Pearson Addison-Wesley. All rights reserved Method Design As we've discussed, high-level design issues include:  identifying primary classes and objects  assigning primary responsibilities After establishing high-level design issues, its important to address low-level issues such as the design of key methods For some methods, careful planning is needed to make sure they contribute to an efficient and elegant system design

© 2004 Pearson Addison-Wesley. All rights reserved Method Design An algorithm is a step-by-step process for solving a problem Examples: a recipe, travel directions Every method implements an algorithm that determines how the method accomplishes its goals An algorithm may be expressed in pseudocode, a mixture of code statements and English that communicate the steps to take

© 2004 Pearson Addison-Wesley. All rights reserved Method Decomposition A method should be relatively small, so that it can be understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity A public service method of an object may call one or more private support methods to help it accomplish its goal Support methods might call other support methods if appropriate

© 2004 Pearson Addison-Wesley. All rights reserved Method Decomposition Let's look at an example that requires method decomposition – translating English into Pig Latin Pig Latin is a language in which each word is modified by moving the initial sound of the word to the end and adding "ay" Words that begin with vowels have the "yay" sound added on the end bookookbaytableabletay itemitemyaychairairchay

© 2004 Pearson Addison-Wesley. All rights reserved Method Decomposition The primary objective (translating a sentence) is too complicated for one method to accomplish Therefore we look for natural ways to decompose the solution into pieces Translating a sentence can be decomposed into the process of translating each word The process of translating a word can be separated into translating words that:  begin with vowels  begin with consonant blends (sh, cr, th, etc.)  begin with single consonants

© 2004 Pearson Addison-Wesley. All rights reserved Method Decomposition See PigLatin.java (page 320)PigLatin.java See PigLatinTranslator.java (page 323)PigLatinTranslator.java In a UML class diagram, the visibility of a variable or method can be shown using special characters Public members are preceded by a plus sign Private members are preceded by a minus sign

© 2004 Pearson Addison-Wesley. All rights reserved Class Diagram for Pig Latin PigLatin + main (args : String[]) : void + translate (sentence : String) : String - translateWord (word : String) : String - beginsWithVowel (word : String) : boolean - beginsWithBlend (word : String) : boolean PigLatinTranslator

Chapter 6 Section 6.8

© 2004 Pearson Addison-Wesley. All rights reserved Method Overloading Method overloading is the process of giving a single method name multiple definitions If a method is overloaded, the method name is not sufficient to determine which method is being called The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters

© 2004 Pearson Addison-Wesley. All rights reserved Method Overloading The compiler determines which method is being invoked by analyzing the parameters float tryMe(int x) { return x +.375; } float tryMe(int x, float y) { return x*y; } result = tryMe(25, 4.32) Invocation

© 2004 Pearson Addison-Wesley. All rights reserved Method Overloading The println method is overloaded: println (String s) println (int i) println (double d) and so on... The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

© 2004 Pearson Addison-Wesley. All rights reserved Overloading Methods The return type of the method is not part of the signature That is, overloaded methods cannot differ only by their return type Constructors can be overloaded Overloaded constructors provide multiple ways to initialize a new object

Chapter 6 Section 6.9

© 2004 Pearson Addison-Wesley. All rights reserved Testing (read Section 6.9 on your own)

© 2004 Pearson Addison-Wesley. All rights reserved Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes any evaluation performed by human or computer to assess quality Some evaluations should occur before coding even begins The earlier we find an problem, the easier and cheaper it is to fix

© 2004 Pearson Addison-Wesley. All rights reserved Testing The goal of testing is to find errors As we find and fix errors, we raise our confidence that a program will perform as intended We can never really be sure that all errors have been eliminated So when do we stop testing?  Conceptual answer: Never  Snide answer: When we run out of time  Better answer: When we are willing to risk that an undiscovered error still exists

© 2004 Pearson Addison-Wesley. All rights reserved Reviews A review is a meeting in which several people examine a design document or section of code It is a common and effective form of human-based testing Presenting a design or code to others:  makes us think more carefully about it  provides an outside perspective Reviews are sometimes called inspections or walkthroughs

© 2004 Pearson Addison-Wesley. All rights reserved Test Cases A test case is a set of input and user actions, coupled with the expected results Often test cases are organized formally into test suites which are stored and reused as needed For medium and large systems, testing must be a carefully managed process Many organizations have a separate Quality Assurance (QA) department to lead testing efforts

© 2004 Pearson Addison-Wesley. All rights reserved Defect and Regression Testing Defect testing is the execution of test cases to uncover errors The act of fixing an error may introduce new errors After fixing a set of errors we should perform regression testing – running previous test suites to ensure new errors haven't been introduced It is not possible to create test cases for all possible input and user actions Therefore we should design tests to maximize their ability to find problems

© 2004 Pearson Addison-Wesley. All rights reserved Black-Box Testing In black-box testing, test cases are developed without considering the internal logic They are based on the input and expected output Input can be organized into equivalence categories Two input values in the same equivalence category would produce similar results Therefore a good test suite will cover all equivalence categories and focus on the boundaries between categories

© 2004 Pearson Addison-Wesley. All rights reserved White-Box Testing White-box testing focuses on the internal structure of the code The goal is to ensure that every path through the code is tested Paths through the code are governed by any conditional or looping statements in a program A good testing effort will include both black-box and white-box tests

© 2004 Pearson Addison-Wesley. All rights reserved THE END