James Tam Object-Oriented Design And Software Testing In this section of notes you will learn about principles of good design as well how testing is an.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Inheritance Inheritance Reserved word protected Reserved word super
James Tam CPSC 233: Introduction to Classes and Objects, Part I Attributes and methods Creating new classes References: Dynamic memory allocation and.
James Tam Object-Oriented Design Approaches to object-oriented design Some principles for good design Benefits and Drawbacks of the Object-Oriented Approach.
James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
James Tam Object-Oriented Programming Relationships between classes: Inheritance Access modifiers: Public, private, protected Interfaces: Types Vs. Classes.
James Tam Classes And Objects Part III Relationships between classes: Inheritance Access modifiers: Public, private, protected Interfaces: Types Vs. Classes.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
James Tam Object-Oriented Design And Software Testing In this section of notes you will learn about principles of good design as well how testing is an.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Object-Oriented Principles in Java: Part I Encapsulation Information Hiding Implementation Hiding Creating Objects in Java Class attributes.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Writing Classes (Chapter 4)
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Programming With Java ICS Chapter 8 Polymorphism.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Session 7 Methods Strings Constructors this Inheritance.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
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.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Functions + Overloading + Scope
Chapter 10 Thinking in Objects
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Java Programming: Guided Learning with Early Objects
Control Statement Examples
CS139 – Fall 2010 How far we have come
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 9 Thinking in Objects
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Group Status Project Status.
Anatomy of a Method.
Chapter 9 Thinking in Objects
Chapter 6 Methods.
Applying OO Concepts Using Java
Advanced Java Programming
Advanced Java Programming
Presentation transcript:

James Tam Object-Oriented Design And Software Testing In this section of notes you will learn about principles of good design as well how testing is an important part of good design

James Tam Some Principles Of Good Design 1.Avoid going “method mad” 2.Keep an eye on your parameter lists 3.Minimize modifying immutable objects 4.Be cautious in the use of references 5.Be cautious when writing accessor and mutator methods 6.Consider where you declare local variables This list was partially derived from “Effective Java” by Joshua Bloch and is by no means complete. It is meant only as a starting point to get students thinking more about why a practice may be regarded as “good” or “bad” style.

James Tam 1. Avoid Going Method Mad There should be a reason for each method Creating too many methods makes a class difficult to understand, use and maintain A good approach is to check for redundancies that exist between different methods

James Tam 2. Keep An Eye On Your Parameter Lists Avoid long parameter lists Rule of thumb: Three parameters is the maximum Avoid distinguishing overloaded methods solely by the order of the parameters

James Tam 3. Minimize Modifying Immutable Objects Immutable objects Once instantiated they cannot change (all or nothing) e.g., String s = "hello"; s = s + " there";

James Tam 3. Minimize Modifying Immutable Objects (2) If you must make changes substitute immutable objects with mutable ones e.g., public class StringBuffer { public StringBuffer (String str); public StringBuffer append (String str); : : : : } For more information about this class

James Tam 3. Minimize Modifying Immutable Objects (3) class StringExample { public static void main (String [] args) { String s = "0"; for (int i = 1; i < 10000; i++) s = s + i; } class StringBufferExample { public static void main (String [] args) { StringBuffer s = new StringBuffer("0"); for (int i = 1; i < 10000; i++) s = s.append(i); }

James Tam 4. Be Cautious In The Use Of References Similar to global variables: program globalExample (output); var i : integer; procedure proc; begin for i:= 1 to 100 do; end; begin i := 10; proc; end.

James Tam 4. Be Cautious In The Use Of References (2) class Foo { private int num; public int getNum () { return num; } public void setNum (int newValue) { num = newValue; } }

James Tam 4. Be Cautious In The Use Of References (3) class Driver { public static void main (String [] argv) { Foo f1, f2; f1 = new Foo (); f1.setNum(1); f2 = f1; f2.setNum(2); System.out.println(f1.getNum()); System.out.println(f2.getNum()); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: First Version class Driver { public static void main (String [] args) { CreditInfo newAccount = new CreditInfo (10, "James Tam"); newAccount.setRating(0); System.out.println(newAccount); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: First Version (2) public class CreditInfo { private int rating; private StringBuffer name; public CreditInfo () { rating = 5; name = new StringBuffer("No name"); } public CreditInfo (int newRating, String newName) { rating = newRating; name = new StringBuffer(newName); } public int getRating () { return rating; }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: First Version (3) public void setRating (int newRating) { if ((newRating >= 0) && (newRating <= 10)) rating = newRating; } public StringBuffer getName () { return name; } public void setName (String newName) { name = new StringBuffer(newName); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: First Version (4) public String toString () { String s = new String (); s = s + "Name: "; if (name != null) { s = s + name.toString(); } s = s + "\n"; s = s + "Credit rating: " + rating + "\n"; return s; } }// End of class CreditInfo

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Second Version (All mutator methods now have private access). class Driver { public static void main (String [] args) { CreditInfo newAccount = new CreditInfo (10, "James Tam"); StringBuffer badGuyName; badGuyName = newAccount.getName(); badGuyName.delete(0, badGuyName.length()); badGuyName.append("Bad guy on the Internet"); System.out.println(newAccount); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Second Version (2) public class CreditInfo { private int rating; private StringBuffer name; public CreditInfo () { rating = 5; name = new StringBuffer("No name"); } public CreditInfo (int newRating, String newName) { rating = newRating; name = new StringBuffer(newName); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Second Version (3) public int getRating () { return rating; } private void setRating (int newRating) { if ((newRating >= 0) && (newRating <= 10)) rating = newRating; } public StringBuffer getName () { return name; } private void setName (String newName) { name = new StringBuffer(newName); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Second Version (4) public String toString () { String s = new String (); s = s + "Name: "; if (name != null) { s = s + name.toString(); } s = s + "\n"; s = s + "Credit rating: " + rating + "\n"; return s; }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Third Version class Driver { public static void main (String [] args) { CreditInfo newAccount = new CreditInfo (10, "James Tam"); String badGuyName; badGuyName = newAccount.getName(); badGuyName = badGuyName.replaceAll("James Tam", "Bad guy on the Internet"); System.out.println(badGuyName + "\n"); System.out.println(newAccount); }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Third Version (2) public class CreditInfo { private int rating; private String name; public CreditInfo () { rating = 5; name = "No name"; } public CreditInfo (int newRating, String newName) { rating = newRating; name = newName; } public int getRating () { return rating; }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Third Version (3) private void setRating (int newRating) { if ((newRating >= 0) && (newRating <= 10)) rating = newRating; } public String getName () { return name; } private void setName (String newName) { name = newName; }

James Tam 5. Be Cautious When Writing Accessor And Mutator Methods: Third Version (4) public String toString () { String s = new String (); s = s + "Name: "; if (name != null) { s = s + name; } s = s + "\n"; s = s + "Credit rating: " + rating + "\n"; return s; }

James Tam 6. Consider Where You Declare Local Variables First Approach: Declare all local variables at the beginning of a method: void methodName (..) { int num; char ch; : } Advantage: Putting all variable declarations in one place makes them easy to find

James Tam 6. Consider Where You Declare Local Variables (2) Second Approach: declare local variables only as they are needed void methodName (..) { int num; num = 10; : for (int i = 0; i < 10; i++) } Advantage: For long methods it can be hard to remember the declaration if all variables are declared at the beginning Reducing the scope of a variable may reduce logic errors

James Tam Object-Oriented Design And Testing Start by employing a top-down approach to design Start by determining the candidate classes in the system Outline a skeleton for candidate classes (methods are stubs) Implement each method one-at-a-time. Create test drivers for methods. Fix any bugs in these methods Add the working methods to the code for the class.

James Tam Determine The Candidate Classes Example: A utility company provides three types of utilities: 1.Electricity: B ill = No. of kilowatt hours used * $ Gas: Bill = No. of gigajoules used * $ Water a) Flat rate: $ (square footage of dwelling * $0.01) b) Metered rate: $1.00 * No. cubic of meters used

James Tam Determine The Candidate Classes (2) Some candidate classes ElectricityBill WaterBill GasBill

James Tam Skeleton For Class WaterBill public class WaterBill { private char billType; private double bill; public static final double RATE_PER_SQUARE_FOOT = 0.01; public static final double BASE_FLAT_RATE_VALUE = 10.0; public static final double RATE_PER_CUBIC_METER = 1.0; public WaterBill () { } : : :

James Tam Determining The Remaining Methods calculateBill () Utility program determineBillType () Water bill billType getSquareFootage () If (flatRate) squareFeet calculateFlatRate () squareFeetbill getCubicMetersUsed () If (metered) cubicMetersUsed calculateMeteredRate () cubicMetersUsed bill Methods of class WaterBill

James Tam Remaining Skeleton For Class WaterBill (2) public double calculateBill () { return 1.0;} public void determineBillType () { } public int getSquareFootage () { return 1; } public double calculateFlatRate (int squareFootage) { return 1.0; } public double getCubicMetersUsed () { return 1.0; } public double calculateMeteredRate (double cubicMetersUsed) { return 1.0; } public char getBillType () { return billType; }

James Tam Implementing The Bodies For The Methods 1.calculateBill 2.determineBillType 3.getSquareFootage 4.calculateFlatRate 5.getCubicMetersUsed 6.calculateMeteredRate

James Tam Body For Method CalculateBill public double calculateBill () { int squareFootage; double cubicMetersUsed; determineBillType(); if (billType == 'f') { squareFootage = getSquareFootage (); bill = calculateFlatRate (squareFootage); } else if (billType == 'm') { cubicMetersUsed = getCubicMetersUsed(); bill = calculateMeteredRate (cubicMetersUsed); } else System.out.println("Bill must be either based on a flat rate or metered."); return bill; }

James Tam Body For DetermineBillType public class WaterBill { : : : : public void determineBillType () { System.out.println("Please indicate the method of billing."); System.out.println("(f)lat rate"); System.out.println("(m)etered billing"); billType = (char) Console.in.readChar(); Console.in.readLine(); }

James Tam Creating A Driver To Test DetermineBillType class Driver { public static void main (String [] args) { WaterBill test = new WaterBill (); test.determineBillType (); bill = test.getBillType(); System.out.println(bill); }

James Tam Body For GetSquareFootage public class WaterBill { : : : : public int getSquareFootage () { int squareFootage; System.out.print("Enter square footage of dwelling: "); squareFootage = Console.in.readInt(); Console.in.readChar(); return squareFootage; }

James Tam Creating A Driver To Test GetSquareFootage class Driver { public static void main (String [] args) { WaterBill test = new WaterBill (); int squareFootage = test.getSquareFootage (); System.out.println(squareFootage); }

James Tam Body For CalculateFlatRate public class WaterBill { : : : : public double calculateFlatRate (int squareFootage) { double total; total = BASE_FLAT_RATE_VALUE + (squareFootage * RATE_PER_SQUARE_FOOT); return total; }

James Tam Creating A Driver For CalculateFlatRate class DriverCalculateFlatRate { public static void main (String [] args) { WaterBill test = new WaterBill (); double bill; int squareFootage; squareFootage = 0; bill = test.calculateFlatRate(squareFootage); if (bill != 10) System.out.println("Incorrect flat rate for 0 square feet"); else System.out.println("Flat rate okay for 0 square feet");

James Tam Creating A Driver For CalculateFlatRate (2) squareFootage = 1000; bill = test.calculateFlatRate(squareFootage); if (bill != 20) System.out.println("Incorrect flat rate for 1000 square feet"); else System.out.println("Flat rate okay for 1000 square feet"); } } // End of Driver

James Tam Body For GetCubicMetersUsed public class WaterBill { : : : : public double getCubicMetersUsed () { double cubicMetersUsed; System.out.print("Enter the number of cubic meters used: "); cubicMetersUsed = Console.in.readDouble(); Console.in.readChar(); return cubicMetersUsed; }

James Tam Creating A Driver To Test GetCubicMetersUsed class Driver { public static void main (String [] args) { WaterBill test = new WaterBill (); double cubicMeters = test.getCubicMetersUsed (); System.out.println(cubicMeters); }

James Tam Body For CalculateMeteredRate public double calculateMeteredRate (double cubicMetersUsed) { double total; total = cubicMetersUsed * RATE_PER_CUBIC_METER; return total; }

James Tam Driver For CalculateMeteredRate class DriverCalculateMeteredRate { public static void main (String [] args) { WaterBill water = new WaterBill (); double bill; double cubicMetersUsed; cubicMetersUsed = 0; bill = water.calculateMeteredRate(cubicMetersUsed); if (bill != 0 ) System.out.println("Incorrect metered rate for 0 cubic meters consumed."); else System.out.println("Metered rate for 0 cubic meters consumed is okay.");

James Tam Driver For CalculateMeteredRate (2) cubicMetersUsed = 100; bill = water.calculateMeteredRate(cubicMetersUsed); if (bill != 100 ) System.out.println("Incorrect metered rate for 100 cubic meters consumed."); else System.out.println("Metered rate for 100 cubic meters consumed is okay."); }

James Tam General Rule Of Thumb: Test Drivers Write a test driver class if you need to verify that a method does what it is supposed to do (determining if it is correct). e.g., When a method performs a calculation, if a method is getting input Benefits of writing test drivers: 1.Ensuring that you know precisely what your code is supposed to do. 2.Making code more robust (test it before adding it a code library).

James Tam You Should Now Know Some general design principles What constitutes a good or a bad design. How to write test drives and what are the benefits of using test drivers in your programs