Polymorphism: Old McDonald

Objectives

  • Create a Java interface
  • Create an abstract base (super) class
  • Create derived classes (subclasses)
  • Compile a multi-file Java application
  • Use object-oriented programming techniques when creating a class
  • Follow Java coding style guidelines

Overview

You have to create an interface, five classes, and complete an existing class to implement a program which produces the lyrics to the song Old McDonald's Farm.

  • Create an interface named McDonaldable
  • Create a base class named Animal which implements McDonaldable
  • Create a subclass of Animal named Cow
  • Create a subclass of Animal named Pig
  • Create a subclass of Animal named Aardvark
  • Create a subclass of Animal named Dog
  • Finish implementing the code in Farm.java
  • Compile, test, and debug your programs

Resources

Farm program (starting code)

Basic requirements analysis

  • McDonaldable.java (5 points)
    • Include proper documentation comments at the start of the file.
    • This should be a pure abstract class, which is an interface.
    • The name of the interface is McDonaldable
    • Create an abstract public method named getSound that takes no arguments and returns a String. Do not write any implementation code - this is an abstract method.
    • Create an abstract public method named getType that takes no arguments and returns a String. Do not write any implementation code - this is an abstract method.
  • Animal.java (10 points)
    • Include proper documentation comments at the start of the file.
    • This class should declare that it implements the McDonaldable interface. If this class does not implement the methods from that interface, then this class will also need to be declared abstract.
    • This class should contain two private String variables named type and sound.
    • Create a constructor which sets the type and sound instance variables. It should take two arguments. The first argument should be for the type, and the second argument for the sound.
    • Create an accessor method named getType to return the type.
    • Create an accessor method named getSound to return the sound.
  • Cow.java (5 points)
    • Include proper documentation comments at the start of the file.
    • This class should extend the Animal class.
    • Create a default constructor which sets the type to cow and the sound to moo.
  • Pig.java (5 points)
    • Include proper documentation comments at the start of the file.
    • This class should extend the Animal class.
    • Create a default constructor which sets the type to pig and the sound to oink.
  • Aardvark.java (5 points)
    • Include proper documentation comments at the start of the file.
    • This class should extend the Animal class.
    • Create a default constructor which sets the type to aardvark and the sound to slurp.
  • Dog.java (10 points)
    • Include proper documentation comments at the start of the file.
    • This class should extend the Animal class.
    • This class should have two private String variables. One for a second sound, and one for a name.
    • Create a constructor which takes one argument. The constructor should set the type to dog and the sound to woof. The argument sent to the constructor should be used to set the instance variable for the name. The second sound should be set to ruff.
    • Create a public method named getSound which returns a String. That String should randomly return either of the two sounds this object contains.
    • Create a public method named getType which returns a String. That String should be in this format: tttt named nnnn (where tttt is replaced by the type, and nnnn is replaced by the name).
  • Farm.java (10 points)
    • Add documentation comments.
    • Check the code for comments that show where you have to add code.
    • Create a new Random number generator named rnd.
    • Create a new ArrayList named animals which holds Animal references
    • Add num Animal objects to the ArrayList you created. The animals must be randomly chosen to be one of the Animal subclasses. If you are creating a Dog object, then you must also choose one of the dog names (from the names array) at random.
  • Documentation and style guidelines should be followed throughout the project. (5 points)

Sample output