Experiments in Java


Notes on Session J2: Objects and Methods

Experiment J2.1, Step 1. You won't be able to execute Point. Why not? Because it doesn't include a main method. Points can be used to manipulate points on the plane, but they won't work by themselves; they need to be coordinated by a separate main routine.

Experiment J2.1, Step 2. Why are the numbers so strange? Because Java, like many programming languages, tends to approximate numbers. However, the internal working of the computer lead to different approximations than we normally get. So, when we subtract 10.2 from 2.7, we should get -7.5. However, the approximation leads to the strange number you see.

Experiment J2.3, Step 4. Experiment J2.4, Step 3. Experiment J2.4, Step 4. All of these cases show that the name used within a method is independent of the name used outside the method. In effect, Java renames any arguments used in a method call so that they correspond to the parameter names in the method. As long as you are consistent within the method, you should be fine.

Experiment J2.6, Step 5. If you call a nonstatic method (a normal method) and don't provide an accompanying object, the Java compiler will issue an error message. In this case, print is not a static method, but it's being used as if it were a static method.

Experiment J2.6, Step 6. The parameters are being passed to the method in the wrong order. The read method is designed to take a SimpleOutput object as its first parameter and a SimpleInput object as its second parameter.

Experiment J2.8, Step 1. It's likely we'll be creating new Font objects and new Color objects, so we import those two classes.

Experiment J2.8, Step 3. We have yet to make explicit use of a Color, so there is no immediate harm in removing the line. However, since we expect to use colors in the near future, it behooves us to reinsert the line once we are done testing.

Experiment J2.8, Step 4. Because we use the Font class and do not import that class, Java is likely to complain that the class has been used without a corresponding import statement.

Experiment J2.8, Step 6. In effect, the one line

    paintBrush.setFont(new Font("Serif", Font.ITALIC, 18));

replaces the two lines

    Font myFont = new Font("Serif", Font.ITALIC, 18));
    paintBrush.setFont(myFont);

That is, it creates a new Font object and then immediately passes it to setFont, rather than storing it in a variable and then passing it with that variable.

If you are only creating a new Font to use with setFont, you can use this technique to avoid the intermediate step and the otherwise-unused variable. You'll find that many experienced Java programmers use similar shortcuts.

Experiment J2.8, Step 9. Since the paint color is only changed after the text has been drawn, there is no visible effect.


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

Source text last modified Mon Oct 25 15:10:13 1999.

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

Contact our webmaster at rebelsky@math.grin.edu