Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm exam 1 review Sp2006: Making Concentric Boxes

Questions, comments, concerns, answers, concerns about answers?
(Back to Midterm Exam 1 Review Sp2006)


This one was fun!

public class MyTurtlePicture {
  
  public static void main(String [] args) {
    
    //FileChooser.setMediaPath("D:/cs1316/MediaSources/");
    Picture canvas = new Picture(500,500);
    Turtle jenny = new Turtle(canvas);
    //Picture lilTurtle = new Picture(FileChooser.getMediaPath("Turtle.jpg"));
    
    /* Contrary to my math education, heading 0 in this case points up (I'm pretty certain)
    *  So let's start by heading east.   */
    jenny.setHeading(90);
    
    /* We'll be making a total of 12 lines */
    /* I'm taking a shortcut around making 3 loops like this:
    *  Each time through, the line is 50 px longer.
    *  So, 100+50*0, 100+50*1, 100+50*2
    *  I used (int)(i/4) to produce 0, 1, 2 (increments by a whole integer every 4 sides) */
    for(i=0;i<12;i++){
      jenny.forward(100+(50*(int)(i/4)));
      jenny.turnLeft();
      /* Yes, turnLeft does exist.  It's in simpleTurtle. */
    }

    canvas.show();
  }
  
}


I think I put in more comments than code! If someone wants to post a 3 loop solution, please do...

/**
 *Method to draw 3 squares(100pixel, 150 pixel, and 200 pixel) on a blank canvas using a turtle
**/ 

public class TurtleBoxes{
  public static void main(String[] args)
  {
    FileChooser.setMediaPath("C:/cs1316/MediaSources/");
    Picture canvas = new Picture(500, 500);
    Turtle yurtle = new Turtle(canvas);
    
    //first square made before the for loop so we don't get any errors with the loop (debugging sucks)
    yurtle.forward(100);yurtle.turn(90);
    yurtle.forward(100);yurtle.turn(90);
    yurtle.forward(100);yurtle.turn(90);
    yurtle.forward(100);yurtle.turn(90);
   
    //for loop, i < 8 because we need it to make a square with 4 sides
    for (int i=0;i<8;i++)
    {
      if (i<4)
        {yurtle.forward(150);yurtle.turn(90);}
      else
        {yurtle.forward(200);yurtle.turn(90);}
    }
    //We want to see the result, and I added in canvas.write(...) to post the picture
    canvas.show();canvas.write("C:/cs1316/TurtleBoxes.jpeg");  
  }
}


Uploaded Image: TurtleBoxes.jpeg

Michael Campbell


I'm going to assume that there are some less than signs that got deleted there...Same thing happened to me earlier, the swiki did it to me.
i<8 and i<4

But, yeah–that's good!
~Jim

hmm... I am confused because I see
"i<8" and "i<4"
in the written code....

I guess they must have fixed it... :):)

Is it possible to do this without a loop, something like:

public class 3BoxTurtle{
public static void main(String[] args){
Picture canvas = new Picture(400, 400);
Turtle turt = new Turtle(canvas);

//The first square
turt.forward(100);turt.turn(90);
turt.forward(100);turt.turn(90);
turt.forward(100);turt.turn(90);
turt.forward(100);turt.turn(90);

//The second square
turt.forward(150);turt.turn(90);
turt.forward(150);turt.turn(90);
turt.forward(150);turt.turn(90);
turt.forward(150);turt.turn(90);

//The third square
turt.forward(200);turt.turn(90);
turt.forward(200);turt.turn(90);
turt.forward(200);turt.turn(90);
turt.forward(200);turt.turn(90);

canvas.show();}

To show < and >, either (a) put <code> and </code> around the code or (b) put in &lt; and &gt;. Otherwise, your browser eats the < and >, thinking it must be part of the HTML that just got screwed up. Mark Guzdial



Link to this Page