CS 151 ObjectOriented Design October 3 Class Meeting
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Assignment #4 o Due Friday, October 25. o Human players of the Rock Paper Scissors game generally do not make random choices. n n Humans try to develop strategies to beat the opponent. Humans exhibit patterns that a computer can exploit. o Continually record the last N choices between the human and the computer player. For this game application, choose N = 3. o After each human’s throw choice: Add the sequence consisting of the last 3 choices (human+computer+human) to your record. SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 2
Assignment #4 o For example, suppose the record contains: SRP, PRS, RPR, PSR, SRP, SPS, PSS (The human’s choices are underlined. ) o Suppose the last pair of throws is SR. What can the computer predict the human will choose next to throw? n n The record contains two instances of SRP, and so based on this small sample, the computer can predict the human will next throw Paper. Therefore, the computer should next choose Scissors to win. _ SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 3
Assignment #4 o The longer the matches are, the more throw choice sequences the computer has recorded during the match, and the harder it will be for the human player to win. n o You can initialize a fresh record of choice sequences each time you restart your application. n o This is a very rudimentary example of machine learning, a branch of the computer science discipline artificial intelligence. In later assignments, you can store the record in a disk file so that the application’s “experience” grows over time. Add another command-line parameter to indicate whether the computer should use the random algorithm (from Assignment #3) or the new smart algorithm. n In addition to the number of throws per match parameter. SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 4
Assignment #4 o Before printing what the computer chose to throw, your program should print what the computer predicted that the human will throw. o Use good object-oriented techniques to design how your application records throw choice sequences of length N = 3 and how your throw calculator looks up sequences to predict the human’s next choice. n o Make your design flexible enough to handle larger (odd) values of N. Include three JUnit test classes. n Each test class should contain at least two test cases. _ SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 5
o Review Quiz #2. SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 6
Quiz 2013 Oct 3 SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 7
Animation o By now we know how to draw a shape and respond to timer ticks. n Therefore, we can do simple animation! n Move a shape horizontally across the screen. o At each timer tick, erase the shape and redraw it slightly offset from its previous position. _ SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 8
Class Shape. Icon o Class Shape. Icon wraps up the shape as an icon: public class Shape. Icon implements Icon { private Moveable. Shape shape; . . . public Shape. Icon(Moveable. Shape shape, int width, int height) { this. shape = shape; . . . } . . . public void paint. Icon(Component c, Graphics g, int x, int y) { Graphics 2 D g 2 = (Graphics 2 D) g; shape. draw(g 2); } } SJSU Dept. of Computer Science CS 151: Object-Oriented Design Fall 2013: October 3 © R. Mak 9
Class JLabel o Put the icon inside of a Swing JLabel object: private static final int CAR_WIDTH = 100; private static final int ICON_WIDTH = 400; private static final int ICON_HEIGHT = 100; Moveable. Shape shape = new Car. Shape(0, 0, CAR_WIDTH); Shape. Icon icon = new Shape. Icon(shape, ICON_WIDTH, ICON_HEIGHT); JLabel label = new JLabel(icon); o Moveable. Shape is an interface. n Class Car. Shape implements that interface. _ SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 10
Interface Moveable. Shape o In order to do animation, you must be able to draw a shape and then move it. n n Move a shape = redraw the shape in a different location AKA translate the shape. public interface Moveable. Shape { void draw(Graphics 2 D g 2); void translate(int dx, int dy); } SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 11
Class Car. Shape public class Car. Shape implements Moveable. Shape { . . . public void translate(int dx, int dy) { public interface Moveable. Shape x += dx; { y += dy; void draw(Graphics 2 D g 2); void translate(int dx, int dy); } } public void draw(Graphics 2 D g 2) { Rectangle 2 D. Double body =. . . ; Ellipse 2 D. Double front. Tire =. . . ; Ellipse 2 D. Double rear. Tire =. . . ; . . . } } SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 12
Animation o We’ll use a Timer object to drive the animation. o At each timer tick: n Move the shape (which we had put inside the JLabel object) n Repaint the label object final int DELAY = 10; // Milliseconds between timer ticks Timer t = new Timer(DELAY, new Action. Listener() { public void action. Performed(Action. Event event) { shape. translate(1, 0); label. repaint(); } } ); t. start(); SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 13
Animation public void action. Performed(Action. Event event) { shape. translate(1, 0); label. repaint(); } o o The method calls shape. translate(1, 0) to move the shape one pixel to the right. The method call label. repaint() tells the JLabel object to repaint itself. n n The label object tells its contents to repaint. Since its contents is an instance of class Shape. Icon which implements the Icon interface, the JLabel object knows to call the content’s paint. Icon() method. SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 14
Animation SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak Animation demo 15
Poor Design! o What is wrong with the design this code? Moveable. Shape shape = new Car. Shape(0, 0, CAR_WIDTH); Shape. Icon icon = new Shape. Icon(shape, ICON_WIDTH, ICON_HEIGHT); JLabel label = new JLabel(icon); . . . Timer t = new Timer(DELAY, new Action. Listener() { public void action. Performed(Action. Event event) { shape. translate(1, 0); label. repaint(); } } Animation. Tester. main() ); SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 16
Poor Design! o Hint: public class Shape. Icon implements Icon { private Moveable. Shape shape; . . . } o Shape. Icon Can you improve the design? _ SJSU Dept. of Computer Science Fall 2013: October 3 CS 151: Object-Oriented Design © R. Mak 17
- Slides: 17