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(): 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(): 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(): 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
Forging The Rings! - Huben's Wiki

Forging The Rings!

From Huben's Wiki
Jump to: navigation, search
(Created page with "Today we will study with [http://lotr.wikia.com/wiki/Rings_of_Power Celebrimbor, forger of the rings of power]. == The Ring Class == We must create a '''class Ring''' that has '...")
 
(Another Constructor)
 
Line 8: Line 8:
 
   private String givenTo;
 
   private String givenTo;
 
   public final String metal;
 
   public final String metal;
   private static String numberForged = 0;
+
   private static int numberForged = 0;
 
...
 
...
 
}
 
}
Line 25: Line 25:
 
}
 
}
 
</pre>
 
</pre>
Constructors such as this one are methods with arguments.  The arguments are variables. Their values are set when the constructor is called (right after a '''new''' keyword.)
+
Constructors such as this one are methods with arguments.  This constructor could be called with: '''Ring oneRing = new Ring("gold", "Sauron");'''
 +
The arguments are variables. Their values are set when the constructor is called (right after a '''new''' keyword.) theMetal is set to "gold" and whom is set to "Sauron".
 +
 
 +
Setting the metal and givenTo fields with the values of the arguments is obvious.  The metal field will never be able to be set again for this instance, because it is final.  But the subtle thing is that the class variable numberForged, which started at zero, is now increased by one every time this constructor is called, which means for every ring instance created.  We are counting the number of rings created!
 +
 
 +
* Add a toString() method to write: "I am a X ring, made for Y."  X should be the metal field, Y should be the givenTo field.
 +
 
 +
* Add a static forged() method that returns the String "There have been N rings forged."  N should be numberForged.
 +
 
 +
Let us now show how this is used to forge rings!
 +
 
 +
== Building The Forge ==
 +
<pre>
 +
class Forge
 +
{
 +
  static public void main (String[] args)
 +
  {
 +
    System.out.println(Ring.forged());
 +
    Ring oneRing = new Ring("gold", "Sauron");
 +
    System.out.println(Ring.forged());
 +
    Ring[] elvenRing = new Ring[3];  // Array of 3 rings for elves.
 +
    System.out.println(Ring.forged());
 +
    System.out.println(elvenRing[0]);
 +
    elvenRing[0] = new Ring("silver", "an elf");
 +
    elvenRing[1] = new Ring("silver", "an elf");
 +
    elvenRing[2] = new Ring("silver", "an elf");
 +
    System.out.println(oneRing);
 +
    System.out.println(elvenRing[0]);
 +
    System.out.println(elvenRing[1].toString());
 +
    System.out.println(elvenRing[2]);
 +
    System.out.println(Ring.forged());
 +
  }
 +
}
 +
</pre>
 +
Let's understand what's happening in Forge.main().  Forge.main() is a static method.  Without an instance variable of something, it can only call other static methods: such as Ring.forged(). 
 +
* The first time we call Ring.forged(), it shows that 0 have been forged.
 +
* The oneRing instance is created by calling the constructor and setting the constructor argument theMetal to "gold" and whom to "Sauron".
 +
* Now when we call Ring.forged(), it shows that 1 ring has been forged.
 +
* '''Ring[] elvenRing = new Ring[3]; ''' creates an array of three Ring ***VARIABLES***.  It does not create any Ring instances for those variables to point to.  Note the peculiar use of the square brackets instead of parentheses: no constructor is being called!
 +
* Ring.forged() shows that we have not created any new instances.
 +
* So what is in those three Ring variables, if they are not pointing to Ring instances?  The value '''null'''.  When we try to println() one of the Ring variables, elvenRing[0], the result is a null.
 +
* We laboriously create three Ring instances and assign them to each Ring variable in the array.
 +
* Now we can print out all the rings created, and how many were forged.
 +
* Notice that your toString() method is used explicitly to print elvenRing[1].  Then println() prints the string value that is returned.  But you obviously don't need to do that: we don't call toString() elsewhere.  What's going on?  Well, println() asks every object it is passed to use its toString() method, and then prints the string.  If you don't write your own toString(), there is a default toString() that prints the object's class and location.
 +
== Another Constructor ==
 +
Classes can have more than one constructor.  This is called '''overloading'''.  We are going to add a simpler constructor to class Ring, called a '''no-arguments constructor'''.  It will provide default values for the two instance fields and update the static field.  Here is the direct way to do it:
 +
<pre>
 +
public Ring()
 +
{
 +
  metal = "iron";
 +
  givenTo = "a human King";
 +
  numberForged++;
 +
}
 +
</pre>
 +
We have no choices with this no-arguments constructor, but these defaults are just fine for creating the nineRingsOfMen[].
 +
 
 +
Another way of writing a no-arguments constructor that avoids mistakenly forgetting to do something like set a field (or increment a field) is to call the already-existing constructor inside the no-arguments constructor.  Like this:
 +
<pre>
 +
public Ring()
 +
{
 +
  this("iron", "a human King");  // calling the other constructor
 +
}
 +
</pre>
 +
The special way to call your own constructors is to use '''this''' as the first line of the constructor body.  Not an obvious thing, so don't forget it.
 +
 
 +
Unless there are special circumstances, it is good style in Java to use the simplest way to accomplish a task, and that often means calling an existing method or constructor to accomplish most or all of the task for you. 
 +
 
 +
* Add this latter constructor to class Ring.
 +
The following changes are for class Forge.
 +
* Create an array of 9 Rings called nineRingsOfMen[], all constructed with the no-arguments constructor.
 +
* Add println() for the first and last of the 9 Rings.  Remember that arrays start at: __.
 +
* Add another println(Ring.forged()).

Latest revision as of 15:21, 19 October 2012

Personal tools
translate