The Demons Command Each Other

From Huben's Wiki
Jump to: navigation, search

Gort.jpeg

We have penetrated the mysteries of finding demon commands. Now we must seek to use those commands, and thus control our demons.

Contents

(A) First Call

Place this class into Dr.Java, save, compile and run it.

public class DayTheEarthStoodStill
{
  static public void main(String[] args)
  {
    System.out.println("Help! Save us!");
    speak();
    System.out.println("Thank goodness, the world is saved!");
  }

  static public void speak()
  {
    System.out.println("Gort: Klaatu barada nicto!");
  }
}

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
  • have a return type
  • are followed by parentheses enclosing arguments
  • and then are followed by the blocks we call their bodies.

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?

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?

(B) Calling Between Classes

Click the New button to start a new Java class, place this code in it, save it and compile it.

public class Nietzsche
{
  static public void speak()
  {
    System.out.println("Thus spake Zarathustra!");
  }
}

Change the main of DayTheEarthStoodStill to have this code:

  static public void main(String[] args)
  {
    speak();
    Nietzsche.speak();
    DayTheEarthStoodStill.speak();
  }

Compile and run DayTheEarthStoodStill. (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 2001: A Space Odyssey. You may click to play that one.)

We now have two demon classes, DayTheEarthStoodStill and Nietzsche. The main() in DayTheEarthStoodStill knows its fellow methods, and can call them directly by their names. But 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?

  • How do we identify the speak() method of Nietzsche in the main() of DayTheEarthStoodStill?
  • 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?
  • If you are looking at Nietzsche and hit run, for once the error message makes sense. Why does this give an error?

(C) 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

y = Math.cos(x);

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().
  • 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,

sound = animal.makeSound(aTypeOfSound, volume);

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).
  • We can be calling methods of different classes by using objects of different classes to make the calls. Notice the dot! Just like the dot when we call with a Class name.

Let's look at some real code now. Here is the main from a class Cryptogram:

  public static void main(String[] args)
  {
    Cryptogram window = new Cryptogram();
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setBounds(30, 30, 800, 600);
    window.setVisible(true);
  }
  • 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?

(D) Do you get it?

Look at studentdisk/Ch07/Craps/RollingDie.java.

  • List the methods of class RollingDie.
  • How many arguments does each of those methods need?
  • Where are each of those methods called in class RollingDie (0 to many places.)
  • What is the name of the constructor method? Is there a coincidence involved?
  • How many different methods of class Math are called?

All you need now are variables, objects, arguments and conditionals: then you shall understand most of the wiles of these demons in Java code.

Personal tools
translate