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
(Another Constructor)
 
Line 30: Line 30:
 
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!
 
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 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.
+
* 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!
 
Let us now show how this is used to forge rings!
Line 70: Line 70:
 
* 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 ==
 
== 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:
+
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>
 
<pre>
 
public Ring()
 
public Ring()
Line 85: Line 85:
 
public Ring()
 
public Ring()
 
{
 
{
   Ring("iron", "a human King");  // calling the other constructor
+
   this("iron", "a human King");  // calling the other constructor
 
}
 
}
 
</pre>
 
</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.
+
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.
 
* Add this latter constructor to class Ring.

Latest revision as of 15:21, 19 October 2012

Personal tools
translate