Experiments in Java


Notes on Session J3: Building Your Own Classes

Experiment J3.2, Step 2. This is an instance of the power of information hiding. Since other classes don't know what you've named the field, changes to the name of the field don't affect the operation of your program.

Experiment J3.3, Step 3. Observe that there are two ways in which you might print out a point,

    out.println(pt);

or

    pt.print(out);

The first reads (informally), ``Ask the output object to print point pt''. The second reads (informally), ``Ask pt to print itself, using output object out as a helper''.

Which is more appropriate? The second is clearly better. Why? Because points know about output objects (you've specifically written the method so that it understands them), while output objects don't necessarily know about points.

You can observe the difference in the output. The first technique will print out something odd, such as MyPoint@12345, indicating that it's printing all that it knows (the class and some other information). The second should print something like (0,0).

Experiment J3.7, Step 3. Although there is not an average(int,int) method, Java knows how to convert ints to doubles and does so.

Experiment J3.7, Step 5. We get two different results because the inputs are treated as different types in the two different computations. In the first case case both inputs are converted to double before computing the average, so the average will be a double (including a fractional part).. In the second case, the average is computed by adding two ints and then dividing by two. In this case, integer division is done, so there is no remainder.

Experiment J3.7, Step 7. The results are different because in the earlier step, the call to average(firstInt,secondInt) used average(double,double) and returned a double. This time, the call uses average(int,int) and returns an int.

Experiment J3.7, Step 9. Surprisingly (or not so surprisingly), Java can no longer tell which version of average you intend to use when you call average(firstInt,secondInt). Why? Because there are two versions of average that seem to match equivalently well.

Experiment J3.9, Step 1. This experiment uses a base class, an applet for testing, and a corresponding HTML file.

BorderedSquare.java is a class which will be used to create drawable objects. Since they're called ``bordered squares'', we can assume that they will be colored squares that have a corresponding border. Right now, the class is mostly unimplemented (although it will draw something).

BorderedSquareTester.java is an applet which will be used to test the new BorderedSquare class as we develop it. Given past experience, it is likely that we will modify and extend the one method contained within this class.

bsquaretester.html is the HTML file used to load the testing applet.

Experiment J3.9, Step 2. We call the square's paint method. In this respect, BorderedSquares are a lot like applets. To get them to draw themselves, you need to call their paint method and give them a paintbrush.


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 15:25:24 1999.

This page generated on Tue Oct 26 15:36:30 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu