Giter Site home page Giter Site logo

oldmacdonald's Introduction

OldMacDonald

Fork and clone down the OldMacDonald repository As you read through the assignment below, add code to your OldMacDonald program when you get to any instructions in italics

Background: Inheritance and Polymorphism

In this assignment we will use Old MacDonald's Farm to learn how implements and extends work, and the meaning of inheritance and polymorphism.

Defining an interface

Old MacDonald had a farm that had several types of animals. Every animal shared certain characteristics: they had a type (such as cow, chick or pig) and each made a sound (moo, cluck or oink). An interface defines those things required to be an animal on the farm.

  interface Animal 
  {    
    public String getSound();        
    public String getType(); 
  }   

Add the Animal interface to your OldMacDonald program after the closing curly brace of the setup() function. Run the program to make sure it compiles and runs.

Once we know what it takes to be an Animal, we can define new classes for the cow, chick and pig that implement the Animal interface. Here is a Cow class meeting the minimum requirements to be an Animal.

class Cow implements Animal 
{     
  private String myType;     
  private String mySound;      
  public Cow(String type, String sound)    
  {         
     myType = type;         
     mySound = sound;     
  }     
  public Cow()    
  {         
     myType = "unknown";         
     mySound = "unknown";     
  }      
  public String getSound(){return mySound;}     
  public String getType(){return myType;} 
}

Add the Cow class below the closing curly brace of your Animal interface. Run the program to make sure it compiles and runs. Implement classes for the chick and the pig. Add the following code to your setup() function, and run the program to verify your work so far. Make sure you create some chick and pig instances in setup()and check their sounds as well.

public void setup() 
{     
    Cow c = new Cow("cow", "moo");   
    System.out.println(c.getType() + " goes " + c.getSound());  }  
}

Now add the following Farm class to complete the farm and test all your animals. Make sure that it isn't inside any the curly braces of the other classes.

class Farm  
{     
  private Animal[] aBunchOfAnimals = new Animal[3];    
  public Farm()     
  {       
    aBunchOfAnimals[0] = new Cow("cow","moo");           
    aBunchOfAnimals[1] = new Chick("chick","cluck");       
    aBunchOfAnimals[2] = new Pig("pig","oink");    
  }         
  public void animalSounds()    
  {       
    for (int nI=0; nI < aBunchOfAnimals.length; nI++)      
    {          
     System.out.println( aBunchOfAnimals[nI].getType() + " goes " + aBunchOfAnimals[nI].getSound());       
    }    
  } 
}

Now, change your code in setup() to create an new instance of type Farm and call its animalSounds function.

It turns out that baby chicks make two different sounds. Some chicks say "cheep" while others say "cluck".

Add a three argument Chick constructor that will randomly choose one of the two possible sounds. You will also have to modify your Farm class code to construct the Chick with two possible sounds.

Finally, it also came to pass that the cows get a personal name, like Elsie.

Create a new class, NamedCow, that extends the Cow class, adding a constructor, a field for the Cow's name, and a new function: getName. Change the Cow member variables from private to protected so that the NamedCow constructor can initialize them

The final Farm code to exercise all your modifications is shown here:

class Farm 
{     
private Animal[] aBunchOfAnimals = new Animal[3];    
public Farm()    
{       
 aBunchOfAnimals[0] = new NamedCow("cow","Elsie","moo");          
 aBunchOfAnimals[1] = new Chick("chick","cheep","cluck");
 aBunchOfAnimals[2] = new Pig("pig","oink");    
}     
public void animalSounds()    
{
  for (int nI=0; nI < aBunchOfAnimals.length; nI++) 
  {             
     System.out.println( aBunchOfAnimals[nI].getType() + " goes " + aBunchOfAnimals[nI].getSound() );       
  }       
  System.out.println( "The cow is known as " + ((NamedCow)aBunchOfAnimals[0]).getName() );    
} 
}

What Did You Just Do?

So you don't miss it, make sure you understand what you just accomplished. Having an array of Animal objects and then having the getSound() function dynamically decide what sound to make is polymorphism. This is also known as late binding because it wasn't known until run-time that aBunchOfAnimlas[1], for example, really had a Chick object. You started with an interface for an Animal and then used the keyword implements in making the three types of animals. Then you created a specialized version of the Cow, a NamedCow, using the keyword extends. This illustrates the concept of inheritance. The NamedCow had all the attributes and functions of the Cow and then added some: a new field and a new function to access the cow's name. Note that there is no web page for this assignment. Submit the url of your GitHub repository via the school loop drop box for the assignment. The url will have the form http://github.com/*your github username*/OldMacDonald

oldmacdonald's People

Contributors

simart avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.