0% found this document useful (0 votes)
20 views

Testing The Duck Code

The document provides instructions to test the Duck code by compiling various classes that implement a duck hierarchy with behaviors for flying and quacking. This includes: 1. Compiling the Duck superclass and MallardDuck subclass 2. Compiling interfaces and classes for fly behaviors 3. Compiling interfaces and classes for quack behaviors 4. Compiling a test class to instantiate a MallardDuck and call its methods 5. Running the test class to output the duck's quacking and flying behaviors.

Uploaded by

api-251901021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Testing The Duck Code

The document provides instructions to test the Duck code by compiling various classes that implement a duck hierarchy with behaviors for flying and quacking. This includes: 1. Compiling the Duck superclass and MallardDuck subclass 2. Compiling interfaces and classes for fly behaviors 3. Compiling interfaces and classes for quack behaviors 4. Compiling a test class to instantiate a MallardDuck and call its methods 5. Running the test class to output the duck's quacking and flying behaviors.

Uploaded by

api-251901021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 Quack implements QuackBehavior { public void quack() { System.out.println("Quack"); } }

public class MuteQuack implements QuackBehavior { public void quack() { System.out.println("<< Silence >>"); } }

public class Squeak implements QuackBehavior { public void quack() { System.out.println("Squeak"); } }

4. Type and compile the test class (MiniDuckSimulator.java).


public class MiniDuckSimulator1 { public static void main(String[] args) { Duck mallard = new MallardDuck(); mallard.performQuack(); mallard.performFly(); } }

5. Run the code!


> java MiniDuckSimulator Quack I'm flying!!

You might also like