Chapter 6: Intro to OOP

The exercises here are taken from my forthcoming book The Java Developer's Resource.

Quiz

  1. What's the difference between an object and a class?

    An class merely defines what an object will be if it is created. An object is an actual, created instance of a class. A class is like a blueprint for a house, while the object is the actual house. A single blueprint can be used to make many different houses.

  2. What's the difference between an instance of a class and an object in that class?

    There is no difference. These are two ways of saying the same thing.

  3. What's the difference between a local variable and a member variable?

    A local variable is only accessible within the method where it's declared. A member variable, a.k.a. a field, is accessible in any method in the class.

  4. What's the difference between a static field and a non-static field?

Exercises

  1. Convert the command line programs of exercises 1, 5, 7, 8, 9, 10, 11, and 12 from chapter 4 into methods that do the calculations but don't handle any input. Then write a main method to handle the input and call the new methods. Do you notice anything similar about the new main methods?

  2. Java lacks a complex data type. Write a Complex class that represents a single complex number and includes methods for all the usual operations, i.e. addition, subtraction, multiplication, division, etc.

    Complex numbers are one of the features needed in a programming language for serious scientific computation. Unfortunately no popular computer language other than Fortran provides them as a built-in data type. (Actually this is such a common and useful example and was used by so many textbooks that it was recently added to the C++ stabdard library which makes it far less useful as an example. Fortunately, however, Java has not yet been around long enough to have all its really useful examples coopted into the standard library.) Let's see how we might implement them in Java. From the standpoint of a data type you really don't need much. Mathematically a complex number is composed of a real part u and an imaginary part v. We can create such a class in the following way:

    public class ComplexNumber extends Object {
    
      public double u;
      public double v;
    
    }
    While this is sufficient to encompass all the data that one needs in a complex number it's not a very good example of object-oriented programming. To actually do anything with this number we have to know exactly how the data structure is defined. If we change the data structure, for instance by defining a complex number in terms of it's magnitude r and its argument theta instead of by its real and imaginary components we have to change all the code that depends on it.

    We also have to write code to explicitly add the numbers, multiply them or do anything else we might need to do with complex numbers. If we need to add complex numbers in more than one place, then we need to write the addition code again, or, at the very least, copy and paste it.

    A better implementaion of a complex number class will shield us from the exact storage of the data, i.e. x and y vs. r and theta. It will also provide methods that let us perform any operation we might need to perform on or with a complex number.

    Before writing code we need to ask ourselves what we'll do with a complex number. Most objects first require a constructor, a method that is called when you create a new complex number. A more complicated object may also require a destructor method that's called when you get rid of an object; but since this is a fairly simple object, we'll let Java's built-in garbage collection take care of that for us.

    Since these are complex numbers it's not unlikely that we'll need to add them, subtract them, multiply them and divide them. We'll also want to be able to access their real and imaginary parts as well as their absolute values and arguments. The following class does all that.

    //
    public class Complex extends Object {
    
      private double u;
      private double v;
    
      Complex (double x, double y) {
    
        u=x;
        v=y;
    
      }
    
      public double Real () {
    
        return u;
    
      }
    
      public double Imaginary () {
    
        return v;
    
      }
    
      public double Magnitude () {
    
        return Math.sqrt(u*u + v*v);
    
      }
    
      public double Arg () {
    
        // Need to watch out for zero
        return Math.atan(v/u);
    
      }
    
      // Add z to w; i.e. w += z
      public Complex Plus (Complex z) {
    
        
        return new Complex(u + z.u, v + z.v);
    
      }
    
      // Subtract z from w
      public Complex Minus (Complex z) {
    
        return new Complex(u - z.u, v - z.v);
    
      }
    
      public Complex Times (Complex z) {
    
        return new Complex(u*z.u - v*z.v, u*z.v + v*z.u);
        
      }
    
    
      // divide w by z
      public Complex DivideBy (Complex z) {
    
        double rz = z.Magnitude(); 
    
        return new Complex((u * z.u + v * z.v)/(rz*rz),(v * z.u - u * z.v)/(rz*rz));
    
      }
    
    }
    Notice especially that x and y are now private. They cannot be accessed by external code even if we want them to be.

  3. Hard: Create a VeryLong class that will store an integer of arbitrary length. The methods of this class should be similar to those of the Complex class.

  4. Using the VeryLong class of the last problem, create a Rational class that handles arbitrarily precise fractions.

  5. Hard: Modify the toString() method of the Rational class so that it outputs results as mixed numbers in reduced form (i.e. 1 + 3/8 instead of 1 + 6/16 or 11/8). Hint: The greatest common denominator of two numbers u and v is the same as the greatest common denominator of u and u - v.


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified August 22, 1996