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).
We retreated in class a bit, and had set the initial value of the field face to be 1, because we don't know the largest size until somebody actually creates a new Die. This made marketing sad, and sales plummeted since nobody bought dice which said 1 straight out of the factory.
Change the constructor so that we really do set the face of the die to be the maximum value. Change the place where the field face is first declared, and don't give it any initial value there.
Test that you can still create and roll a Die.
/** Look at the bottom of the die, without changing the Die's state. * @return the number on the *bottom* face of the Die. */ int getOppositeFace() { ... } |
The manager of Carry's Cadillac Company has determined that when people are considering whether to buy a particular Cadillac, there are really only a few things that they ever ask about: The car's price, its miles-per-gallon (as determined by government tests), its Vehicle ID Number (the “VIN”), and the state tax rate on buying cars.
Carry hires you to make a Java class representing this information about the Cadillacs in her lot. You start wondering what data (fields) you need to keep, and whether each of the fields are static, and whether they are final. You go back and get some clarifications from her:
Your task:
Start to write class Cadillac.
Include fields for the above information,
deciding whether each field is final, static, and
can be initialized when you declare the field,
or whether you can't initialize it until the Constructor is called.
Then, write the constructor.
(You'll need to decide, which information the constructor
needs to be given when each car is made!)
You don't need to write any methods, but should be able to
construct Cadillacs instances (and inspect them in BlueJ).
To think about:
which fields will have getters? Which will have setters?
With a partner, finish the above tasks, and the one next method, and we'll check them off.
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." |
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 … } |
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." |
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.
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 |