Difference between revisions of "CIS 3020 Project 1"

From In The Wings
Jump to navigation Jump to search
 
 
Line 9: Line 9:
 
# square(length: double) draws a regular square, and
 
# square(length: double) draws a regular square, and
 
# a driver class, DrawHouse( ), that draws the house given in the figure below.
 
# a driver class, DrawHouse( ), that draws the house given in the figure below.
 +
==GeometricTurtle.java==
 +
<pre>
 +
public class GeometricTurtle extends Turtle {
 +
  // Simple method that draws a line of length "length", then turns
 +
  // to the left "angle" degrees. Note that we put the turtle's
 +
  // tail down at the beginning and pick it up at the end. Since
 +
  // we do it here, we don't have to worry about doing it in any
 +
  // of the methods following this one that use this particular method.
 +
  public void drawSide(double length, double angle) {
 +
    this.tailDown();
 +
    this.move(length);
 +
    this.turnLeft(angle);
 +
    this.tailUp();
 +
  } // end drawSide
 +
 +
  // Method to draw a rectangle. We will be able to use this method
 +
  // again in a method after this for drawing a square, seeing as
 +
  // a square is just a rectangle with equal sides.
 +
  //
 +
  // Input is the lenth of the two differing sides of the rectangle.
 +
  public void drawRectangle(double length, double breadth) {
 +
    this.drawSide(length,90); // Rectangle angles are 90 degrees (360/4)
 +
    this.drawSide(breadth,90);
 +
    this.drawSide(length,90);
 +
    this.drawSide(breadth,90);
 +
  } // End drawRectangle
 +
 +
  // Method to draw a pentagon. Input is just the length of the sides
 +
  // of the pentagon. If we knew about loops this code could be even
 +
  // cleaner, of course.
 +
  public void drawPentagon(double length) {
 +
    this.drawSide(length,72); // Pentagon angles are 72 degrees (360/5)
 +
    this.drawSide(length,72);
 +
    this.drawSide(length,72);
 +
    this.drawSide(length,72);
 +
    this.drawSide(length,72);
 +
  } // End drawPentagon
 +
 +
  // Method to draw a hexagon. Input is the length of the sides of the
 +
  // hexagon.
 +
  public void drawHexagon(double length) {
 +
    this.drawSide(length,60); // Hexagon angles are 60 degrees (360/6)
 +
    this.drawSide(length,60);
 +
    this.drawSide(length,60);
 +
    this.drawSide(length,60);
 +
    this.drawSide(length,60);
 +
    this.drawSide(length,60);
 +
  } // End drawHexagon
 +
 +
  // Method to draw an octagon. Input is the length of the sides of the
 +
  // octagon.
 +
  public void drawOctagon(double length) {
 +
    this.drawSide(length,45); // Octagon angles are 45 degrees (360/8)
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
    this.drawSide(length,45);
 +
  } // End drawOctagon
 +
 +
  // Method to draw a square. Input is the length of the sides of the
 +
  // square. Note that we are using the drawRectangle method from this
 +
  // method since it does exactly what we need here as well.
 +
  public void drawSquare(double length) {
 +
    this.drawRectangle(length,length);
 +
  } // End drawSquare
 +
} // End class GeometricTurtle
 +
</pre>

Latest revision as of 09:46, 30 March 2007

Background

We have discussed the class Turtle and Island in class and in Lab sessions. In this homework you will focus on getting acquainted with extending a class to add more functionality. You have been given the UMLs for the two classes in your lecture notes. Your job is to augment the Turtle class by creating a new subclass called GeometricTurtle that contains at least the following methods:

  1. drawSide(length: double, angle: double) draws the side of an object of the specified length, then turns the specified angle.
    Using this drawSide method you will implement the following methods:
  2. rectangle(length: double, breadth: double) draws a rectangle.,
  3. pentagon(length: double) draws a regular pentagon,
  4. hexagon(length: double) draws a regular hexagon,
  5. octagon(length: double) draws a regular octagon,
  6. square(length: double) draws a regular square, and
  7. a driver class, DrawHouse( ), that draws the house given in the figure below.

GeometricTurtle.java

public class GeometricTurtle extends Turtle {
  // Simple method that draws a line of length "length", then turns
  // to the left "angle" degrees. Note that we put the turtle's
  // tail down at the beginning and pick it up at the end. Since
  // we do it here, we don't have to worry about doing it in any
  // of the methods following this one that use this particular method.
  public void drawSide(double length, double angle) {
    this.tailDown();
    this.move(length);
    this.turnLeft(angle);
    this.tailUp();
  } // end drawSide

  // Method to draw a rectangle. We will be able to use this method
  // again in a method after this for drawing a square, seeing as
  // a square is just a rectangle with equal sides.
  //
  // Input is the lenth of the two differing sides of the rectangle.
  public void drawRectangle(double length, double breadth) {
    this.drawSide(length,90); // Rectangle angles are 90 degrees (360/4)
    this.drawSide(breadth,90);
    this.drawSide(length,90);
    this.drawSide(breadth,90);
  } // End drawRectangle

  // Method to draw a pentagon. Input is just the length of the sides
  // of the pentagon. If we knew about loops this code could be even
  // cleaner, of course.
  public void drawPentagon(double length) {
    this.drawSide(length,72); // Pentagon angles are 72 degrees (360/5)
    this.drawSide(length,72);
    this.drawSide(length,72);
    this.drawSide(length,72);
    this.drawSide(length,72);
  } // End drawPentagon

  // Method to draw a hexagon. Input is the length of the sides of the
  // hexagon.
  public void drawHexagon(double length) {
    this.drawSide(length,60); // Hexagon angles are 60 degrees (360/6)
    this.drawSide(length,60);
    this.drawSide(length,60);
    this.drawSide(length,60);
    this.drawSide(length,60);
    this.drawSide(length,60);
  } // End drawHexagon

  // Method to draw an octagon. Input is the length of the sides of the
  // octagon.
  public void drawOctagon(double length) {
    this.drawSide(length,45); // Octagon angles are 45 degrees (360/8)
    this.drawSide(length,45);
    this.drawSide(length,45);
    this.drawSide(length,45);
    this.drawSide(length,45);
    this.drawSide(length,45);
    this.drawSide(length,45);
    this.drawSide(length,45);
  } // End drawOctagon

  // Method to draw a square. Input is the length of the sides of the
  // square. Note that we are using the drawRectangle method from this
  // method since it does exactly what we need here as well.
  public void drawSquare(double length) {
    this.drawRectangle(length,length);
  } // End drawSquare
} // End class GeometricTurtle