Chapter 6 Object-Oriented Design Part 3. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Outline Enumerated Types Revisited Method Design Testing.

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.
Enumerated Types Define a new data type and list all possible values: enum Season {winter, spring, summer, fall} Then the new type can be used to declare.
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 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
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.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
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 …
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.
Writing Classes (Chapter 4)
Chapter 6 Object-Oriented Design Part 3. © 2004 Pearson Addison-Wesley. All rights reserved 2/35 Outline Software Development Activities Identifying Classes.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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,
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 7 Object-Oriented Design Concepts
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.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
© 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
© 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.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
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.
COS 312 DAY 8 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Corrected – Great results Capstones proposals Over due – 2 received Capstone progress.
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 VII: Arrays.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Chapter 4: Writing Classes
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)
Overriding Methods & Class Hierarchies
Chapter 4: Writing Classes
Presentation transcript:

Chapter 6 Object-Oriented Design Part 3

© 2004 Pearson Addison-Wesley. All rights reserved6-2 Outline Enumerated Types Revisited Method Design Testing

© 2004 Pearson Addison-Wesley. All rights reserved6-3 Enumerated Types In Chapter 3 we introduced enumerated types, which define a new data type and list all possible values of that type enum Season {winter, spring, summer, fall} Once established, the new type can be used to declare variables Season time; // Note: no quotes around time. The only values this variable can be assigned are the ones established in the enum definition

© 2004 Pearson Addison-Wesley. All rights reserved6-4 Enumerated Types An enumerated type definition is a special kind of class The values of the enumerated type are or objects of that type For example, fall is an object of type Season Fall is ‘associated’ with a list of values. That's why the following assignment is valid time = Season.fall;

© 2004 Pearson Addison-Wesley. All rights reserved6-5 Enumerated Types An enumerated type definition can be more interesting than a simple list of values Because they are like classes, we can add additional instance data and methods We can define an enum constructor as well Each value listed for the enumerated type calls the constructor See Season.java (page 318)Season.java See SeasonTester.java (page 319)SeasonTester.java DO go through these routines.

© 2004 Pearson Addison-Wesley. All rights reserved6-6 // Season.java Author: Lewis/Loftus // Enumerates the values for Season. //******************************************************************** public enum Season //Note: this is NOT a class! You do NOT see ‘class’ here… { winter ("December through February"), // Note the syntax: the comma?? spring ("March through May"), summer ("June through August"), fall ("September through November"); // and ultimately the semicolon! // Note that this is a new ‘data type’ And we list all possible values of this type. // The only values of this type are winter, spring, summer, and fall. private String span; // // Constructor: Sets up each value with an associated string. // Season (String months) // Note the Constructor; kind of ‘like’ a Constructor. { span = months; } // // Returns the span message for this value. // public String getSpan() { return span; } } // end enum Season

© 2004 Pearson Addison-Wesley. All rights reserved6-7 //******************************************************************** // SeasonTester.java Author: Lewis/Loftus // // Demonstrates the use of a full enumerated type. //******************************************************************** public class SeasonTester { // // Iterates through the values of the Season enumerated type. // public static void main (String[] args) { for (Season time : Season.values()) // note: ‘time is a ‘variable’ of enum Season. This invokes enum types via Season.values(). Very neat! When we declare ‘time’, System.out.println (time + "\t" + time.getSpan()); }

© 2004 Pearson Addison-Wesley. All rights reserved6-8 Enumerated Types Every enumerated type contains a static method called values that returns a list of all possible values for that type  The list returned from values is an iterator, so a for loop can be used to process them easily (infer that there is a next() and a hasNext() method. An enumerated type cannot be instantiated outside of its own definition A carefully designed enumerated type provides a versatile and type-safe mechanism for managing data

© 2004 Pearson Addison-Wesley. All rights reserved6-9 Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing

© 2004 Pearson Addison-Wesley. All rights reserved6-10 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 reserved6-11 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 reserved6-12 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 reserved6-13 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 reserved6-14 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 reserved6-15 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 Know these visibility symbols: +. -, #. (# means ‘protected’ – later with inheritance)

© 2004 Pearson Addison-Wesley. All rights reserved6-16 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  Be able to explain everything on this UML! If you do NOT understand, ask me!

© 2004 Pearson Addison-Wesley. All rights reserved6-17  Objects as Parameters Another very important issue related to method design involves parameter passing Parameters in a Java method are passed by value. This is often referred to as Call 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 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 reserved6-18 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 reserved6-19 Bottom Line – Know This. The whole thing about Call by Value (true for all languages that do this: C, COBOL (calls theirs Call by Content), Java, etc.) is that only the values of the variables are copied into the format parameters of the invoked methods.  So, once values are passed, whatever the called method does with them WILL NOT affect the values in the calling method at all! When we ‘return,’ the original values are unchanged. BUT: Objects and other structures are called by what we say is Call by Reference. In this case, only the reference (address) of the item is passed. So, the calling method and the called method are actually looking at the same item in the computer’s memory. Thus, anything changed in the called method to, say, an attribute in a passed object, will have the same result in the calling reference.

© 2004 Pearson Addison-Wesley. All rights reserved6-20 // ParameterTester.java Author: Lewis/Loftus public class ParameterTester { // serve as actual parameters to the changeValues method. Prints their values before and after // calling the method. public static void main (String[] args) // main driver. { ParameterModifier modifier = new ParameterModifier(); int a1 = 111; // plain, old integer with a value Num a2 = new Num (222); // two objects, each with a value Num a3 = new Num (333); // “ // The integer will be passed ‘by value’; object: ‘by reference.’ System.out.println ("Before calling changeValues:"); System.out.println ("a1\ta2\ta3"); // merely a header System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); //returns value of int a1 plus executes the toString method of Num for a2 and a3 (since these are objects. No values changed yet. modifier.changeValues (a1, a2, a3); //invokes the changeValues method object, modifier. // passes the int, a1, by value; passes objects a2 and a3 by reference. System.out.println ("After calling changeValues:"); System.out.println ("a1\ta2\ta3"); System.out.println (a1 + "\t" + a2 + "\t" + a3 + "\n"); //note: value for a1 not changed. } // values for a2 and a3 ARE changed (as changed in the changeValues method) }

© 2004 Pearson Addison-Wesley. All rights reserved6-21 //******************************************************************** // ParameterModifier.java Author: Lewis/Loftus // // Demonstrates the effects of changing parameter values. //******************************************************************** public class ParameterModifier { // // Modifies the parameters, printing their values before and // after making the changes. // public void changeValues (int f1, Num f2, Num f3) { System.out.println ("Before changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); f1 = 999; f2.setValue(888); f3 = new Num (777); System.out.println ("After changing the values:"); System.out.println ("f1\tf2\tf3"); System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n"); } Prints out values of formal params (f2 and f3 use toString…) Discuss: integer; f2.setValue(888); f3? old f3?? Talk! And new values are: xxxxxx But when we ‘return’ to driver?? Note: f2 and a2 are aliases! f3 was unchanged here. so too a3.

© 2004 Pearson Addison-Wesley. All rights reserved6-22 // Num.java Author: Lewis/Loftus Represents a single integer as an object. //******************************************************************** public class Num { private int value; // // Sets up the new Num object, storing an initial value. // public Num (int update) { value = update; } // // Sets the stored value to the newly specified value. // public void setValue (int update) { value = update; } // // Returns the stored integer value as a string. // public String toString () { return value + ""; } } // end class Num

© 2004 Pearson Addison-Wesley. All rights reserved6-23  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 Method overloading is a very important concept!

© 2004 Pearson Addison-Wesley. All rights reserved6-24 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 reserved6-25 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 reserved6-26 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 This is very important to understand! We may have a class built to do op1 in different ways depending upon the data type for which op1 is to be instantiated. If Strings, we do it one way; if objects, perhaps some other way; if ints, some other way!

© 2004 Pearson Addison-Wesley. All rights reserved6-27 Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited Method Design Testing

© 2004 Pearson Addison-Wesley. All rights reserved6-28 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 reserved6-29 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 reserved6-30 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 reserved6-31  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 reserved6-32 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 reserved6-33  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 reserved6-34  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