RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect06c
Classes are Types
and references are values

Review Exercise

Classes are Types

Compare:

Math.sqrt(25.0)

// versus:

double x;      // declare a variable (a box) labeled `x`
x = 25.0;      // initialize x (put a value into the box)
Math.sqrt(x);  // use (the contents of the box labeled) `x`
These two cases both compute the square root of 25; it's just that one of them attaches a name to 25 before using it.

Now compare:

(new PizzaServer()).pizzaArea(25)

// versus:

PizzaServer jo;         // declare a variable (a box) labeled `jo`
jo = new PizzaServer(); // initialize jo (put a value into the box)
jo.pizzaArea(25);       // use (the contents of the box labeled) `jo`

Note that: Calling a function (possibly a constructor) and getting a result back is a different task from storing that result in a variable.

Methods which take objects as inputs

  /** Does this PizzaServer make more money (per hour) than some rival?
   * @param rival The PizzaServer to compare against.
   * @return true if this's salary is strictly bigger than rival's salary.
   */
  boolean hasHigherSalaryThan( PizzaServer rival ) {
    return true; //  A stub function; will write the rest later.
    }

Now that we've recorded some test cases (even though they don't currently pass), we can go back and actually implement the function:

Putting the above questions together leads us naturally to:
  /** Does this PizzaServer make more money (per hour) than their rival?
   * @param rival The PizzaServer to compare against.
   * @return true if this's salary is strictly bigger than rival's salary.
   */
  boolean hasHigherSalaryThan( PizzaServer rival ) {
    return (this.getSalary() > rival.getSalary());
    }

Note that the series of questions we asked can make sense when writing the body of any method. It's the last step of The Design Recipe!

What happens, when the function is called?

We will step through this function. In fact, we can put a stop-sign at the line of code inside hasHigherSalaryThan, and run our tests. We've seen the debugger window before. Note that under fields we see this's fields, and under local variables we see the parameter rival.

Especially note that the value of rival is “object reference”. This means that really, rival is a variable whose value is an arrow pointing to a particular object. This is subtly different than the variable storing the object itself; we'll see exactly why, later.

Imaging the following is typed at the Code Pad:

1. PizzaServer p1;
2. p1 = new PizzaServer( "Radford" );
3. 
4. PizzaServer p2;
5. p2 = new PizzaServer( "Pulaski" );
6. 
7. p1.hasHigherSalaryThan( p2 );
8. 
9. p1.hasHigherSalaryThan( p1 );

Let's role-play what happens on each line. As the instructor, I'll be the code pad. Line 1 declares a variable (local to me). Line 2 actually (a) calls a constructor -- we need a volunteer to be a PizzaServer object -- and (b) initializes my local variable with a reference to that new object (a piece of string attached to that volunteer). Note that our volunteer has a sheet attached to them, listing all their fields.
Lines 4 and 5 are similar.

Line 7 is interesting:

Finally, in line 9, the same thing happens. The only thing to raise an eyebrow is that the parameters (local variables) rival and this happen to hold the same reference inside of them. That's not a problem; it's no different than having two int variables which both happen to contain 17.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Jan.24 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme