CS111 Lab 2 Answers

Exercise 1: Drawing a CheckerBoard Using Methods

Given the prelab solution, we just need to fill in the run method:

public void run () {
  CheckerBuggle andy = new CheckerBuggle();
  andy.drawPattern(); // bottom side
  andy.setColor(Color.blue);
  andy.drawPattern(); // right side
  andy.setColor(Color.yellow);
  andy.drawPattern(); // top side
  andy.setColor(Color.black);
  andy.drawPattern(); // left side
  andy.setColor(Color.white);
}
 
 
 

Exercise 2: Drawing Shapes Using Methods

Lines of code added to the run method to draw the shapes using the 6 ShapeBuggles, plus code for the ShapeBuggle methods:

		.
		.
		.
       public void run () {
		.
		.  (code which creates/initializes 6 ShapeBuggles)
		.
fred.drawSquare();
doug.drawRectangle();
barb.drawSquare();
andy.drawRectangle();
earl.drawSquare();
carl.drawTriangle();
		
	}
}
 
class ShapeBuggle extends Buggle {
	
	//draws a square in a 4x4 grid, starting position/heading of buggle will be the lower lefthand  corner of the 4x4 grid, facing EAST
	//ending position as shown
	
	public void drawSquare() {
this.drawRow(4);
this.nextRow();
this.drawRow(4);
this.nextRow();
this.drawRow(4);
this.nextRow();
this.drawRow(4);  	  
	}
 
     //draws a rectangle in a 6x2 grid, starting  position/heading of buggle will be the lower lefthand  corner of the 6x2 grid, facing EAST
     //ending position as shown
	
	public void drawRectangle() {
this.drawRow(6);
this.nextRow();
this.drawRow(6);  
	} 
 
     //draws a triangle in a 5x3 grid, starting  position/heading of buggle will be the lower lefthand  corner of the 5x3 grid, facing EAST
     //ending position as shown
 
	public void drawTriangle() {
this.drawRow(5);
this.nextRow();
this.skipSpace();
this.drawRow(3);
this.nextRow();
this.skipSpace();
this.drawRow(1); 
	}
		
	//draws a row of length n, leaves the buggle in the original position/heading when done	
		
	public void drawRow(int n) {
this.forward(n);
this.brushUp();
this.backward(n);
this.brushDown();
	}
	
	//moves buggle up one space, starting and ending heading facing EAST
	
	public void nextRow() {
this.left();
this.forward();
this.right();  
      }
  	  
  	//moves forward one space without painting 
public void skipSpace() {
this.brushUp();
this.forward();
this.brushDown();
      }
}
 
	
 
 

Exercise 3: TetrisWorld

SuperTetrisBuggle.java