Java 13 - Object Oriented Concepts

Modules vs. Objects

  1. 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
  2. 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.
  3. 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.
    1. 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.
  4. 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.
  5. 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
  6. DecimalFormat DollarFormat;
  7. 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.
  8. 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
  9. DollarFormat = new DecimalFormat;
  10. 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
  11. DecimalFormat DollarFormat = new DecimalFormat;
  12. 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:
  13. DollarFormat.minDecimalPlaces(2);
    DollarFormat.maxDecimalPlaces(2);
  14. After that, we used the object in our print statements to print out our values in the format we wanted:
  15. System.out.print(DollarFormat.format(Interest));
  16. In this same program, we could have had another DecimalFormat object for printing percentages. The program might have looked like:
  17. 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));
  18. 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.
  19. Where else have we already used objects?
    1. Actually, anywhere we were using something of a type that began with a capital letter
    2. 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.
    3. 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:
    4. BufferedReader stdin = new BufferedReader (
                             new InputStreamReader (System.in));
    5. 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.
    6. 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.

An example program that uses object to calculate trip mileage, and another that does two trips.