[G1 Android Development] mohsinkhan47:
HANDOUTS AND

ASSIGNMENTS:	Handout A1.1, DrawingTool Class Specifications

Lab Assignment A1.1, DrawHouse

Worksheet A1.1, Object-Oriented Programming

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 = new 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);

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! (Write the code on the back of this sheet)

LAB ASSIGNMENT A1.1

DrawHouse

Background:

You will be provided with a file named gpdraw.jar, which contains the code needed to implement the graphics tools to draw objects. The specifications of the drawing tools are provided in Handout A1.1 – DrawingTool. Simply place the gpdraw.jar file in the appropriate folder location so the Java compiler can find it. Then add this line of code at the top of your program and the drawing tools are available for use.

import gpdraw.*;

Assignment:

Write a program that creates a drawing area of appropriate size (try 500 x 500) and draws a house similar to the one shown below and with these specifications:

1. The house should fill up most of the drawing area, i.e. draw it big.

2. The house should be centered horizontally on the screen.

3. The house must have a sloped roof. It can be of any slope or configuration. But you cannot have a flat roof on the house.

4. Adding a door (centered) and windows is optional.

Instructions:

1. Include your name as a documentation statement and also a brief description of the program.

2. You will need to turn in (either on paper or electronically) a copy of your code and a picture of the house that resulted.

HANDOUT A1.1

DrawingTool Class Specifications

These classes are not part of Java but are available through the library named gpdraw. You must have the file gpdraw.jar in the appropriate directory where Java can access it. To have these classes available in your program, use this command:

import gpdraw.*;

Other features of gpdraw will be covered in later lessons.

DrawingTool

protected double xPos

protected double yPos

protected double direction;

protected int width;

protected boolean isDown;

protected Color color;

...

<<constructors>>

DrawingTool()

DrawingTool(SketchPad)

...

<<accessors>>

public Color getColor()

public double getDirection()

public int getWidth()

public String toString()

...

<<modifiers>>

public void down()

public void drawString(String)

public void drawCircle(double)

public void forward(double)

public void home()

public void move(double, double)

public void setColor(Color)

public void setDirection(double)

public void setWidth(int)

public void turn (double)

public void turnLeft(double)

public void turnRight(double)

public void up()

...

Invariant

A DrawingTool object

•	Appears in a SketchPad Window (this window is 250 pixels wide and 250 pixels high initially, but can be constructed with different dimensions.)

•	The origin (0, 0) is at the center of the drawing window.

•	Is directed either up, down, left, or right.

•	Is either in drawing mode or in moving mode.

Constructor Methods

public DrawingTool()

postcondition

•	A new DrawingTool is created and placed in the center (0, 0) of a SketchPad window that is 250 pixels wide and 250 pixels high.

•	This object is set to drawing mode.

•	The direction for this object is up (90º).

•	The DrawingTool color is set to blue.

•	The DrawingTool width is 1.

public DrawingTool(SketchPad win)

postcondition

•	A new DrawingTool is created and placed in the center (0, 0) of the SketchPad window win.

•	This object is set to drawing mode.

•	The direction for this object is up (90º).

•	The DrawingTool color is set to blue.

•	The DrawingTool width is 1.

Accessor Methods

public String toString();

postcondition

result = color

public Color getColor();

postcondition

result = color

public double getDirection();

postcondition

result = direction

public int getWidth();

postcondition

result = width

Modifier Methods

public void down();

postcondition

•	This object is set to drawing mode.

public drawString(String text);

postcondition

•	The string text is drawn at the current location using the current color.

public drawCircle (double r);

postcondition

•	If the object is in drawing mode, a circle of radius r is drawn around the current location using the current width and color.

public forward(double distance);

postcondition

•	This DrawingTool object is moved in the current direction by distance pixels from the old (previous) location.

•	If this object is in drawing mode, a line segment is drawn across the distance path just traversed.

•	A 0.5 second delay occurs following this method’s execution.

public home();

postcondition

•	The location of the DrawingTool object is set to the center of the SketchPad window.

•	The drawing direction of the object is up.

public move(double x, double y);

postcondition

•	This DrawingTool object is moved from the current position to the position specified by the coordinates x and y.

•	If this object is in drawing mode, a line segment is drawn from the old (previous) position to the absolute position specified by x and y.

public setColor(Color c);

precondition

•	c is a valid Color

•	requires the following at the beginning of your program to set the color to red

import java.awt.Color;

.

.

setColor(Color.red);

postcondition

•	The color of the DrawingTool object is set to c.

public setDirection(double d);

postcondition

•	Sets the direction to d degrees. The orientation is d degrees counterclockwise from the positive x-axis

public setWidth(int w);

precondition

•	w is >= 1

postcondition

•	The width of the DrawingTool object is set to w pixels.

public turn(double d);

postcondition

•	Changes the current direction counterclockwise by d degrees from the current direction.

public turnLeft(double degrees);

postcondition

•	Changes the current direction counterclockwise by d degrees from the current direction.

public turnRight(double degrees);

postcondition

•	Changes the current direction clockwise by d degrees from the current direction.

public up();

postcondition

•	This object is set to moving mode.