Tuesday, February 07, 2006

M301: Polyfrogism

Below is some code (based upon the Amphibian World of M206) I knocked to show a fellow student the concept of polymorpism within Java.

import java.util.StringTokenizer;

public abstract class Amphibian
{
static final int HOME_POSITION = 1; // Specifies the starting stone of an amphibian.
static final int NUM_OF_STONES = 1; // Specifies the number of stones an amphibain will move by.
static final String INITIAL_COLOUR = "Green"; // Specifies the default colour of an amphibian.

private String colour; // The colour of the amphibian.
private int position; // Represents the stone on which the amphibian is on.

public Amphibian(String colour, int position)
{
this.colour = colour;
this.position = position;
}

/**
* @return the position of the amphibian, an integer.
*/
public int getPosition()
{
return this.position;
}

/**
* @return the colour of the amphibian, a String.
*/
public String getColour()
{
return this.colour;
}

/**
* Set the stone for the amphibian to stand on.
* @param position The position the amphibian is to be at.
* @return no answer.
*/
void setPosition(int position)
{
this.position = position;
}

/**
* Set the colour of the amphibian.
* @param colour The colour the amphibian is to be.
* @return no answer.
*/
void setColour(String colour)
{
this.colour = colour;
}

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void right();

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void left();

/**
* Set the colour of the amphibian to Brown.
* @return no answer.
*/
public void brown()
{
this.setColour("Brown");
}

/**
* Set the colour of the amphibian to Green.
* @return no answer.
*/
public void green()
{
this.setColour("Green");
}

/**
* Set the colour of the amphibian to that of anAmphibian.
* @param anAmphibian The amphibian who's colour is to be used.
* @return no answer.
*/
public void sameColourAs(Amphibian anAmphibian)
{
this.setColour(anAmphibian.getColour());
}

/**
* @return the class name of the amphibian, a string.
*/
public String className()
{
StringTokenizer element = new StringTokenizer(this.toString(),"@");
return element.nextToken();
}

/**
* Textual representation of the amphibian.
* @return amphibians details.
*/
public String inspect()
{
return ("An instance of class " + this.className() + "(position " +
this.getPosition() + ", colour " + this.getColour() + ")");
}

/**
* subclass responsibility.
* @return no answer.
*/
public abstract void home();

} // End of class Amphibian


public class Frog extends Amphibian
{
public Frog()
{
this(INITIAL_COLOUR, HOME_POSITION);
}

public Frog(String colour)
{
this(colour, HOME_POSITION);
}

public Frog(String colour, int position)
{
super(colour, position);
}

/**
* Increase the position of the frog instance by the
* default number of stones.
* @return no answer.
*/
public void right()
{
this.setPosition(this.getPosition() + NUM_OF_STONES);
}

/**
* Decrease the position of the frog instance by the
* default number of stones.
* @return no answer.
*/
public void left()
{
this.setPosition(this.getPosition() - NUM_OF_STONES);
}

/**
* Move the frog instance to its home (start) position.
* @return no answer.
*/
public void home()
{
this.setPosition(HOME_POSITION);
}
} // End of class Frog


public class Toad extends Amphibian
{
static final int HOME_POSITION = 11;
static final int NUM_OF_STONES = 2;
static final String INITIAL_COLOUR = "Brown";

public Toad()
{
this(INITIAL_COLOUR);
}

public Toad(String colour)
{
this(colour, HOME_POSITION);
}

public Toad(String colour, int position)
{
super(colour, position);
}

/**
* Increase the position of the toad instance by the
* default number of stones.
* @return no answer.
*/
public void right()
{
this.setPosition(this.getPosition() + NUM_OF_STONES);
}

/**
* Decrease the position of the toad instance by the
* default number of stones.
* @return no answer.
*/
public void left()
{
this.setPosition(this.getPosition() - NUM_OF_STONES);
}

/**
* Move the toad instance to its home (start) position.
* @return no answer.
*/
public void home()
{
this.setPosition(HOME_POSITION);
}

} // End of class Toad


import java.util.*;

public class AmphibianWorld
{
public static void main(String [] args)
{
HashMap thePond = new HashMap(1);

thePond.put("kermit", new Frog()); // put kermit in the pond
thePond.put("toady", new Toad("red", 1)); // put a red toady in the pond on stone 1

Amphibian tempAmphib;

// lets see the pond
tempAmphib = (Amphibian) thePond.get("kermit");
System.out.println(tempAmphib.inspect());

tempAmphib = (Amphibian) thePond.get("toady");
System.out.println(tempAmphib.inspect());

System.out.println("move kermit right 3 stones");
tempAmphib = (Amphibian) thePond.get("kermit");
tempAmphib.right();
tempAmphib.right();
tempAmphib.right();
System.out.println(tempAmphib.inspect());

System.out.println("move toady right 6 stones");
tempAmphib = (Amphibian) thePond.get("toady");
tempAmphib.right(); // toady moves 2 stones at a time
tempAmphib.right();
tempAmphib.right();
System.out.println(tempAmphib.inspect());

System.out.println("now lets make both of them blue");
String name;
Set pondSet = thePond.keySet();
Iterator pondIt = pondSet.iterator();

while (pondIt.hasNext()){
name = (String) pondIt.next();
tempAmphib = (Amphibian) thePond.get(name);
tempAmphib.setColour("blue");
System.out.println(tempAmphib.inspect());
}
}
} // End of class AmphibianWorld

No comments: