Reading: all of Chapter 3
except for the sections on graphics (whose pages have green trim).
These notes are my take on those same topics,
just with different emphasis.
So far, every variable we have declared has been final, meaning its value can't be changed. This is a bit odd, because we haven't ever seen how to change the value of something.
void printSomeAdvertisingSpecs() { double discSize = 0.0; // Will hold different sizes, for passing to a function. System.out.println( "A " + discSize + "GB disk can hold " + gb2albums( discSize ) + " albums." ); discSize = 2.5; System.out.println( "A " + discSize + "GB disk can hold " + gb2albums( discSize ) + " albums." ); discSize = 5.0; System.out.println( "A " + discSize + "GB disk can hold " + gb2albums( discSize ) + " albums." ); discSize = 7.5; System.out.println( "A " + discSize + "GB disk can hold " + gb2albums( discSize ) + " albums." ); discSize = Math.pow( 3, 2 ); System.out.println( "A " + discSize + "GB disk can hold " + gb2albums( discSize ) + " albums." ); } |
Of course, we realize that as written, we have a bunch of repeated code. How to factor out that common code? We know how to do that!
What's going on in the environment, where the function looks up the value of discSize?
See example from Chapter 2, Geometry.java. That's an I/O bound example, so it's not too impressive, but note how (by using copy/paste) you could make it do the same thing 100 more times, which wouldn't be true if you'd hand-calculated the value of count for each call to println. (We will not emphasize this as an important technique, becase we'll want to get rid of copy/pasting altogether; we'll later see how to get the computer to count for us (“loops”).)
To consider: How would you abstract out the common code in Geometry.java?
Consider:
/** * @returns The number of albums which I can fit onto all the disc space I can find. */ int albumsInCollection() { int albumsSoFar = 0; albumsSoFar = albumsSoFar + gb2albums(0.75); // My entire H drive. albumsSoFar = albumsSoFar + gb2albums(0.5); // My phone. albumsSoFar = albumsSoFar + gb2albums(4.2); // Free space on my computer. albumsSoFar = albumsSoFar + gb2albums(2.0); // Borrow my friend's new iPod nano! return albumsSoFar; } |
"The value of n is " + n + "." |
name = expression; |
n = 2 * NUM_PEOPLE; m = n + 1; n = 7; // This statement does not retroactively change m's value; see Semantics. x = Math.sqrt(20); name = "Engelbert Humperdinck"; y = y + 1; jo = new PizzaServer(); // jo's value is a *reference* to a brand-new PizzaServer. The PizzaServer formerly known as jo might still exist, though we probably have no way to *refer* to that server. |
Java data can be one of a few primitive (built-in) types (int, boolean, double, …), and one more type of thing: objects.
Objects are things which have knowledge and behavior. For instance, we actually saw a PizzaServer object, named jo, who had a couple of behaviors (you could ask her to compute pieAreas, and you could ask her to computer crustAreas. (That was all there was to a PizzaServer; She didn't have any “knowledge”, though.)
Other objects we've seen are System.out, and java.util.Scanners.
Start reading Chapter 3 in the book. (More to follow here, later!)
Strings are objects (really used new); calling methods; references;
©2006, Ian Barland, Radford University Last modified 2006.Sep.12 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |