CSC 216 F09/Polymorphism: Difference between revisions

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


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


Now we need to make a subclass that extends Animal.
Now we need to make a subclass that extends Animal.


//name the creature </br>
    //name the creature
public class ____ extends Animal {   
public class ____ extends Animal {   
     //add unique characteristics
     //add unique characteristics

Revision as of 15:49, 17 November 2009

Formatting Resources

Formatting Help Guide from MetaWiki

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 chalkboard/whiteboard with chalk/dry erase markers is necessary for drawing. 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

Objective

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 artists must draw a creature that implements those methods. For example, if the horn 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 and critique the 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 programmers must translate this into code.

The roles rotate after a method has been completed. When the creature is complete, a subclass of the created creation will be created. This class will represent a boss type of the previously created creature. Follow the above steps and until the boss class is complete. The class can create as many creatures as desired, but for the sake of time, 2-3 creatures would complete the lesson.

Questions

After the activity is completed, the instructor should ask the following questions:

  1. Can a (created creature's name) be a (boss creature name)? If so, what methods would we have to modify?
  2. Can a (boss creature) be a (created creature? If so, what needs to be modified?
  3. Can an Animal be casted as a ___?
  4. Can a _______ be casted as an Animal?