Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860
The Demons Command Each Other - Huben's Wiki

The Demons Command Each Other

From Huben's Wiki
Jump to: navigation, search
Line 7: Line 7:
 
   static public void main(String[] args)
 
   static public void main(String[] args)
 
   {
 
   {
     saveTheWorld();
+
     System.out.println("Help! Save us!");
 +
    speak();
 +
    System.out.println("Thank goodness, the world is saved!");
 
   }
 
   }
  
   static public void saveTheWorld()
+
   static public void speak()
 
   {
 
   {
 
     System.out.println("Gort: Klaatu barada nicto!");
 
     System.out.println("Gort: Klaatu barada nicto!");
Line 16: Line 18:
 
}
 
}
 
</pre>
 
</pre>
This honorable demon saves the earth from destruction by the robot Gort.  There are two '''methods''', main and saveTheWorld.  We can spot them because they:
+
[http://en.wikipedia.org/wiki/The_Day_the_Earth_Stood_Still This honorable demon saves the earth from destruction by the robot Gort.] There are two '''methods''', main() and speak().  (When we discuss methods in writing, their names are followed by parens.) We can spot them because they:
 
* are indented only once
 
* are indented only once
 
* have a '''return''' type
 
* have a '''return''' type
Line 22: Line 24:
 
* and then are followed by the '''blocks''' we call their '''bodies'''.
 
* and then are followed by the '''blocks''' we call their '''bodies'''.
  
The first method, main, instructs the demon to use the second method, saveTheWorld.  This is called a '''method call''',or call for short.
+
The first method, main(), instructs the demon to use the second method, speak().  This is called a '''method call''', or call for short.
  
 
* Method calls look something like the method signatures.  How can you tell them apart?
 
* Method calls look something like the method signatures.  How can you tell them apart?
* What would happen if the main method had another call to saveTheWorld on another line?
+
 
* Create another method, named saveThrice, that calls saveTheWorld three times. Change main to call saveThrice two times.  How many times is the world saved?  Why?
+
The '''execution''' or '''running'''' of this program is interesting.  (Those are the muggle terms: we might use "invocation".)  When main() starts running because we click the Run button, the first line is executed, printing "Help! Save us!".  When main() executes the second line, it stops and speak() executes its only line, printing "Gort: Klaatu barada nicto!".  Then, since speak() is done, we return to main() and continue at the third line, printing "Thank goodness, the world is saved!"
 +
 
 +
* What would happen if the main() method had another call to speak() on another line?
 +
* Create another method, named speakThrice(), that calls speak() three times. Change main() to call speakThrice() two times.  How many times is the world saved?  Why?
  
 
==Calling Between Classes==
 
==Calling Between Classes==
Use File/New to start a new Java class, place this code in it, save it and compile it.
+
Click the New button to start a new Java class, place this code in it, save it and compile it.
 
<pre>
 
<pre>
 
public class Nietzsche
 
public class Nietzsche
Line 43: Line 48:
 
   static public void main(String[] args)
 
   static public void main(String[] args)
 
   {
 
   {
     Nietzsche nietzsche = new Nietzsche();
+
     speak();
     saveTheWorld();
+
     Nietzsche.speak();
     nietzsche.speak();
+
     DayTheEarthStoodStill.speak();
 
   }
 
   }
 
</pre>
 
</pre>
Compile and run DayTheEarthStoodStill.
+
Compile and run DayTheEarthStoodStill. ([http://en.wikipedia.org/wiki/Thus_Spake_Zarathustra Thus spake Zarathustra] is a famous philosophical novel by Nietzsche, and also a tone poem by Richard Strauss that is the most famous music in the film [https://www.youtube.com/watch?v=cWnmCu3U09w 2001: A Space Odyssey].  You may click to play that one.)
  
In order to use the method of class Nietzsche, we had to creat an '''instance''' of that class with the name nietzsche: we have created another demon! It too will obey our commands if we call it by nameWe shall speak more anon of instances.
+
We now have two demon classes, DayTheEarthStoodStill and Nietzsche. The main() in DayTheEarthStoodStill knows its fellow methods, and can call them directly by their namesBut that DayTheEarthStoodStill main() cannot call the methods in class Nietzsche by their names alone: say Nietzsche methods had the same names as those in DayTheEarthStoodStill?
  
* Change main to alternate the two sayings twiceYou need not recreate nietzsche.
+
* How do we identify the speak() method of Nietzsche in the main() of DayTheEarthStoodStill?
* make a method named alternate which will say each saying once, and use it three times in main to get six sayings.
+
* The third line of main() shows another way to invoke the speak() of DayTheEarthStoodStillWhy does this work?
 +
* What rule would you make to explain why sometimes you can call with just the name of a method and other times you need something more complex?
 +
 
 +
Class names start with upper case letters and method names start with lower case letters. Statements such as
 +
<pre>
 +
y = Math.cos(x);
 +
</pre>
 +
are fairly obvious:
 +
* Math is a class.
 +
* cos() is a method in class Math.
 +
* x is a '''variable''' that is the '''argument''' we are giving to Math.cos().
 +
* y is a '''variable''' where we are storing the '''return value''' from calling Math.cos().
 +
So in this example, we are giving the Math demon x, a value, and will receive in trade the value cosine of x, which we store in y.  If the variable x has a 0 in it, the demon will return a 1 to place into the variable y. More on variables soon.
 +
 
 +
Slightly more complicated,
 +
<pre>
 +
sound = animal.makeSound(aTypeOfSound);
 +
</pre>

Revision as of 23:31, 17 September 2012

Personal tools
translate