Testing The Duck Code
Testing The Duck Code
1. Type and compile the Duck class below (Duck.java) and the MallardDuck class from two pages back (MallardDuck.java)
public abstract class Duck { FlyBehavior flyBehavior; QuackBehavior quackBehavior; public Duck() { } public abstract void display(); public void performFly() { flyBehavior.fly(); } public void performQuack() { quackBehavior.quack(); } public void swim() { System.out.println("All ducks float, even decoys!"); } }
2. Type and compile the FlyBehavior interface (FlyBehavior.java) and the two behavior implementation classes (FlyWithWings.java and FlyNoWay.java)
public interface FlyBehavior { public void fly(); } public class FlyWithWings implements FlyBehavior { public void fly() { System.out.println("I'm flying!!"); } }
public class FlyNoWay implements FlyBehavior { public void fly() { System.out.println("I can't fly"); } }
3. Type and compile the QuackBehavior interface (QuackBehavior.java) and the three behavior implementation classes (Quack.java, MuteQuack.java and Sqeak.java)
public interface QuackBehavior { public void quack(); }
public class MuteQuack implements QuackBehavior { public void quack() { System.out.println("<< Silence >>"); } }