CS116      Lab 1 - Objects and Methods

Objectives:

  1. Before starting this lab you should have read chapter 3 in your text.
     
  2. Start DrJava and open the file Turtle.java
     
  3. In Chapter 3 you should have read about creating objects by using Class objectName = new Class(parameterList);
    Create a World object and a Turtle object in that world by typing the following commands in the Interactions Pane:

    World worldObj = new World();
    Turtle turtle1 = new Turtle(worldObj);

    You should have a frame containing your world with a turtle in the middle.
     

  4. Chapter 3 discusses using objects by executing "methods".  Look at the documentation for the class Turtle located on the course website.  You will see that it "inherits" from SimpleTurtle, meaning that it has all the methods of that class plus special features of its own.  Check out the methods available from SimpleTurtle. 
    We can use a method with
    objectName.method(parameterList);
    For example,
        turtle1.forward(20);
    moves the turtle forward 20 steps.  Try out the methods that move, and turn the turtle.  Also try the methods penUp and penDown. 
     

  5. We can define our own methods.  On p57 of your text you are given a method for the Turtle class which will tell the turtle to draw a square of any size given by the parameter width.  Type in this new method into the Turtle class (including the comments which describe the method to a human reader).  Save your changes and compile.  Then try it out in the Interactions pane by creating a new turtle and telling the turtle to draw a square of size 200.  Use this same method again to draw a square of size 50.
     


  6.  

    Assignment:
    Write a Turtle method called stackedSquares which draws the following:

     

    Hint:  Your methods should invoke or use the method draswSquare three times.  (Using code that is already written in the class is always a good thing!)


     


  7.  

    Assignment:
    Write a Turtle method which draws an equilateral triangle.  The method should take a parameter which is the length of the sides. 


     


  8.  
    Assignment:
    Write a Turtle method which draws a star.  The method should take a parameter which is the length of the sides. 

                                                   

    Hint:  The interior angles at each point are 36 degrees.


     


  9.  

    Assignment:
    Write a Turtle method which draws both of your initials in block, squared off letters.  This method mostly likely will not have any parameters.


     


  10.  

    Assignment:
    Edit MyProgram.java so that instead of printing a message it now, creates a World object, creates a Turtle object, and then the turtle draws your initials.  Do this by simply USING the method that you wrote above (rather than rewriting that code). 

    Hint:  This should be really short and easy!

     

  11. Send your modified code MyProgram.java and Turtle.java to me using GoucherLearn.