Radford University ITEC

ITEC120

lab06:
get, set, ... construct!

Today we'll work in pairs, trying BlueJ features we've seen in class, and adding our own constructors. There will be little snags to work around and changes you'll need to make to the code; discuss with your partner, what/how/why certain changes work (or, don't work).


For Sep.29 (Fri)

With a partner, finish the above tasks, and the one next method, and we'll check them off.

solution

toString, and more behind-the-scenes Java

Recall how the code "Catch-" + 23, which looks like it's trying to concatenate two things which aren't both Strings, actually gives a String result: Java, behind the scenes, converts the int 23 into a String4 (at which point doint string-concatenation is no problem).

This help from Java is convenient, when we want to convert ints and doubles into Strings. But Java generalizes this philosophy of “convert-numbers-into-strings-as-needed” a bit further, to all values, not merely numbers! Try typing this in BlueJ's codepane:

Cadillac myRide = new Cadillac( whatever-arguments-needed-for-constructor );
"I bought " + myRide + " at Carry's Caddy Co."
It is surprising that Java even tries to convert myRide into a String for us (after all, when Java was written, our code class Cadillac hadn't even been invented!). On the other hand, it isn't surprising5 that Java can't do a nicer job of converting a Cadillac into a String, since it doesn't have any real idea of what information about the instance should be printed.

Here is what was happening: Java converted "I bought " + myRide + " at Carry's Cadicallac Co." into "I bought " + myRide.toString() + " at Carry's Cadicallac Co.". And, Java (again working behind the scenes) wrote its own version of toString for Cadillacs for us.

But if we can do a better job, we can write our own method toString for Cadillacs, “overriding”6 Java's default version toString:

/** toString: Return a String representation for this Cadillac.
 * @return A string, showing the state of this Cadillac (non-static fields only).
 */
public String toString() {
  return 
  }
An example string which might get returned7 is: "[Cadillac: price $21000, VIN "4YTV7 2AB22"]". Write toString, and test it.

Then, try the following:

Cadillac myRide = new Cadillac( whatever-arguments-needed-for-constructor );

"I bought " + myRide.toString() + " at Carry's Caddy Co."
"I bought " + myRide            + " at Carry's Caddy Co."
These two lines generate the exact same result — because Java (behind the scenes) converts the last line into the next-to-last line for us, automagically.

Since Java lets us override toString for any class, we'll often take advantage of this, and require it for most future assignments. Note that in software companies, it's often required to have all classes override toString; this policy is helpful when inspecting objects which were passed to you from other parts of a project:

If you have some object, just call its method toString, to get a String describing that object.

public?

Note that We're having you add one new, mysterious word: “public”. We'll discuss this keyword in lecture on Monday, but as a quick preview: it means that other classes (besides class Cadillac) are allowed to call this method. For example a PizzaServer would be able to walk up to a Cadillac and call its toString method, but the PizzaServer might not be allowed to call a car's setPrice method: that method might be “private”, and perhaps only Carry will be allowed to set a car's price.

Java's policy that “anybody can call an object's toString method” entails that toString always be public. Java will complain if you define this method but try to make it private (or, if you leave off the public keyword at all).


1A 12-sided solid is a dodecagon -- “dodec” meaning 2+10. The etymology is similar to words like “thirteen”, which means 3+10. (One comes from latin roots, the other is anglo-saxon?)      back

2When first hearing this, you might wonder if it's always possible to do this. Sure, for six-sided dice maybe, but for 44-sided dice? 3-sided dice? If you start trying to make a case that this is (or isn't) always possible, then you are a natural mathematician! Consider a major/minor in math, as well as ITEC.      back

3If her standard-initial price of $23,500 ever changes, she's willing to re-start her Java program.      back

4 In fact, as we may have learned on the exam1 review sheet, "Catch-" + ((new) Integer(22)).toString() is what it does. After that, it even converts the + into an explicit call to string-concatenation: (new String("Catch-")).concat( ((new) Integer(22)).toString() ), which is quite a mouthful.      back

5 Now that I think about it, Java has access to the source code, and the names of the fields of our class, so hey actually I do expect Java to be able to do better than it currently does, when translating brand-newly-defined classes into Strings.      back

6 We'll talk more about overriding general behaviors with specific ones, near the end of the semester. It is a general (and powerful) concept.      back

7 Remember, if you want to embed a double-quote-character ‘"’ inside of a String literal in your program, use “\"”, as in “"She is 5\' 6\" tall."”      back


©2006, Ian Barland, Radford University
Last modified 2006.Sep.29 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme