CSC 216 F09/Polymorphism: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(Create a Creature : Using Inheritance and Polymorphism)
No edit summary
 
(28 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Redirect|Java language|the Indonesian spoken language|Javanese language}}
'''Bailey Hayes, Daniel Kennedy, and Catherine Pike'''
{{distinguish|JavaScript}}
 
{{infobox programming language
| name                  = Java
| logo                  = [[File:Java logo.svg|100px]]
| paradigm              = [[Object-oriented programming|Object-oriented]], [[Structured programming|structured]], [[Imperative programming|imperative]]
| year                  = [[1995]]
| designer              = [[Sun Microsystems]]
| latest_release_version = Java Standard Edition 6 (1.6.0_17)
| latest_release_date    =
| latest_test_version    =
| latest_test_date      =
| turing-complete        = Yes
| typing                = [[Type system|Static, strong, safe]], [[Nominative type system|nominative]], [[Manifest typing|manifest]]
| implementations        = [[:Category:Java virtual machine|Numerous]]
| influenced_by          =  [[Objective-C]],<ref>[[Patrick Naughton]] cites [[Objective-C]] as a strong influence on the design of the Java programming language, stating that notable direct derivatives include Java interfaces (derived from Objective-C's [[Objective-C#Protocols|protocol]]) and primitive wrapper classes. [http://cs.gmu.edu/~sean/stuff/java-objc.html]</ref> [[Ada (programming language)|Ada 83]], [[Object Pascal|Delphi Object Pascal]]<ref>«We looked very carefully at [[Object Pascal|Delphi Object Pascal]] and built a working prototype of bound method references in order to understand their interaction with the Java programming language and its APIs.» <br/>«Our conclusion was that bound method references are unnecessary and detrimental to the language. This decision was made in consultation with Borland International, who had previous experience with bound method references in [[Object Pascal|Delphi Object Pascal]].» [http://java.sun.com/docs/white/delegates.html White Paper.About Microsoft's "Delegates"], java.sun.com</ref>, [[UCSD Pascal]]<ref>{{cite web|url=http://www.fscript.org/prof/javapassport.pdf|quote=The project went ahead under the name "green" and the language was based on an old model of [[UCSD Pascal]], which makes it possible to generate interpretive code|title=History of Java|work=Java Application Servers Report|author=TechMetrix Research|date=1999}}</ref><ref>http://queue.acm.org/detail.cfm?id=1017013</ref> [[C++]], [[C Sharp (programming language)|C#]],<ref>Java 5.0 added several new language features (the [[foreach|enhanced for loop]], [[autoboxing]], [[variadic function|varargs]] and [[Java annotation|annotations]]), after they were introduced in the similar (and competing) [[C Sharp (programming language)|C#]] language [http://www.barrycornelius.com/papers/java5/][http://www.levenez.com/lang/]</ref> [[Eiffel (programming language)|Eiffel]],<ref>{{ cite web | authors = Gosling and McGilton | title = The Java Language Environment | date=  May 1996 | url = http://java.sun.com/docs/white/langenv/Intro.doc1.html#943 }}</ref> [[Smalltalk]], [[Mesa (programming language)|Mesa]],<ref>{{cite web | authors = J. Gosling, B. Joy, G. Steele, G. Brachda | title = The Java Language Specification, 2nd Edition | url= http://java.sun.com/docs/books/jls/second_edition/html/intro.doc.html#237601 }}</ref> [[Modula-3]],<ref>http://www.computerworld.com.au/index.php/id;1422447371;pp;3;fp;4194304;fpid;1</ref> [[Generic Java]]
| influenced            = [[Ada (programming language)|Ada&nbsp;2005]], [[C Sharp (programming language)|C#]], [[Clojure]], [[D (programming language)|D]], [[ECMAScript]], [[Groovy (programming language)|Groovy]], [[J Sharp|J#]], [[PHP]], [[Scala (programming language)|Scala]], [[JavaScript]], [[Python (programming language)|Python]], [[BeanShell]]
| dialects = [[Generic Java]], [[Pizza (programming language)|Pizza]]
| operating_system      = [[Cross-platform|Cross-platform (multi-platform)]]
| license                = [[GNU General Public License]]&nbsp;/ [[Java Community Process]]
| website                = http://java.sun.com
}}


===Formatting Resources===
===Formatting Resources===
[http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki]
[http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki]


==Place Title of Exercise Here==
==Create a Creature==
 
Create A Creature


===The problem===
===The problem===
Line 36: Line 12:
===Participants and props===
===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.
* 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 script===
Line 56: Line 34:
* row 1 - 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.
====Objective====
The objective is to create the creatures within a new [http://en.wikipedia.org/wiki/Massively_multiplayer_online_game 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:
Example:


<source lang="java5">
    abstract public class Animal {
abstract public class Animal {
          private boolean living;
    private boolean living;
          public void eat();
    public void eat();
          public void attack();
    public Animal() {
          public String talk();
          this.living = true;
          public Animal() {
    }
                this.living = true;
    public void die() {
          }
          this.living = false;
          public void die() {
    }
                this.living = false;
}
          }
</source>
      }


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


<source lang="java5">
    //name the creature
//name the creature
    public class ____ extends Animal {   
public class ____ extends Animal {   
          //add unique characteristics
    //add unique characteristics
          private double damage = 5.5;
    public ____(){  
          public ____() {  
          super();
                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.
     }
     }
    public eat(){
        //define herbivore, carnivore, omnivore status
    }
    //add other methods : attack, walk, fly, etc.
}


</source>


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.
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:


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.
<pre>
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();
    }
}
</pre>

Latest revision as of 02:12, 18 November 2009

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:

  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?
  5. 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();
     }
}