cs 151: object-oriented design september 24 class meeting department of computer science san jose...

29
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu /~mak

Upload: tyrone-hunt

Post on 01-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

CS 151: Object-Oriented DesignSeptember 24 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2013Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

2

Assignment #3

Due Friday, October 4

First version of your Rock-Paper-Scissors game. Command line only. No GUI. The number of throws per match should be a command-line

argument when you start the application. At the start of each throw, the application should prompt the

human player for his or her choice (rock, paper, or scissors) which the human should enter, or the human can ask for the current score or a help message.

For each throw, the computer should compute its choice entirely by random.

Download and use the free NetBeans IDE: http://netbeans.org/

Page 3: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

3

Assignment #3

Apply the object-oriented principles that you’ve learned. Make your code reliable, robust, flexible, easy to maintain, etc. You will be building on this code base in future assignments.

new algorithms for the computer’s choice for each throw a graphical user interface (GUI) ...

Update your Functional and Design Specifications Before editing, do an “Accept Change” on all your changes to

the Functional Specification to create a new base document. Make sure “Track Changes” is on for both documents.

_

Page 4: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

4

Assignment #3

What to turn in A zip file containing all your Java source files in the src

subdirectory that NetBeans creates. Do not include the nbproject and test subdirectories. If your mailer rejects the zip file as an attachment, try renaming it

so that the suffix is something other than .zip , such as .zzz . Email the zip file and the updated Functional Specification and

Design Specification to [email protected] CC all your team members.

Subject: CS 151 Assignment #3 team name

Page 5: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

5

Review Quiz #1

Page 6: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

6

Unit Testing

Unit test: To test a single class in isolation.

Think about how to test a class when: Writing the design specification. Formulating postconditions and invariants.

Regression testing Make a collection of unit tests. Run all the tests whenever you make a code change.

Make sure your application did not lose functionality or “regress”. Make sure a bug fix did not introduce new bugs.

Programmers are much less reluctant to improve their code if they can run regression tests to validate their changes._

Page 7: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

7

JUnit

JUnit: A popular tool for unit testing Java classes. To test a class with JUnit:

Design a companion test class that contains test cases. Each test case is a method whose name starts with test. The test class must import junit.framework.* The test class must extend TestCase

Example: Test class for class Day:

import junit.framework.*;public class DayTest extends TestCase{ public void testAdd() { ... } public void testDaysBetween() { ... } . . .}

Page 8: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

8

JUnit

Each test case method executes some code and then tests a condition. Use assert to test the condition.

Example:

public void testAdd(){ Day d1 = new Day(1970, 1, 1); int n = 1000; Day d2 = d1.addDays(n);

assert d2.daysFrom(d1) == n;}

Page 9: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

9

JUnit Integration with NetBeans

NetBeans creates two subdirectories in your project directory: src for the Java source files of your application test for the test classes

Right-click a test class and select “Test File” NetBeans will run each test case method of the test class and

display a graph showing the number of tests that succeeded and the number of tests that failed.

NetBeans + Junit demo

Page 10: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

10

Design Patterns

A design pattern is A description of a problem. A solution that you can apply to many programming situations.

Design patterns show you how to build good software with good object-oriented design qualities. Design patterns are proven object-oriented experience.

Design patterns are not code, but are general solutions to design problems. You apply them to your specific application.

Design patterns are not invented – they’re discovered. Design patterns address how to manage change.

Page 11: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

11

Design Patterns

Design patterns give programmers a very high-level, short-cut vocabulary to discuss design issues. Independent of specific implementations

or programming languages. “We should use the factory method design pattern here.” “The decorator pattern will simplify this code.”

Each design pattern has A short name A brief description of the context A description of the problem that it solves A prescription for a solution

_

Page 12: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

12

Design Patterns

The building architect Christopher Alexander formulated over 250 patterns for architectural design. Co-authored A Pattern Language: Towns, Buildings,

Construction, published in 1977.

In 1995, four authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides published the classic software book Design Patterns. AKA “The Gang of Four” Original 23 design patterns

_

Page 13: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

13

The Factory Method Design Pattern

Context An application can instantiate (create an object from)

any one of several subclasses of a superclass.

Description You know that your application needs to

instantiate one of the subclasses. But you won’t know which subclass until run time.

You need to provide a means to instantiate the subclassas determined by the application at run time. Your code must have the flexibility to instantiate

and work with any of the subclasses.

Solution Design a factory method that will, based on its parameters,

create and return an object of the correct subclass type.

Page 14: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

14

Factory Method Example

ComputerPlayer

ThrowCalculator

SmartThrow RandomThrow GeniusThrow

public abstract class ThrowCalculator{ public static ThrowCalculator makeCalculator(int type, ...) { switch (type) { case RANDOM: return new RandomThrow(...); break; case SMART: return new SmartThrow(...); break; case GENIUS: return new GeniusThrow(...); break; } } public abstract Throw calculateThrow(...); // abstract method}

ThrowCalculator calc =

ThrowCalculator.makeCalculator(type, ...);

Page 15: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

15

Code to the Interface (or Superclass)

The object referenced by variable calc can be a RandomThrow, a SmartThrow, or a GeniusThrow. Which one is determined at run time by the value of type. t = calc.calculateThrow(...) doesn’t care.

What if you need to know the type of that object? Use the Java operator instanceof

ComputerPlayer

ThrowCalculator

SmartThrow RandomThrow GeniusThrow

ThrowCalculator calc = ThrowCalculator.makeCalculator(type, ...);

Throw t = calc.calculateThrow(...);

Page 16: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

16

A Class Hierarchy Puzzle

Animal

Lion Dog Piranha Goldfish Parrot Hummingbird

Mammal Fish Bird

Suppose we want to add the category HouseholdPet. Do we make it a superclass? Where does it belong in this class hierarchy?

What if we also want to add the category Biter? Java allows a class to inherit from at most one superclass. No “multiple inheritance”

Page 17: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

17

A Class Hierarchy Puzzle

Make HouseholdPet and Biter Java interfaces. A Java class can implement multiple interfaces.

Animal

Mammal Fish Bird

Lion Dog Piranha Goldfish Parrot Hummingbird

«interface»HouseholdPet

«interface»Biter

Page 18: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

18

Java Subclass

If a class C is a subclass of superclass S, then C “is a” S: Dog is a Mammal Dog is an Animal

_

Animal

Mammal Fish Bird

Lion Dog Piranha Goldfish Parrot Hummingbird

Page 19: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

19

Java Interface

If a class C implements interface N, then C “is a” N: Dog is a HouseholdPet Dog is a Biter

Animal

Mammal Fish Bird

Lion Dog Piranha Goldfish Parrot Hummingbird

«interface»HouseholdPet

«interface»Biter

Page 20: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

20

Java instanceof Operator

If the value of variable x is of type Dog, then the following conditionals are all true:

x instanceof Dog x instanceof Mammal x instanceof Animal x instanceof HouseholdPet x instanceof Biter

_

Page 21: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

21

The Icon Interface Type

Use the static showMessageDialog() method of class JOptionPane to display a simple message:

Note the default icon to the left._

JOptionPane.showMessageDialog(null, "Hello, World!");

Page 22: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

22

The Icon Interface Type

The full declaration of static method showMessageDialog() in class JOptionPane:

public static void showMessageDialog(Component parent, Object message, String title, int messageType, Icon anIcon)

Icon is an interface type._

Page 23: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

23

The Icon Interface Type

Class ImageIcon implements the Icon interface:

JOptionPane.showMessageDialog(null, "Hello, World!", "Message", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("globe.gif"));

Page 24: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

24

Java Interface Methods A Java interface can contain method headers

but no method implementations. If a class implements the interface, then it must provide an

implementation for each method header in the interface.public interface HouseholdPet{ void feed(...);}

public class Dog extends Mammal implements HouseholdPet{ public void feed(...) { // code to feed a dog } ...}

All methods of an interface typeare automatically public.

public class Goldfish extends Fish implements HouseholdPet{ public void feed(...) { // code to feed a goldfish } ...}

Page 25: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

25

The Icon Interface Type

Any class that implements the Icon interface will work with method showMessageDialog().

public interface Icon{ int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y);}

public static void showMessageDialog(Component parent, Object message, String title, int messageType, Icon anIcon)

Demo: Mars icon.

Page 26: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

26

The Icon Interface Type

Page 27: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

27

Java Interface Variables

An interface type cannot contain instance variables. Only a class type can have instance variables.

If an interface type definition declares any variables, they are automatically made public static final In other words, they’re constants. Any class that implements the interface shares the constants.

public interface SystemOps{ ... int ABORT_CODE = -999; // public static final constant ...}

Page 28: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

28

Objects and Interfaces

If class C implements interface N, then objects of class C can be assigned to variables of the interface type N.

public class MarsIcon implements Icon{ ...}

Icon anIcon = new MarsIcon(...);

Implements theIcon interface

Interfacetype

Page 29: CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak mak

SJSU Dept. of Computer ScienceFall 2013: September 24

CS 151: Object-Oriented Design© R. Mak

29

Objects and Interfaces

The type of an object is never an interface type. The type of a variable can be an interface type.

The value of such a variable is a reference to an object whose class implements the interface type.

Icon anIcon = new MarsIcon(...);