CSC 216 F09/Polymorphism: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 35: Line 35:
Example:
Example:


<source lang="csharp">
<source lang="java5">
 
abstract public class Animal {
abstract public class Animal {
     private boolean living;
     private boolean living;
Line 51: Line 50:
Now we need to make a subclass that extends Animal.
Now we need to make a subclass that extends Animal.


<source lang="csharp">
<source lang="java5">
 
//name the creature
//name the creature
public class ____ extends Animal {   
public class ____ extends Animal {   
Line 64: Line 62:
     //add other methods : attack, walk, fly, etc.
     //add other methods : attack, walk, fly, etc.
}
}
</source>
</source>



Revision as of 05:30, 17 November 2009

Formatting Resources

Formatting Help Guide from MetaWiki

Place Title of Exercise Here

Create A Creature

The problem

Students will learn the principles of inheritance and polymorphism.

Participants and props

This game requires a minimum of three people, but is designed to engage an entire class. A driver created in Flash can be used, but is not required.

The script

The class will be divided up into teams (by row, programming partners, etc.). The first team will be the "Artists," the second team will be the "Programmers," and all of the remaining teams will be the "Designers." Turns cycle from Designer -> Programmer -> Artist

For example: If we divided the class into teams by row and there were 5 rows:

  • row 1 - Artists
  • row 2 - Programmers
  • row 3 - Designers
  • row 4 - Designers
  • row 5 - Designers

After the first rotation, the roles would be:

  • row 2 - Artists
  • row 3 - Programmers
  • row 4 - Designers
  • row 5 - Designers
  • row 1 - Designers

The objective is to create the creatures within a new MMO. To begin, an Animal class has to be created, and the methods/variables of an Animal must also be created. This is the job of the entire class. The current Programming team will type/write this code to be displayed as ideas are called out. Example:

abstract public class Animal {
     private boolean living;
     public void eat();
     public Animal() {
          this.living = true;
     }
     public void die() {
          this.living = false;
     }
}

Now we need to make a subclass that extends Animal.

//name the creature
public class ____ extends Animal {  
     //add unique characteristics
     public ____(){ 
          super();
     }
     public eat(){
         //define herbivore, carnivore, omnivore status
     }
     //add other methods : attack, walk, fly, etc.
}

As each method is written, the artist's must draw a creature that implements those methods. For example, if the hear method requires the creature to attack using a unicorn horn, the artists will add a unicorn horn to the creature. The designers call out ideas, critique written code and drawn animals. It is the designer's job to come up with the components of the creature and what it can do. The programmer's must translate this into code.

The roles rotate after a method has been completed. When the creature is complete, the designers decide if a subclass of this class should be written (i.e. boss, weaker version etc.). The class can create as many creatures as desired, but for the sake of time, 2-3 creatures would complete the lesson.