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 60: Line 60:
 
* The third line of main() shows another way to invoke the speak() of DayTheEarthStoodStill.  Why does this work?
 
* The third line of main() shows another way to invoke the speak() of DayTheEarthStoodStill.  Why 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?
 
* 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?
 +
 +
==Method Calls In Real Programs==
 +
It is easy to identify method calls in the toy programs on this page.  It is also easy to identify them in real programs.
  
 
Class names start with upper case letters and method names start with lower case letters.  Statements such as
 
Class names start with upper case letters and method names start with lower case letters.  Statements such as
Line 70: Line 73:
 
* x is a '''variable''' that is the '''argument''' we are giving to Math.cos().
 
* 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().
 
* 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.
+
* Variable names also start with lower case letters.
 +
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.  Variables are places we can store information, such as numbers.  More on variables soon.
  
 
Slightly more complicated,
 
Slightly more complicated,
 
<pre>
 
<pre>
sound = animal.makeSound(aTypeOfSound);
+
sound = animal.makeSound(aTypeOfSound, volume);
 
</pre>
 
</pre>
 +
in this example "animal" is not a class.  It is a kind of variable called an '''object'''.  Objects belong to classes, and can call the methods of their class.  Thus:
 +
* makeSound() is a method in some class. (We can figure out which class later.)
 +
* animal is a variable that is an object.
 +
* animal's class has the method makeSound().
 +
* aTypeOfSound and volume are variables that are arguments for makeSound().
 +
* sound is a variable where we store the result of animal.makeSound(aTypeOfSound, volume).
 +
 +
Let's look at some real code now.  Here is the main from a class Cryptogram:
 +
<pre>
 +
  public static void main(String[] args)
 +
  {
 +
    Cryptogram window = new Cryptogram();
 +
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
 +
    window.setBounds(30, 30, 800, 600);
 +
    window.setVisible(true);
 +
  }
 +
<pre>
 +
* The first statement, "Cryptogram window = new Cryptogram();" creates an object named "window" that has class Cryptogram.
 +
* The first statement uses the method Cryptogram() to create the object.  As we mentioned before, methods with the same name as their class start with capital letters and are called '''constructors'''.  They make new objects, in this case window.
 +
* The remaining statements are all method calls.
 +
* What class is window?
 +
* What class are these methods for?
 +
* How many arguments does setBounds() have?
 +
* Do these 3 last method calls return any values?
 +
 +
All you need now are variables, objects, arguments and conditionals: then you shall understand most of the wiles of these demons in Java code.

Revision as of 00:16, 18 September 2012

Personal tools
translate