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
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.
UML Package Diagrams. package_name presentation view controller model.
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
James Tam Object-Oriented Design Approaches to object-oriented design Some principles for good design Benefits and Drawbacks of the Object-Oriented Approach.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
James Tam Classes And Objects Part III Relationships between classes: Inheritance Access modifiers: Public, private, protected Interfaces: Types Vs. Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Writing Classes (Chapter 4)
Lecture 9 Polymorphism Richard Gesick.
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Programming With Java ICS Chapter 8 Polymorphism.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Session 7 Methods Strings Constructors this Inheritance.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
The need for Programming Languages
Intro To Classes Review
CS139 – Fall 2010 How far we have come
Today’s topics UML Diagramming review of terms
Applying OO Concepts Using Java
Advanced Java Programming
Advanced Java Programming
Review for Midterm 3.
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 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 many changes consider substituting 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) The full examples can be found in UNIX under: /home/courses/219/examples/designTesting (StringExample.java and StringBufferExample.java) public class StringExample { public static void main (String [] args) { String s = "0"; for (int i = 1; i < ; i++) s = s + i; } public class StringBufferExample { public static void main (String [] args) { StringBuffer s = new StringBuffer("0"); for (int i = 1; i < ; i++) s = s.append(i); }

James Tam 4. Be Cautious In The Use Of References Similar pitfall to using global variables: #include int i; fun () { for (i = 0; i < 100; i++) { printf("foo"); } } main () { i = 10; fun (); }

James Tam 4. Be Cautious In The Use Of References (2) public 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) public 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 The full example can be found in UNIX under: /home/courses/219/examples/designTesting/firstVersion public 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 { public static final int MIN = 0; public static final int MAX = 10; 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 >= MIN) && (newRating <= MAX)) 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 The full example can be found in UNIX under: /home/courses/219/examples/designTesting/secondVersion (All mutator methods now have private access). public 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 The full example can be found in UNIX under: / home/courses/219/examples/designTesting/thirdVersion public 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 5. Be Cautious When Writing Accessor And Mutator Methods When choosing a type for an attribute it comes down to tradeoffs, what are the advantages and disadvantages of using a particular type. In the previous examples: –Using mutable types (e.g., StringBuffer) provides a speed advantage. –Using immutable types (e.g., String) provides additional security

James Tam Unit Testing Place objects in a know state Send a message to the object Compare the resulting state vs. the expected stated

James Tam Testing Programs There are many available, some of them for free (and many commercial ones are available for free on a trial basis): –JUnit: or –JTest:

James Tam Some specialized testing classes must be downloaded. The location of the downloaded test classes must be either permanently set for the operating system (the ‘ classpath ’ variable) or at runtime: >javac -classpath. ; c:/junit jar SubscriptionTest.java Typically they test if a method’s operations are correct via an assertion (assert if a method is correct: true/false). How The Test Programs Work The compressed jar file containing the testing classes is located in the C drive. The name of the jar file is “junit jar”. The name of the Driver class used to run the tests (the name of class being tested is “Subscription”. Must explicitly include the current working directory

James Tam Example Program To Be Tested A program that calculates the monthly cost of a subscription. Given information: the total price of the submission and the number of months that the subscription will last. Example from:

James Tam Class Subscription public class Subscription { private int price ; // subscription total price in euro-cent private int length ; // length of subscription in months public Subscription(int p, int n) { price = p ; length = n ; } public int pricePerMonth() { double r = price / (double) length ; return r ; } public void cancel() { length = 0 ; }

James Tam Testing A Class A ‘ Driver ’ class must be written. The purpose of this class is to test the methods of another class. The testing will occur by creating an instance of the class to be tested and putting it into a known state. A message will be sent to the object to put it into another state. An assert statement is used to determine if the new state is correct.

James Tam Setting Up The Driver Class import org.junit.* { }

James Tam Class SubscriptionTest import org.junit.* ; import static org.junit.Assert.* ; public class SubscriptionTest public void test_returnEuro() { System.out.println("Test if pricePerMonth returns Euro...") ; Subscription S = new Subscription(200,2) ; assertTrue(S.pricePerMonth() == 2.0) ; public void test_roundUp() { System.out.println("Test if pricePerMonth rounds up correctly...") ; Subscription S = new Subscription(200,3) ; assertTrue(S.pricePerMonth() == 0.67) ; }

James Tam Compiling The Program Compile the Subscription class as you normally would. Compiling the Driver class: >javac -classpath. ; c:/junit jar SubscriptionTest.java Running the test java -classpath.;c:/junit jar org.junit.runner.JUnitCore Subscription Test results FAILURES!!! Tests run: 2, Failures: 2 Running the Junit test classes Name of the class to be tested Both assertions failed (evaluated to false)

James Tam You Should Now Know Some general design principles –What constitutes a good or a bad design. How automated testing programs such as JUnit can be used to test the methods in your classes.