CSC 216/s08/strive for happiness: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(21 intermediate revisions by 2 users not shown)
Line 7: Line 7:
===The problem===
===The problem===


Describe what you are attempting to teach students by this exercise.
Polymorphism is derived from Greek, meaning "many forms".  In Java, polymorphism is using a superclass variable to refer to a subclass object, the "many forms" of the superclass variable.  
<p>Polymorphism is derived from Greek, meaning "many forms".  Therefore, in Java, polymorphism is using a super class variable to refer to a subclass object, the "many forms" of the super class variable. It is useful because interfaces and inheritance to be used more abstractly.</p>
 
<p>A game loosely based on "Let's Make A Deal" would be played to illustrate this abstraction.</p>
The core concepts that this activity will teach are polymorphism and inheritance. The activity will visually illustrate why inheritance is an "is-a" relationship between two classes, and how variables of a given type can refer to objects of either their own class, or any subclass object. It will also touch on how methods are overridden by subclass methods when they are defined in both the superclass and subclass.


===Participants and props===
===Participants and props===


The whole class would participate and be divided into 2 teams.
The class will be divided into small groups of 3-5 people. Each group will receive a number of Animal Cards (explained below) and a list of exercises will be shown on the board.


<p>5 students would participate in the front of the class.</p>
An Action Card will be a small index card with a class definition and one or two methods.  
<p>Teams would be vying for "the prize".  Incentive might be a victory trophy (cans of soda, candy bars, 1 extra point on the exam or homework).</p>


<p>Props would 8.5" x 11" paper with writing on it.</p>
For example:
<p>4 students would be each be holding 3 pieces of paper: 1 with a number on the "door" and another  with code on it.  The code would be hidden behind the "door numbered" piece of paper.  The code would list the animal type.  The last piece of paper would be a method.</p>


What else do you need (e.g., old tennis ball, Powerpoint slides, software).
    public class Dog extends Quadruped {
<p>There may be a Powerpoint slide still up on the screen with the rules listed as a reminder.</p>
        public void talk() {
            System.out.println("Woof");
        }
    }
 
    public class Quadruped extends Animal {
        public void walk() {
            System.out.println("Walk");
        }
    }
 
    public class Animal {
    }
 
    public class Bird extends Animal {
        public void fly() {
            System.out.println("Fly");
        }
        public void talk() {
            System.out.println("Chirp");
        }
    }
 
    public class Owl extends Bird {
        public void talk() {
            System.out.println("Whoo");
        }
    }
 
    public class Parrot extends Bird {
        public void talk(String s) {
            System.out.println(s);
        }
    }
 
An html page, powerpoint slide, or simply a part of the class notes will have a list of exercises for the groups to perform.
 
For example:
 
    Bird B = new Owl();
    B.talk();
 
    Animal A = new Bird();
    A.talk();
 
A few, more advanced exercises would be added at the end, to showcase the real power of polymorphism, such as:
 
    Animal[] A = new Animal[3];
    A[0] = new Owl();
    A[1] = new Dog();
    A[2] = new Cow();
    for(Animal a: A)
        a.talk();
 
An example of overloaded methods:
 
    Parrot P = new Parrot();
    P.talk();
    P.talk("Hello");
 
The students will then write down the answers to all of these exercises based upon what is on their cards.


===The script===
===The script===


<p>A game loosely based on "Let's Make A Deal" would be played.</p>
The exercise will be introduced, and the props and instructions given to the class, which should take no more than 5 minutes. Overhead instructions will also be available for students to refer to.  Each group will be given a stack of about 15 cards, and each group will have identical cards to each of the other groups.
<p>2 teams would see a walk-through of 1 person going through the motions of choosing 1 of 4 doors and seeing what's behind each of the doors.</p>
 
<p>Behind each door would be an animal.  Each animal would have maybe 2 methods, such as talk() or daySleeper().</p>
The class will be given 10-15 minutes to do about 20 exercises, based on the cards they are given.
<p>A follow up of the exercise would be that each team would be given lines of code (from the animal abstract interface example) to order correctly.</p>
 
<p>Next exercise:  each team would attempt to write the code in order to make the walk-through work in a specified/requested fashion from the Powerpoint slide listed on the overheadFor example:  Make an animal that is a carnivore, talk.</p>
<p>After the exercise is concluded, the class will review the correct answers and discuss them for 10-15 minutesThe class will go over how the Animal superclass is analogous to the Object superclass in JavaDiscussion will cover how methods of a subclass override methods of the superclass, in the same way that the methods of the animals do.</p>
<p>Next exercise: Each group needs to write a main method for testing purposes to create a working action, specified by the overhead, from the code that was ordered together from the previous exercise.  Perhaps there is a time limit....</p>

Latest revision as of 20:15, 4 April 2008

Formatting Resources

Formatting Help Guide from MetaWiki

What is Polymorphism?

The problem

Polymorphism is derived from Greek, meaning "many forms". In Java, polymorphism is using a superclass variable to refer to a subclass object, the "many forms" of the superclass variable.

The core concepts that this activity will teach are polymorphism and inheritance. The activity will visually illustrate why inheritance is an "is-a" relationship between two classes, and how variables of a given type can refer to objects of either their own class, or any subclass object. It will also touch on how methods are overridden by subclass methods when they are defined in both the superclass and subclass.

Participants and props

The class will be divided into small groups of 3-5 people. Each group will receive a number of Animal Cards (explained below) and a list of exercises will be shown on the board.

An Action Card will be a small index card with a class definition and one or two methods.

For example:

   public class Dog extends Quadruped {
       public void talk() {
           System.out.println("Woof");
       }
   }
   public class Quadruped extends Animal {
       public void walk() {
           System.out.println("Walk");
       }
   }
   public class Animal {
   }
   public class Bird extends Animal {
       public void fly() {
           System.out.println("Fly");
       }
       public void talk() {
           System.out.println("Chirp");
       }
   }
   public class Owl extends Bird {
       public void talk() {
           System.out.println("Whoo");
       }
   }
   public class Parrot extends Bird {
       public void talk(String s) {
           System.out.println(s);
       }
   }

An html page, powerpoint slide, or simply a part of the class notes will have a list of exercises for the groups to perform.

For example:

   Bird B = new Owl();
   B.talk();
   Animal A = new Bird();
   A.talk();

A few, more advanced exercises would be added at the end, to showcase the real power of polymorphism, such as:

   Animal[] A = new Animal[3];
   A[0] = new Owl();
   A[1] = new Dog();
   A[2] = new Cow();
   for(Animal a: A)
       a.talk();

An example of overloaded methods:

   Parrot P = new Parrot();
   P.talk();
   P.talk("Hello");

The students will then write down the answers to all of these exercises based upon what is on their cards.

The script

The exercise will be introduced, and the props and instructions given to the class, which should take no more than 5 minutes. Overhead instructions will also be available for students to refer to. Each group will be given a stack of about 15 cards, and each group will have identical cards to each of the other groups.

The class will be given 10-15 minutes to do about 20 exercises, based on the cards they are given.

After the exercise is concluded, the class will review the correct answers and discuss them for 10-15 minutes. The class will go over how the Animal superclass is analogous to the Object superclass in Java. Discussion will cover how methods of a subclass override methods of the superclass, in the same way that the methods of the animals do.