Computer Science CS50 Lecture Notes - Lecture 1: Method Stub

38 views6 pages

Document Summary

Design a simuduck application in java using strategy design pattern. The game can show a large variety of duck species swimming and making quacking sounds. We want to assign behavior to instance of the duck. Even better, if can change the behavior dynamically design principle: program to an. We will use interface to represent this behavior interface, not an implementation. Quackbehavior quackbehavior; public duck() { public abstract void display(); public void performfly(){ flybehavior. fly(); public void performquack(){ quackbehavior. quack(); public void swim(){ //change duck behavior through setter public void setflybehavior(flybehavior flybehavior) { this. flybehavior = flybehavior; public void setquackbehavior(quackbehavior quackbehavior) { this. quackbehavior = quackbehavior; //behavior interface public interface flybehavior { public void fly(); //behavior that can"t fly public class flynoway implements flybehavior{ System. out. println("i can"t fly"); public class flyrocketpowered implements flybehavior{ System. out. println("i"m flying with a rocket! public class flywithwings implements flybehavior { System. out. println("i"m flying!! public class mallardduck extends duck { public mallardduck() { quackbehavior = new quack(); flybehavior = new flywithwings();

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents