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
Creating Uruk Hais - Huben's Wiki

Creating Uruk Hais

From Huben's Wiki
Jump to: navigation, search
Line 6: Line 6:
 
<pre>
 
<pre>
 
class Saruman
 
class Saruman
 +
{
 +
  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();
 +
  }
 +
}
 
</pre>
 
</pre>
 
<pre>
 
<pre>
class UrukHai
+
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;
 +
  }
 +
}
 
</pre>
 
</pre>
  
Line 39: Line 103:
 
== Understanding Army Life. ==
 
== Understanding Army Life. ==
  
You have run Saruman.  The main() creates two instances of class UrukHai.  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.
+
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.
  
 
# What are the names of the two UrukHai variables?
 
# What are the names of the two UrukHai variables?
 
# Where are the UrukHai objects located?
 
# Where are the UrukHai objects located?
 
# What do the UrukHai variables have in them?
 
# What do the UrukHai variables have in them?
 +
# What keyword was used to create the objects?
 +
# What kind of method follows the keyword?
 +
# How can you spot object creation in programs?
 
# Why do the two objects have the same battlecry but different weapons?
 
# Why do the two objects have the same battlecry but different weapons?
 
# Which method is used to set an instance variable of each object?
 
# Which method is used to set an instance variable of each object?
Line 53: Line 120:
 
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.
 
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.
  
# Paste the line 'UrukHai.screamTheSharedBattleCry();' at the end of main().  Look at UrukHai, and predict if it will work.  Explain why.
+
# 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?
# Paste the line 'one.screamTheSharedBattleCry();' at the end of main().  Look at UrukHai, and predict if it will work.  Explain why.
+
# 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?
# Paste the line 'UrukHai.declaim();' at the end of main().  Look at UrukHai, and predict if it will work.  Explain why.
+
# 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! ==
 
== It Is Forbidden! ==
Line 63: Line 132:
 
* Private means that a variable or method of a class may be used only within the same 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.
 
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'.
 +
 +
# What modifier method do we use in main()?
 +
# What field of what class does that modifier set?
 +
# Look at UrukHai.  Which combinations of public and private,  class and instance variables are used?
 +
# What are the two accessors?
 +
# What are the two modifiers?
 +
# Why wouldn't you write an accessor/modifier pair for sharedBattleCry?
 +
# 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?
 +
# 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?
 +
# 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?
 +
# 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?
 +
# 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?
 +
# 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:
 +
# The difference between static and non-static for variables.
 +
# The difference between static and non-static for methods.
 +
# The difference between public and private for variables and methods.
 +
# What the 'new' keyword signifies.
 +
# The difference between a variable of some class type and an object of that class.
 +
# Add a private instance variable to UrukHai: your choice. 
 +
# Write an accessor and a modifier for the new instance variable.
 +
# Set the new instance variable in Saruman.main() for each of the two instances.
 +
# Change the declaim method to show the new instance variable also.

Revision as of 03:21, 1 October 2012

Personal tools
translate