CSC 216 F09/Polymorphism
Bailey Hayes, Daniel Kennedy, and Catherine Pike
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
- 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 void attack(); public String talk(); 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 private double damage = 5.5; public ____() { super(); } public eat() { //define herbivore, carnivore, omnivore status } public String talk( ){ return "Some noise"; } public double attack() { return damage; //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 attack 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, though 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:
- Can a (created creature's name) be a (boss creature name)? If so, what methods would we have to modify?
- Can a (boss creature) be a (created creature)? If so, what needs to be modified?
- Can an Animal be casted as a ___?
- Can a _______ be casted as an Animal?
- When is each method bounded? (Compile/Run Time) for the following example:
import java.util.Scanner; public class MysteryCreature { public static void main(String[] args) { System.out.println("Which dungeon do you choose? (1, 2, or 3) "); Scanner input = new Scanner(System.in); int dungeonNumber = input.nextInt(); Animal animal; if (dungeonNumber == 1) { animal = new [first creature created](); } else if (dungeonNumber == 2) { animal = new [second creature created](); } else if (dungeonNumber == 3) { animal = new [third creature created](); } else { System.out.println("Not a valid Dungeon Number"); System.exit(1); } System.out.println("Your animal attacks with: " + animal.attack() + "!"); animal.walk(); } }