Anda di halaman 1dari 6

Worksheet A1.

1 1

WORKSHEET A1.1

Object-Oriented Programming

The following object declarations and initializations will be used for all questions. This code
will create a DrawingTool object called ​marker​ and a SketchPad object called ​poster​. The ​poster
will have dimensions of 600 x 600, and the ​marker​ will be constructed and used on the ​poster.
Each drawing will begin at the center of the poster at the point (0,0).

DrawingTool marker;
SketchPad poster;

poster = ​new​ SketchPad(600,600);


marker = n​ ew​ DrawingTool(poster);

1. Draw the figure generated by the following code segment:

marker.forward(120);
marker.turnRight(45);
marker.forward(80);
marker.turnLeft(90);
marker.forward(80);
marker.turnLeft(90);
marker.forward(80);
marker.turnLeft(90);
marker.forward(80);
Worksheet A1.1 2

2. Draw lines A and B as described by the following – note the move() method is introduced
here for line B. It allows you to draw lines with fewer commands:

marker.up();
marker.turnRight(90);
marker.forward(100);
marker.down();
marker.drawString(" A");
marker.move(-100,0);
marker.up();
marker.move(-175,100);
marker.down();
marker.move(175,100);
marker.drawString(" B");

3. Write code that will draw the following figure. The lower left corner is at the point (0,0),
and the length of each side of the square is 200 units. The upper left part of the mouth
begins at (40,60), and the upper part of the nose begins at (100,100). The eyes each have
a radius of 10 units and are centered at (60,150) and (140,150). The other key points are
left for you to decide. Have fun!

import gpdraw.*;
public class Lesson1HappyFace {
public static void main(String[] args) {
DrawingTool marker;
SketchPad poster;
poster = new SketchPad(600,600);
marker = new DrawingTool(poster);
marker.move(0,0);
marker.forward(200);
marker.turnRight(90);
marker.forward(200);
marker.turnRight(90);
marker.forward(200);
marker.turnRight(90);
marker.forward(200);
marker.up();
marker.move(40,60);
Worksheet A1.1 3

marker.turnRight(146);
marker.down();
marker.forward(30);
marker.turnLeft(90);
marker.forward(120);
marker.turnLeft(90);
marker.forward(30);
marker.up();
marker.move(100,100);
marker.down();
marker.turnLeft(90);
marker.forward(30);
marker.turnLeft(122);
marker.forward(30);
marker.up();
marker.move(60,150);
marker.down();
marker.drawCircle(10);
marker.up();
marker.move(140,150);
marker.down();
marker.drawCircle(10);
}

}
Worksheet A1.1 4

4. Enhance # 2 by writing code to connect the left endpoint of B to the right endpoint of A.
Write additional code to connect the right endpoint of B to the left endpoint of A.
import gpdraw.*;
public class Lesson1Enhance2v1 {
public static void main(String[] args) {
DrawingTool marker;
SketchPad poster;
poster = new SketchPad(600,600);
marker = new DrawingTool(poster);
marker.up();
Worksheet A1.1 5

marker.turnRight(90);
marker.forward(100);
marker.down();
marker.drawString(" A");
marker.move(-100,0);
marker.up();
marker.move(-175,100);
marker.down();
marker.move(175,100);
marker.drawString(" B");
marker.up();
marker.move(-100,0);
marker.down();
marker.move(175,100);
marker.up();
marker.move(100,0);
marker.down();
marker.move(-175,100);
}
}

5. Enhance # 2 again by writing code to construct the perpendicular bisector to segments A


and B (25 units above B and 25 units below A).
import gpdraw.*;
public class Lesson1Enhance2v1 {
public static void main(String[] args) {
DrawingTool marker;
SketchPad poster;
poster = new SketchPad(600,600);
marker = new DrawingTool(poster);
marker.up();
marker.turnRight(90);
marker.forward(100);
marker.down();
marker.drawString(" A");
marker.move(-100,0);
marker.up();
marker.move(-175,100);
marker.down();
marker.move(175,100);
marker.drawString(" B");
marker.up();
marker.move(0,125);
Worksheet A1.1 6

marker.down();
marker.move(0,-25);
}
}