So far you have been using public class variables. The word public makes them visible to the other classes, so they can be referred to in the a.realpart way. This is not a good way to design a class, for the following reason. Say you wanted to change the way the complex number was stored from Cartesian to polar, so you replace the public class variables realpart and imagpart with argument and modulus. Now you have a problem because anywhere in any other program that Complex objects have been used, you will have to change them to use the argument and modulus instead of realpart and imagpart.
It would be much better to somehow hide all the internal workings of the Complex class, so any alterations to the class did not require changes to be made to any of the other classes using Complex.
This is possible with a combination of private rather than public class variables, and methods that can be called to find out what these variable values are. Class variables that are declared as private can not be referred to from other classes, they are only visible within their own class. It is considered better programming practice to use private rather than public class variables, and you should aim to do this in the remainder of the course. Here is the Complex class rewritten using only private class variables.
/** * Purpose: A class from which objects might be created, which behave like * complex numbers. Private variables are used to demonstrate * good programming practice. */ public class Complex { // private class variables private double realpart; private double imagpart; // default constructor, real and imaginary parts are initialised to zero. public Complex() { realpart = 0; imagpart = 0; return; } // another constructor public Complex(double x, double y) { realpart = x; imagpart = y; return; } // method to find out value of real part public double getReal() { return realpart; } // method to find out value of imaginary part public double getImag() { return imagpart; } // method to add complex "z" onto this object public void increaseBy(Complex z) { realpart += z.getReal(); imagpart += z.getImag(); return; } // method to add together two complex numbers and return the result public static Complex add(Complex z, Complex w) { Complex sum = new Complex(); sum.realpart = z.getReal() + w.getReal(); sum.imagpart = z.getImag() + w.getImag(); return sum; } // method to print out in usual complex number form public void print() { System.out.print(realpart); if (imagpart < 0) { System.out.print(" - " + (-1*imagpart) + "i"); } else if (imagpart > 0) { System.out.print(" + " + imagpart + "i"); } System.out.println(""); return; } }
See how the values of realpart and imagpart may now be obtained by calling the methods getReal and getImag. Any calculations etc. that might need to be done with complex numbers should be achieved by writing static methods such as add and non-static methods like increaseBy.
Exercise 8.2
Copy the file `Complex.java' from the web site and save it in your
current Java project or directory:
http://www.pp.rhul.ac.uk/~george/PH2150/downloads/Complex.java.
Open the file and look at it -- it contains the
Complex class listed above with private class variables discussed
above with
the constructors, the increaseBy method and the print method.
Printouts of both files and sample outputs should go in your lab notebook.