städtisches Gymnasium :: Schulstr. 2 - 6 :: 45468 Mülheim a. d. Ruhr :: Tel. (0208) 30 87 00 :: Fax (0208) 30 87 049 :: karl-ziegler-schule@stadt-mh.de

Von Guppys und Haien


Die Methode!


Die Konsolenausgabe!


Ein Beispiel von unserem Freund dem Fish, aber was ist es für ein Fischlein?

class Fish extends Pet{

    int currentDepth=0;
    int tooDeep=0;
    
    public String say(String something){
        return "Dummy, don't Ya know that fish do NOT talk?!";
    }
    public int dive (int howDeep){
        currentDepth = currentDepth + howDeep;
        tooDeep = currentDepth - 100;
        if (currentDepth > 100){
            System.out.println("I'm just a little fish and can't dive " +
                    "like a Shark " + currentDepth + " feet, you PappNose!");
            System.out.println("That is " + tooDeep + " feet too deep!");
        }else{    
            System.out.println("Diving for " + howDeep + " feet.");
            System.out.println("I'm at " + currentDepth +
                    " feet below sea level.");
        }
        return currentDepth;
    }
}




Don't let a guppy dive like a shark!


Die aufrufende Klasse!


public class FishMaster {

    public static void main(String[] args) {
        String fishReaction;
        String sharkReaction;
       
        Fish myGuppy = new Fish();
       
        myGuppy.dive(2);
        myGuppy.dive(3);
       
        System.out.println("");
        System.out.println("A little fish likes to dive like a shark!");
        myGuppy.dive(45);
        myGuppy.dive(45);
        myGuppy.dive(6);
       
        Shark myShark = new Shark();          /*Something you should know about sharks*/
           
        System.out.println("");
        System.out.println("A shark CAN dive like a shark!");
        myShark.dive(50);
        myShark.dive(45);
        myShark.dive(6);
               
        fishReaction = myGuppy.say("Hello I'am a fish!");
        sharkReaction = myShark.say("Hello I'am a shark!");
        System.out.println("");
        System.out.println(fishReaction);
        System.out.println(sharkReaction);
        System.out.println("");
       
        myGuppy.sleep();
        myShark.sleep();
    }

}


Haie schlafen nicht und tauchen tief!


Die Klasse "Shark" mit "Overriding" verschiedener Methoden in "Pet" und "Fish"!


public class Shark extends Fish{          /*Something you should know about sharks*/

    int currentDepth=0;
    
    public String say(String something){
        return "Dear my friend, a shark can not talk either!";
    }
    
    public int dive (int howDeep){
        currentDepth = currentDepth + howDeep;
        System.out.println("Diving for " + howDeep + " feet.");
        System.out.println("I'm at " + currentDepth + " feet below sea level.");
        return currentDepth;
    }


    public void sleep(){
        System.out.println("Sharks do not sleep!");
        System.out.println("");
    }
}


Aufgabe

  1. Beschreibe und skizziere die Funktionsweise der Schleifen und die Kommunikation zwischen den Klassen! (Schriftlich)

  2. Schreibe neue Methoden oder Funktionselemente heraus und diskutiere in der Gruppe (2-3 Personen nicht mehr!) ihre Funktionsweise. (Schriftlich)
letzte Änderung: 02.09.2010