Creating Uruk Hais

From Huben's Wiki
Jump to: navigation, search

Saruman did not create single monsters to attack the lands of men. Each class is a race composed of many individuals (instances or objects.) The most horrible of the races Saruman created were the UrukHai, a form of Orc. If Saruman had created only one UrukHai, it would not fare well against the armies of men. But Saruman created a race, and could produce plentiful UrukHai for his battles.

Contents

Build Saruman's Army!

Place class Saruman and class UrukHai into Dr.Java.

class Saruman  // Saruman the White, from the tower of Orthanc at Isengard.
{
  static public void main(String[] args)
  {
    UrukHai one = new UrukHai();  // create an instance
    UrukHai two = new UrukHai();  // create a second instance
    
    // Set the instance variables of both instances.
    one.individualWeapon = "short sword";  // set public instance variable individualWeapon
    one.setIndividualNickName("Brad");  // set private instance variable individualName
    
    two.individualWeapon = "scimitar";  // set public instance variable individualWeapon
    two.setIndividualNickName("Janet");  // set private instance variable individualName
   
    // Both instances declaim their static fields and instance variables.
    one.declaim();
    two.declaim();
  }
}
class UrukHai  // a form of Orc
{
  // static fields are the same for ALL objects of this class.
  static public String sharedBattleCry = "Yaarrrrrgggghhhhh!";
  static private String sharedAlliegance = "Saruman";
  
  // Each object has its own set of non-static fields (instance variables)
  public String individualWeapon;
  private String individualNickName;
  
  static public void screamTheSharedBattleCry()  // a class method
  {
    System.out.println("Ready? 1, 2, 3: " + sharedBattleCry);
  }
  
  public void declaim()
  {
    System.out.println("My name is " + individualNickName + " and I wield a " + individualWeapon + ".");
    System.out.println("My alliegance is to " + sharedAlliegance + " and my battle cry is " + sharedBattleCry);
    System.out.println("");
  }

  static public void setSharedAlliegance(String whom)  // a modifier method
  {
    sharedAlliegance = whom;
  }
  
  static public String getSharedAlliegance()  // an accessor method
  {
    return sharedAlliegance;
  }

  public void setIndividualNickName(String whom)  // a modifier method
  {
    individualNickName = whom;
  }
  
  public String getIndividualNickName()  // an accessor method
  {
    return individualNickName;
  }

  public String askNickNameOfOtherUrukHai(UrukHai other)  // look at instance variables of another member of class
  {
    return other.individualNickName;
  }
}

Compile and run.

What Is An Object

We create classes primarily to create objects in those classes. Objects know two things: their class and a bunch of variables (fields.) Each object has sets of fields declared by the class. Objects are stored in computer memory, and we create variables of their class to store the locations in memory. Strings are objects. If I create a String variable like this:

String s = "example string";

a string object containing the information "example string" is created somewhere in computer memory, and the variable 's' is set to that location. Variables storing locations of objects can be called pointers' or references. We can say they point to the object or refer to the object. The type of those variables is the type of their object's class.

  1. What does 's' have in it?
  2. Is 's' an object or a variable?
  3. what is the type of 's'? (This is easy!)
  4. How many String objects can you have in your program?

Fields Of Battle.

Until now, we have only used class fields. Wouldn't it be great if we could have more than one set of fields? That is what objects allow. Look at class UrukHai. Each object (instance) has its own set of instance variables (non-static fields) with the same names but that store different information for each object. Each object also shares the set of class fields (class variables or static variables), but they are not in the object: they are in the class. Objects do not contain arguments or local variables: those are for methods only.

Thus, when using a class and objects of that class, there is ONE set of class variables but there will be as many sets of instance variables as objects created.

Look at UrukHai.

  1. Which are the instance variables?
  2. Which are the class variables?
  3. How can you tell class variables from instance variables?
  4. When I create an UrukHai object, what variables will be in it?

Understanding Army Life.

You have run Saruman. The main() creates two instances of class UrukHai. Notice that it uses a special keyword, new, to create each instance. Then it sets their separate instance variables two ways: directly with the dot notation, and with a method from class UrukHai. Notice that the dot notation we used with classes before can be used with instances as well, both for calling methods and setting fields. Then, the two instances each call declaim() to shout the values of their variables.

  1. What are the names of the two UrukHai variables?
  2. Where are the UrukHai objects located?
  3. What do the UrukHai variables have in them?
  4. What keyword was used to create the objects?
  5. What kind of method follows the keyword?
  6. How can you spot object creation in programs?
  7. Why do the two objects have the same battlecry but different weapons?
  8. Which method is used to set an instance variable of each object?
  9. Which instance variable of each object is set directly?
  10. If you inserted 'one.sharedBattleCry = "Meep!";' before the declamations, what would change in the declamations? Why?

Static And Regular Methods

The static reserved word distinguishes class variables from instance variables. It also has an important meaning for methods. A static (class) method may not use any instance variables, though it can use class variables. A static method may be called by its class or by one of the instances of the class with the dot notation. But a non-static, regular method cannot be called by its class because the class has no instance variables and the method might use them.

  1. Paste the line 'UrukHai.screamTheSharedBattleCry();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  2. Paste the line 'one.screamTheSharedBattleCry();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  3. Paste the line 'UrukHai.declaim();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?

You can throw away the 3 newly pasted lines now.

It Is Forbidden!

At last the secret meanings of the keywords public and private may be revealed to you.

  • Public means that a variable or method of a class may be used by any other class.
  • Private means that a variable or method of a class may be used only within the same class.

This is the secret of encapsulation, the principle of hiding the inner workings of a class. The public parts of a class are called the interface, the private parts are concealed.

If you look at Saruman.main(), you will notice that ALL of the UrukHai methods called are declared public. All of the variables set (without a method call) are also declared public.

  • Private UrukHai methods can only be called within other UrukHai methods.
  • Private UrukHai methods can only be used within UrukHai methods.

But say you really want to be able to change or see private variable values? Then you would create public methods for doing so, called accessors and modifiers. Usually accessors start with 'get' and modifiers start with 'set'.

  1. What modifier method do we use in main()?
  2. What field of what class does that modifier set?
  3. Look at UrukHai. Which combinations of public and private, class and instance variables are used?
  4. What are the two accessors?
  5. What are the two modifiers?
  6. Why wouldn't you write an accessor/modifier pair for sharedBattleCry?
  7. Paste the line 'String s = one.sharedAlliegance;' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  8. Paste the line 'String s = one.getSharedAlliegance();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  9. Paste the line 'String s = one.sharedBattleCry;' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  10. Paste the line 'one.individualNickName = "Mork";' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  11. Paste the line 'one.individualWeapon = "LOIC";' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?
  12. Paste the line 'one.setIndividualNickName("Cutie");' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. If not, is there a helpful error message?

Summation

Please write in your own words:

  1. The difference between a variable of some class type and an object of that class.
  2. What the 'new' keyword signifies.
  3. What is the method after the 'new'?
  4. The difference between static and non-static for variables.
  5. The difference between static and non-static for methods.
  6. The difference between public and private for variables and methods.
  7. Add a private instance variable to UrukHai: your choice.
  8. Write an accessor and a modifier for the new instance variable.
  9. Set the new instance variable in Saruman.main() for each of the two instances.
  10. Change the declaim method to show the new instance variable also.
  11. Create a third UrukHai, set all its variables, and have it declaim(). Please type everything, no cutting or pasting.
Personal tools
translate