Class Monkey

java.lang.Object
  extended by Monkey

public class Monkey
extends java.lang.Object


Constructor Summary
Monkey(Sex sex, int year)
          Create a monkey instance.
 
Method Summary
 void addChild(Monkey child)
          This method adds a child "child" for this monkey.
 Monkey getDad()
           
 int getID()
           
 Monkey getMom()
           
 int getNumChildren()
           
 Sex getSex()
           
 int getYearofbirth()
           
 boolean isChild(Monkey child)
           
static void main(java.lang.String[] args)
           
 void removeChild(Monkey child)
           
 void setDad(Monkey dad)
           
 void setMom(Monkey mom)
          Set/replace a monkey's mom.
static void stressTest()
          Generates a large number monkeys with random year of births and random sex.
static void testAddingRelations()
          Creates a small number of monkeys, add a few relationships, and then does some basic tests.
 void testInvariants()
          This method should contain a lot of checks using assertions that test the correctness of values and relationships in this monkey.
static void testRemovingRelations()
          Creates a small number of monkeys, add.removes a few relationships, and then does some basic tests.
 java.lang.String toString()
           
 void unsetDad()
           
 void unsetMom()
          Remove the mom for this monkey.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Monkey

public Monkey(Sex sex,
              int year)
Create a monkey instance. The constructor should set the ID value to the next available ID. Available ID value should go up by 1 every time a monkey is created, starting from 0. It should also initialize other values, such as children, numChildren, etc. testinvariants() should be called at the end of this method to make sure that the class invariants are established correctly when an object is created.

Parameters:
sex - the sex of the new monkey. Values Monkey.Sex.M or Monkey.Sex.F
year - he year in which the monkey was born. A value >= 0
Method Detail

setMom

public void setMom(Monkey mom)
Set/replace a monkey's mom. If an illegal monkey is given (e.g., younger or same age, male monkey), you should ignore the request. Make sure you call testInvariants() at the start and end of the method to make sure you maintain the invariants.

Parameters:
mom - This monkey's mom should be set to the the specified parameter.

getMom

public Monkey getMom()
Returns:
the mom of this monkey. If no mom set, returns null.

setDad

public void setDad(Monkey dad)

getDad

public Monkey getDad()

unsetMom

public void unsetMom()
Remove the mom for this monkey. This method may be necessary if the mom was incorrectly set previously. This should also cause the monkey to be removed from the list of children for the mom. Should do nothing if the monkey does not have a mom.


unsetDad

public void unsetDad()

getID

public int getID()
Returns:
the unique ID assigned to this monkey. The ID is auto-assigned by the constructor.

isChild

public boolean isChild(Monkey child)
Parameters:
child -
Returns:
true if the parameter child is one of this monkey's children; else returns false.

addChild

public void addChild(Monkey child)
This method adds a child "child" for this monkey. The method should return without adding a child if the given child monkey is same age or older. It should also return without adding if the number of children is at the MAX_CHILDREN limit.

Parameters:
child - child to be added

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getSex

public Sex getSex()
Returns:
the sex of the monkey. Possible values: Sex.M or Sex.F

testInvariants

public void testInvariants()
This method should contain a lot of checks using assertions that test the correctness of values and relationships in this monkey. We we give one example below. The invariants you should test for are given in the assignment handout.


removeChild

public void removeChild(Monkey child)
Parameters:
child - This removes the specified parameter from the children's list of this monkey. It should also update the state of the child, if necessary, so that the invariants are maintained. If the child paramter is not a child of this monkey, this method should return without changing the state of the monkey.

getNumChildren

public int getNumChildren()
Returns:
the number of children of this monkey

getYearofbirth

public int getYearofbirth()
Returns:
the yearofbirth value

stressTest

public static void stressTest()
Generates a large number monkeys with random year of births and random sex. Then establish a random set of child-parent relationships among them. Use assertions to check that the class is working as desired.


testAddingRelations

public static void testAddingRelations()
Creates a small number of monkeys, add a few relationships, and then does some basic tests.


testRemovingRelations

public static void testRemovingRelations()
Creates a small number of monkeys, add.removes a few relationships, and then does some basic tests.


main

public static void main(java.lang.String[] args)