Skip to main content
ICT
Lesson A4 - Object Behavior
 
Main   Previous
 

LAB ASSIGNMENT A4.2 page 11 of 11

Rectangle

Background:

  1. It is always important to have a well-designed class before writing down any code. Having a class laid out on paper before writing the code allows programmers to see any design flaws before they have coded those flaws into their classes. Determining which classes are needed, what data those classes hold, and how those classes behave are the main objectives of OOP.

  2. The specifications of a class that models a rectangular shape would be:

Variables

private double myX;      // the x coordinate of the rectangle
private double myY;      // the y coordinate of the rectangle
private double myWidth;  // the width of the rectangle
private double myHeight; // the height of the rectangle

// saves the direction the pen is pointing
// (0 = horizontal, pointing right)
private double myDirection;

// Creates a 500 x 500 SketchPad with a DrawingTool, pen, that is used
// to display Rectangle objects. The Drawingtool is declared static
// so that multiple Rectangle objects can be drawn on the Sketchpad
// at the same time.
private static DrawingTool pen = new DrawingTool(new SketchPad(500, 500));

Constructors

// Creates a default instance of a Rectangle object with all dimensions
//   set to zero.
public Rectangle()
{
   // constructor code
}

// Creates a new instance of a Rectangle object with the left and right
// edges of the rectangle at x and x + width. The top and bottom edges
// are at y and y + height.
public Rectangle(double x, double y, double width, double height)
{
   // constructor code
}

// Creates a new instance of a Rectangle object that is a copy of an
// existing Rectangle object.
public Rectangle(Rectangle rect)
{
    myX = rect.getXPos();
    myY = rect.getYPos();
    myWidth = rect.getWidth();
    myHeight = rect.getHeight();
    myDirection = rect.getDirection();
}

Methods

// Returns the x coordinate of this Rectangle
public double getXPos() { }

// Returns the y coordinate of this Rectangle
public double getYPos() { }

// Returns the width of this Rectangle
public double getWidth() { }

// Returns the height of this Rectangle
public double getHeight() { }

// Returns the direction the DrawingTool is pointing
// 0 = horizontal to the right
public double getDirection() { }

// calculates and returns the perimeter of the rectangle
public double getPerimeter() { }

// Calculates and returns the area of the rectangle.
public double getArea() { }

 // Sets the x coordinate of this Rectangle
public void setXPos(double x) { }

// Sets the y coordinate of this Rectangle
public void setYPos(double y) { }

// Sets the width this Rectangle
public void setWidth(double width) { }

// Sets the height of this Rectangle
public void setHeight(double height) { }

// Sets the direction the DrawingTool is pointing
// 0 = horizontal to the right
public void setDirection(double direction) { }

// Draws String str at the specified x and y coordinates
public void drawString(String str, double x, double y)
{ 
    pen.up();
    pen.move(x, y);
    pen.setDirection(myDirection);
    pen.down();
    pen.drawString(str);
    pen.render();
}

// Draws a new instance of a Rectangle object with the left and right
// edges of the rectangle at x and x + width. The top and bottom edges
// are at y and y + height.
public void draw() { }

Assignment:

  1. Implement a Rectangle class with the following properties.

    1. A default Rectangle object is specified in the constructor with the x, y, width and height set to 0.

    2. A Rectangle object is specified in the constructor with the left and right edges of the rectangle at x and x + width. The top and bottom edges are at y and y + height.

    3. A Rectangle object is specified that is a copy of an existing Rectangle.

    4. Methods getXPos, getYPos, getWidth, and getHeight, return the x, y, height and width of the Rectangle respectively.

    5. A method, getDirection, returns the current orientation of the DrawingTool.

    6. Methods setXPos, setYPos, setWidth, and setHeight, sets the x, y, height and width of the Rectangle respectively to the value of each methods double parameter.

    7. A method setDirection, sets the current orientation of the DrawingTool.

    8. A method getPerimeter calculates and returns the perimeter of the Rectangle.

    9. A method getArea calculates and returns the area of the Rectangle.

    10. A method draw displays a new instance of a Rectangle object.

    11. A method drawString displays String at the specified x and y coordinates of the drawing area.

  2. The methods draw, drawString, and setDirection make use of existing DrawingTool methods. Refer to DrawingTool, documentation for details on the DrawingTool methods.

  3. Write a testing class with a main method that constructs a Rectangle, rectA, and calls setDirection, setWidth, and draw for each Rectangle created. It is recommended that the changes in orientation and width of each successive rectangle in the spiral be calculated using the getDirection and getWidth methods. For example, if the increment for each turn is given by turnInc and the decrease in size of rectangle is given by widthDec, then successive calls to the following:

    rectA.setDirection(rectA.getDirection() - turnInc);
    rectA.setWidth(rectA.getWidth() - widthDec);
    rectA.draw();

    would draw each "spoke" of the rectangular spiral:

    Construct another Rectangle, rectB, that is a copy of the orginal rectA. Draw the rectangle in the upper left corner of drawing area. Label the rectangle with its width, height, perimeter and area.

    The resulting image would be similar to the one shown below:

  4. Submit the project including the source code and the run output image. It is recommended that the Rectangle class and the testing class be in separate source files (Rectangle.java and RectangleTester.java).

 

Main   Previous
Contact
 © ICT 2006, All Rights Reserved.