Modules vs. Objects
- We have seen the idea of modules in ML and in Java -- both languages
have a Math module, and in that module have a function
sqrt that we use as Math.sqrt. Other functions
in the Java Math module include cos, sin,
and pow. These are used by us as Math.cos,
Math.sin, and Math.pow
- But what if we had a module that might be used in more than one way? For
example, we used the DecimalFormat module to print our dollar amounts in
a 2-decimal-place format. We did that by setting the minimum and maximum
decimal-places options both to 2. What if, in the same program, we wanted
to print, say, percentages in a 3-decimal-place format? Then, what
we should be able to do is to have two DecimalFormat modules, one with
the options set to 2 decimal places, and one with the options set to 3
decimal places.
- Java lets us do this, and it calls these objects. In Java, a
class is a definition of a type of object, with which
you can then create specific objects of that type.
- Class and Type are interchangeable words. We have called "int"
a type, but we could also call it a class of numbers. The only difference
in Java is that types are built-in and predefined, whereas you can
create new classes.
- So, DecimalFormat is a class, and we created an object that we named
DollarFormat. This version of the class DecimalFormat was used to print
2-decimal-place numbers.
- The way you name and create objects from classes is very similar to
the way you name and create variables of the types integer, double, etc.
In fact, they are variables, just like your other variables. First you
declare the object name, like
DecimalFormat DollarFormat;
- That is exactly the same as saying "int x;" or "int
A[];" -- it simply declares a variable named DollarFormat
of the type (or class) DecimalFormat.
- Object variables are like array variables -- when you declare them,
you only declare a reference to an object of that type. So, you
must use new to create the object, just like you used new
to create the array. So after the declaration, we do
DollarFormat = new DecimalFormat;
- But, as with arrays and other variables, you can initialize object
variables right in the declaration. So we can combine the above two statements
into
DecimalFormat DollarFormat = new DecimalFormat;
- Once the object variable is declared, and an object is created (using
new), it can be used. In our lab, we first set some parameters
on the DollarFormat object by calling two of its methods:
DollarFormat.minDecimalPlaces(2);
DollarFormat.maxDecimalPlaces(2);
- After that, we used the object in our print statements to print out
our values in the format we wanted:
System.out.print(DollarFormat.format(Interest));
- In this same program, we could have had another DecimalFormat object
for printing percentages. The program might have looked like:
DecimalFormat DollarFormat = new DecimalFormat;
DecimalFormat PercentFormat = new DecimalFormat;
DollarFormat.minDecimalPlaces(2);
DollarFormat.maxDecimalPlaces(2);
PercentFormat.minDecimalPlaces(3);
PercentFormat.maxDecimalPlaces(3);
...
System.out.print(DollarFormat.format(Interest));
...
System.out.print(PercentFormat.format(SomePercentVariable));
- In this version, then, we have two DecimalFormat objects,
one that does 2-decimal-place formatting, and one that does 3-decimal-place
formatting. And they are completely separate. They are referred to with
different variables (DollarFormat and PercentFormat),
are created separately, and are used separately. The only thing they have
in common are they are of the same class, or type. They are both DecimalFormat
objects.
- Where else have we already used objects?
- Actually, anywhere we were using something of a type that began with
a capital letter
- The String type is a class that stores and manipulates strings of characters.
We never used it heavly, but it has methods for comparing strings, searching
strings, etc.
- The BufferedReader is a class that you used to read input, both from
the keyboard and from a file. When we did the declaration and initialization
like:
BufferedReader stdin = new BufferedReader (
new InputStreamReader (System.in));
- We were declaring an object called stdin that was a BufferedReader
object that used an InputStreamReader object that used the
predefined input stream System.in, which is the keyboard (or whatever
file was redirected using <). We could have named it anything
we wanted, but we chose stdin because in other programming languages
this is a common name for the keyboard input stream.
- The BufferedReader class of objects contains methods for reading a
line -- readLine() -- and a method to determine if there is any
more to read -- ready().It has more but we didn't use the other
methods.