ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
In lab, we saw code a method public String toString()
(for each of class Die, class Player),
and we said that
public meant that any other object can walk up to
a Die and ask it to convert itself toString.
If a method or field is declared as private, then
other classes cannot use it.
[Demo, making the fields of Die private.
What about the random-number generator randy,
should we provide getter/setter?
Should randy even be mentioned in toString?
What about the access privilege for setFace?]
From now on: Make all fields private (always), and all methods public (for now). You only need to provide getters and setters for fields you want other classes to be able to get/set. Within the class itself, you can just refer to the field itself.
What exactly1 happens, when you call a constructor, like new Blarg(…)?
Constructors rarely do anything interesting, besides accept some values which (almost always) are directly put into fields. Note that there are two common naming conventions: You can use parameter names which start with underscores (but otherwise have the meaningful name corresponding to the field):
class Blarg { int blargNum; String blargName; double blargDub = Double.MIN_VALUE: bool blargTF; /** * Construct a Blarg, given */ Blarg( int _blargNum, String _blargName ) { blargNum = _blargNum; blargName = _blargName; blargTF = false; // This initial value might be provided when // the field was declared, as with blargDub. } |
class Blarg { int blargNum; String blargName; double blargDub = Double.MIN_VALUE: bool blargTF; /** * Construct a Blarg, given */ Blarg( int blargNum, String blargName ) { this.blargNum = blargNum; this.blargName = blargName; this.blargTF = false; // This initial value might be provided when // the field was declared, as with blargDub. } |
While it's possible to call a method (in the same class) from a constructor, you should be very careful about doing this: People writing the other methods always assume the object being worked with (this) is in a valid, fully-initialized state. But if you call it from the middle of a constructor, you're only half initialized! Even getters and setters should be avoided in this situation. If you have code which needs to be done both in the constructor and in regular methods, then you'll have to be creative2. Fortunately, this situation doesn't arise often, as constructors usually just initialize fields more-or-less directly from their parameters.
I'll put out a drill-homework as extra credit; it will feature series of questions, all of the form:
Read in book, Chapter 5: The type boolean is much simpler than (say) int, because rather than 4+ billion ints, there are only two boolean values: true, and false. Note that these two names aren't variables; they're values, just like 17 and -34 are.
There are some functions in Java which take two numbers, and return a boolean: >, >=, ==, !=, etc..
There is only one built-in Java statement which really uses booleans: that's the if-else statement. Syntax:
if (condition) { statements… } else { statements… } |
/** * @param animalType The type of animal (e.g. "newt") * @param numInStock The number of the given animal in stock. * @return A complete sentence describing the inventory * For example: * inventoryMessage( "dog", 3 ) = "There are 3 dogs in stock." */ String inventoryMessage( String animalType, int numInStock ) { return "There are " + numInStock + animalType + "s" + " in stock."; } |
/** * @param animalType The type of animal (e.g. "newt") * @param numInStock The number of the given animal in stock. * @return A complete sentence describing the inventory * For example: * inventoryMessage( "dog", 3 ) = "There are 3 dogs in stock." */ String inventoryMessage( String animalType, int numInStock ) { String answer; if (numInStock == 1) { answer = "There is " + numInStock + " " + animalType + " in stock."; } else { answer = "There are " + numInStock + " " + animalType + "s" + " in stock."; } return answer; } |
We can do better: Where do the two solutions differ? In only two places: “is” vs. “are”, and the suffix “s” vs. no suffix at all. We can factor out the common code, and let our if statement focus on just those differences:
/** * @param animalType The type of animal (e.g. "newt") * @param numInStock The number of the given animal in stock. * @return A complete sentence describing the inventory * For example: * inventoryMessage( "dog", 3 ) = "There are 3 dogs in stock." */ String inventoryMessage( String animalType, int numInStock ) { String verbForm; // The correct verb for our result. String plurality; // The noun-suffix, correctly plural or singular. if (numInStock == 1) { verbForm = "is"; plurality = ""; } else { verbForm = "are"; plurality = "s"; } return "There " + verbForm + " " + numInStock + " " + animalType + plurality + " in stock."; } |
Another example:
In your book's reading of Chpt.5, you saw that
/** * @param animalType The type of animal (e.g. "newt") * @param numInStock The number of the given animal in stock. * @return A complete sentence describing the inventory * For example: * inventoryMessage( "dog", 3 ) = "There are 3 dogs in stock." */ String inventoryMessage( String animalType, int numInStock ) { String verbForm = "is"; // The correct verb for our result. String plurality = ""; // The noun-suffix, correctly plural or singular. if (numInStock != 1) { verbForm = "are"; plurality = "s"; } return "There " + verbForm + " " + numInStock + " " + animalType + plurality + " in stock."; } |
if (numInStock == 1) verbForm = "is"; else verbForm = "are"; |
if (numInStock == 1) verbForm = "is"; plurality = ""; // DANGER, WILL ROBINSON! else verbForm = "are"; plurality = "s"; |
/** Return a greeting, selected randomly from a list of four greetings. * @return A greeting (randomly selected, not necessarily uniformly). */ public static String greet() { int choice = (new java.util.Random()).nextInt(100); if (choice < 30) { return "Hello."; } else if (choice < 33) { return "Aloha."; } else if (choice < 50) { return "Buenos dias, amigo/amiga."; } else if (choice < 99) { return "Yo."; } else if (choice < 100) { // Not an advisable greeting. Use sparingly. return "System dot out dot println open \"hello\" close."; } String warning = "*** This line of code is unreachable! ***"; } |
1 Well, this isn't the whole story, as we ignore initialization blocks, as well as not mention static (class) initializon nor superclass constructors. back
2 Probably, your best solution is to create a static (presumably private) method which does that common code, and invoke that method by explicitly passing this as an argument. The documentation for that static method should clearly state that this method is called from the constructor, and specify which parts of the object must be initialized to call it. back
3So really, the actual syntax for Java's if-else statement is
if (statement) expression else expression |
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
©2006, Ian Barland, Radford University Last modified 2006.Nov.09 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |