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).
import pgss.*;
public class ShowWindow {
public static void main(String[] args) {
// shows a window
RobotWindow win;
win = new RobotWindow();
win.show();
}
}
RobotWindow win; win = new RobotWindow(); win.show();win is a variable: a name for a thing, a handle, a place to keep something.
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.
RobotWindow win; win = new RobotWindow(); win.show();
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.)
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();
}
}
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);
}
}