CS111 Lab 4 -- Practice Exam 1 Answers

Problem 1a

Problem 1b

  public void horseShoe (Point p, Direction d, Color c, int size) {
    this.setPosition(p);
    this.setColor(Color.red);
    this.setHeading(d);
    this.forward();
    this.setColor(c);
    this.forward(size-2);
    this.right();
    this.forward(2);
    this.right();
    this.forward(size-1);
    this.setColor(Color.red);
    this.forward();
  }

Problem 1c

  public void run() {
    horseShoeBuggle a = new horseShoeBuggle();
    horseShoeBuggle b = new horseShoeBuggle();
    horseShoeBuggle c = new horseShoeBuggle();

    // make a horseshoe of size 4
    a.horseShoe(new Point(4,6), Direction.WEST, Color.green, 4);

    // make a horseshoe of size 5
    b.horseShoe(new Point(2,4), Direction.EAST, Color.pink, 5);

    // make a horseshoe of size 3
    c.horseShoe(new Point(4,5), Direction.NORTH, Color.orange, 3);
  }

Problem 2a

Problem 2b

We define an auxiliary method called twoTriangles which draws the following pattern which we notice is repeated in the star.

twoTriangles(Color.black,Color.red)
  public Picture twoTriangles (Color c1, Color c2) {
    return overlay(above(empty(),triangle(c1),0.75),
		   above(empty(),triangle(c2)));
  }
Then we use that method to define star.
  public Picture star (Color c1, Color c2, Color c3, Color c4,
		       Color c5, Color c6, Color c7, Color c8) {
    return fourOverlay(twoTriangles(c1,c2),
		       flipHorizontally(twoTriangles(c3,c4)),
		       clockwise90(twoTriangles(c5,c6)),
		       flipVertically(clockwise90(twoTriangles(c7,c8))));
  }

Problem 2c

  public Picture pattern1 (Color c1, Color c2) {
    Picture p1 = star(c1,c2,c1,c2,c1,c2,c1,c2);
    return fourSame(p1);
  }

  public Picture pattern2 (Color c1, Color c2, Color c3, Color c4) {
    Picture p1 = star(c1,c2,c3,c4,c1,c2,c3,c4);
    return rotations(p1);
  }

Problem 3a

Problem 3b

Coming soon