Starting with Java

For the programming portion, we'll basically be following the textbook: Today I hope to cover Sections 3.2 and 3.3 (pages 12 to 14) and Chapter 4 (pages 17 to 25).

About Java

The Programming Process

They're all the result of ``bugs''.

A Simple Java Program

	import pgss.*;

	public class ShowWindow {
	    public static void main(String[] args) {
		// shows a window
		RobotWindow win;
		win = new RobotWindow();
		win.show();
	    }
	}

Programming Style

People read programs too!

Variables, Classes, and Objects

In our simple example program:
		RobotWindow win;
		win = new RobotWindow();
		win.show();
win is a variable: a name for a thing, a handle, a place to keep something.
Variables in Java have a type, indicating the kinds of things they can refer to (or the kinds of things that can be kept in them).
A variable declaration defines a new variable, and gives its type (the first line here).
Here, win is a variable of type RobotWindow.
RobotWindow, in turn, is a class, a type whose members will be objects, abstract entities with internal structure.

The second line here is an assignment statement.
It makes the name win refer to something (or puts something in the place called win). The right side of the second line is the constructor method for the RobotWindow class. It creates a new object of that class.

Methods

Methods define what actions an object can take. To make something happen, you send a message to the object, telling it to execute one of its instance methods.
In addition to its contructor method, RobotWindow has a method called show, which makes it bring itself up on the screen. The third line here sends that message to the object named/pointed to/stored in win:
		RobotWindow win;
		win = new RobotWindow();
		win.show();

Class documentation

Documentation is important, and it is notoriously difficult to get people to document their programs. So Java has built-in documentation facilities.

RobotWindow()
(Constructor method) Constructs an object to represent a window on the screen, 200 pixels wide and 200 pixels tall.
void show()
Displays this window on the screen.

Robot(RobotWindow world, double x, double y)
(Constructor method) Constructs a robot object, located in the world window at coordinates (x, y) facing east.
void move(double dist)
Moves this robot dist pixels in its current direction, tracing a line along the path.
void turn(double angle)
Turns this robot angle degrees counterclockwise.
void switchOff()
Removes this robot from its window.

(See also Appendix A.)

Parameters

The methods of the Robot class have parameters that further specify the requested action. The types of parameters are shown in the class documentation.

Suppose we want a Robot object named robin to move forward 100 pixels:

		robin.move(100);

Of course, first we would have to create the Robot, using the constructor (with its required parameters):

		robin = new Robot(win, 50, 150);

Now let's actually do something; how about draw a triangle in a RobotWindow? (The first seven lines are the same as before, except for the program's name.)

	import pgss.*;

	public class DrawTriangle {
	    public static void main(String[] args) {
		RobotWindow win;
		win = new RobotWindow();
		win.show();

		Robot rob;
		rob = new Robot(win, 50, 150);
		rob.move(100);
		rob.turn(120);
		rob.move(100);
		rob.turn(120);
		rob.move(100);
		rob.switchOff();
	    }
	}

Names vs. Objects

Let's see how confused we can get; suppose we have different variables that refer to the same object? (The object is actually distinct from the variable that currently refers to it!)
	import pgss.*;

	public class Race {
	    public static void main(String[] args) {
		RobotWindow win;
		win = new RobotWindow();
		win.show();

		Robot upper;
		Robot cur;
		cur = new Robot(win, 10, 60);
		upper = cur;
		cur.move(90);
		cur = new Robot(win, 10, 140);
		cur.move(90);
		cur = upper;
		cur.move(90);
	    }
	}