Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 SJSU Dept. of Computer Science Fall 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/ http://netbeans.org/

3 SJSU Dept. of Computer Science Fall 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. _

4 SJSU Dept. of Computer Science Fall 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 ron.mak@sjsu.eduron.mak@sjsu.edu  CC all your team members. Subject: CS 151 Assignment #3 team name

5 SJSU Dept. of Computer Science Fall 2013: September 24 CS 151: Object-Oriented Design © R. Mak 5 Review Quiz #1

6 SJSU Dept. of Computer Science Fall 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. _

7 SJSU Dept. of Computer Science Fall 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() {... }... }

8 SJSU Dept. of Computer Science Fall 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; }

9 SJSU Dept. of Computer Science Fall 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

10 SJSU Dept. of Computer Science Fall 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.

11 SJSU Dept. of Computer Science Fall 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 _

12 SJSU Dept. of Computer Science Fall 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 _

13 SJSU Dept. of Computer Science Fall 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 subclass as 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.

14 SJSU Dept. of Computer Science Fall 2013: September 24 CS 151: Object-Oriented Design © R. Mak 14 Factory Method Example ComputerPlayer Throw Calculator SmartThrowRandomThrowGeniusThrow 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,...);

15 SJSU Dept. of Computer Science Fall 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 Throw Calculator SmartThrowRandomThrowGeniusThrow ThrowCalculator calc = ThrowCalculator.makeCalculator(type,...); Throw t = calc.calculateThrow(...);

16 SJSU Dept. of Computer Science Fall 2013: September 24 CS 151: Object-Oriented Design © R. Mak 16 A Class Hierarchy Puzzle Animal LionDogPiranhaGoldfishParrotHummingbird MammalFishBird  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”

17 SJSU Dept. of Computer Science Fall 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter

18 SJSU Dept. of Computer Science Fall 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird

19 SJSU Dept. of Computer Science Fall 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter

20 SJSU Dept. of Computer Science Fall 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 _

21 SJSU Dept. of Computer Science Fall 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!");

22 SJSU Dept. of Computer Science Fall 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. _

23 SJSU Dept. of Computer Science Fall 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"));

24 SJSU Dept. of Computer Science Fall 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 type are automatically public. public class Goldfish extends Fish implements HouseholdPet { public void feed(...) { // code to feed a goldfish }... }

25 SJSU Dept. of Computer Science Fall 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.

26 SJSU Dept. of Computer Science Fall 2013: September 24 CS 151: Object-Oriented Design © R. Mak 26 The Icon Interface Type

27 SJSU Dept. of Computer Science Fall 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... }

28 SJSU Dept. of Computer Science Fall 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 the Icon interface Interface type

29 SJSU Dept. of Computer Science Fall 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(...);


Download ppt "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."

Similar presentations


Ads by Google