CS111 Lab 3 Answers

Exercise 1: Another Java Execution Model Diagram

Available in hardcopy outside Professor Turbak's office.

Exercise 2: Bagel Rugs

public void run () {
                
                // create a RugBuggle object weaver
                RugBuggle weaver = new RugBuggle();
                weaver.makeRug();    
        }
}
        
class RugBuggle extends Buggle {
 
        public void makeRug() {
           weaver.outerRing(Color.red);
           weaver.innerRing(Color.blue);
           weaver.centerRing(Color.yellow);    
        }
      
        public void outerRing(Color c) {
 
                setColor(c);
                outerEdge();
                outerEdge();
                outerEdge();
                outerEdge();
                moveIn();
 
        }
        
        public void innerRing(Color c) {
 
                setColor(c);
                innerEdge();
                innerEdge();
                innerEdge();
                innerEdge();
                moveIn();
        }
        
        public void centerRing(Color c) {
 
                setColor(c);
                centerEdge();
                centerEdge();
                centerEdge();
                centerEdge();
                moveIn();
        }
        
        public void outerEdge() {
                pattern1();
                pattern2();
                pattern2();
                pattern2();
                pattern2();
                turnCorner();
 
        }
        
        public void innerEdge() {
                pattern3();
                pattern3();
                pattern3();
                turnCorner();
        }
 
        public void centerEdge() {
                pattern4();
                turnCorner();
        }
        
        public void pattern1() {
                brushDown();
                left();
                dropBagel();
                forward();
                dropBagel();
                forward();
                dropBagel();
                right();
                right();
                forward();
                brushUp();
                forward();
                left();
                forward();
                brushDown();
                dropBagel();
                forward();
                dropBagel();
                forward();
                brushUp();
        }
 
        public void pattern2() {
                brushDown();
                dropBagel();
                forward();
                dropBagel();
                forward();
                dropBagel();
                forward();
                brushUp();
        }
 
        public void pattern3() {
                brushDown();
                dropBagel();
                forward();
                brushUp();
                forward();
                left();
                forward();
                brushDown();
                dropBagel();
                forward();
                brushUp();
                left();
                forward(2);
                brushDown();
                dropBagel();
                left();
                forward();
                brushUp();
                forward(1);
                left();
                forward(3);
        }
 
        public void pattern4() {
                brushDown();
                dropBagel();
                forward();
                brushUp();
                forward();
                brushDown();
                dropBagel();
                left();
                forward();
                brushUp();
                left();
                forward();
                brushDown();
                dropBagel();
                forward();
                brushUp();
                right();
                forward();
                brushDown();
                dropBagel();
                right();
                forward();
                brushUp();
                forward();
                brushDown();
                dropBagel();
                right();
                forward();
                brushUp();
                forward();
                left();
                forward();
        }       
 
        public void moveIn() {
                        
                forward(3);
                left();
                forward(3);
                right();
        }
        
        public void turnCorner() {
                
                forward(2);
                left();
        }               
                
}
        

Exercise 3: Checkerboards

CheckerBuggle.java