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

Forging The Rings!

From Huben's Wiki
Jump to: navigation, search
Line 69: Line 69:
 
* Now we can print out all the rings created, and how many were forged.
 
* 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.
 
* 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.  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()
 +
{
 +
  Ring("iron", "a human King");  // calling the other constructor
 +
}
 +
</pre>
 +
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 to accomplish most or all of the task for you.
 +
 +
* Add this latter constructor to class Ring.
 +
* 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()).

Revision as of 14:27, 18 October 2012

Personal tools
translate