Radford University ITEC

ITEC120

variables which vary; intro to Objects

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.

Is everything final?

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." );
  }
Note how the first time we mention discSize, it's being declared (as well as initialized). When declaring a variable, we must mention its type. Later on, we are just assigning to the variable, and we are not allowed to re-specify that it is of type double. (If you did so, Java would think you were giving a second -- possibly different? -- declaration for the same variable name, which isn't allowed.)

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 Syntax and Semantics of variables

Objects, and References to Objects

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
Powered by PLT Scheme