Radford University ITEC

ITEC120

intro to Objects, continuted

Reading: Chapters 3 and 4, except for the sections on graphics (whose pages have green trim).
These notes are my take on those same topics, just with different emphasis.

Start reading Chapter 3 in the book. (More to follow here, later!)

Strings are objects (really used new); calling methods; references;

When we call a function, we don't actually pass an entire object, just a reference to the object: What is written down on the piece of paper is just a signpost which refers to the object.
[Actually, we won't be able to actually write a program confirming or denying fact, until we cover field-mutation, which isn't until Chpt. 4 at least.]

Review:

New material

Tues

Class Random

Class Random. (See book.)
Calling nextInt(), calling nextInt(10); how to emulate a die-roll; creating with new Random() (once we import java.util.Random), and creating with new Random(234) (make two, observe that they're in sync).

class Random { /** @return A random int, in Integer.MIN_VALUE...Integer.MAX_VALUE. */ int nextInt() { // ...code already written by others at Sun Microsystems. return ...; } /** @return A random int in 0..max-1. */ int nextInt( int max ) { return /* How can I write this function easily? */ ; } }

Class Math

Looking more closely at Math, we see it's static -- the functions like Math.sqrt and Math.floor don't have/need any state, and we didn't actually create any object using new.

In fact, it makes sense that in order to calculate a sqrt we don't need any object. So, sqrt is a static method or a class method (because it belongs to the entire class, not each instance.)

Which methods of PizzaServer should actually be static? Which can't be? Try this in BlueJ, and call the new method. Where is the method located?!

The concept applies to fields as well as methods: Consider Math.PI. Or, Integer.MAX_VALUE. In fact, all eternal-named-constants, which we used in all-caps, should be final static.

Wrapper classes

There are also classes Integer and Double. What do you suspect we'll pass the constructor for Integer? Yep, a lower-case int, such as new Integer(31). We'll give it a name, since it's too incontent to be a mere int: we'll say Integer fancyPants = new Integer(31);.

What on earth can we do with an Integer that we can't do with an int? Well, looking at the documentation, we see several methods. One is intValue, which gives us back the number wrapped inside the Integer (clearly necessary, but thoroughly unexciting). There is also doubleValue, which returns a double; this has the same effect as casting the primitive int. 2

A bit more interesting method in class Integer is toString: Indeed, fancyPants.toString() has the object return a text-description of itself (in this case, the character 3 followed by the character 1).

What about some of the other methods? Hmm, toHexString looks interesting; it claims to return a number written in base-16 (“hexadecimal”). So what happens when we try fancyPants.toHexString()? An error? With a totally unhelpful error message? It turns out, the problem is that toHexString is a static method, as we just discussed. Thus, we can't walk up to fancyPants and say “hey, give me a representation of you as a base-16 numeral (String)”. Instead, we give the function's full name and don't refer to any instance: Integer.toHexString(31).

Sep.21 (Thu)

(Review quiz1 and lab-quiz1. Announce the make-up chance, for these.)

Let's write a class representing a die (as in, half of two dice), from scratch. We actually have a series of questions to consider:

(Continued…)


2 I'll again lament that in Java, (int)6.02e23 and (new Double(6.02e23)).intValue() both return Integer.MAX_VALUE, rather than causing an error.      back

1 So, if you want to convert an int n into a double, should you (a) cast, e.g. (double) n or (b) call (new Integer(n)).doubleValue()?
Well, the cast is certainly shorter, but the second version just leverages the idea of calling a method and doesn't require a whole new syntax, like casting does. In practice, nobody bothers to write out the long form, and people use the cast. 2      back


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