Lab 3

advertisement
LAB 3
Introduction to Computer Science
CSI201
Fall 2011
SUMMARY
In this lab you will start using the Turtle and World classes to learn about objects and methods. Before you can use these classes however, you will do a one-time setup on your ITSUnix account.
PRE-LAB
You should have done the following things before lab:
1.
Complete all previous labs.
2.
Read chapter 3 in the book.
IN-LAB
ADD THE BOOK CLASSES TO YOUR ACCOUNT
At the lab computer, click on the icon (small picture) in the upper left of the screen for “SSH To
ITSUnix”. The shell window will appear, log in with your ITSUnix account information.
Next, type the following command then hit <Enter> to copy the bookClasses needed for this course to
your account:
source ~acsi201/pub/lab3setup
ADD THE BOOK CLASSES TO YOUR CLASSPATH IN DRJAVA
Start DrJava with the following command:
java -jar ~acsi201/bin/drjava.jar
After a few moments the DrJava splash screen will appear, followed by the DrJava window itself.
From DrJava's Edit menu, select Preferences. When the Preferences window opens, click the Add
button. In the list of files and directories shown in the new window should be one with your
bookClasses directory. Click on that, and then on Select. Then click on OK at the bottom of the
Preferences window.
DrJava will now look in the bookClasses directory for Classes it needs.
This is a one-time setup, there is no need to repeat this process now that you’ve set up your
account.
TRY OUT THE TURTLE
To get familiar with the Turtle class and it’s methods lets try a few simple commands. Enter the following in the interactions pane:
World world1 = new World();
Turtle turtle1 = new Turtle(world1);
turtle1.turnRight();
turtle1.forward(100);
turtle1.turn(45);
The turn(int degrees) method allows you to turn the turtle by any degree. Try it out. Be sure
these commands work properly before proceeding. This will ensure that you are correctly using the
Turtle and World classes.
Open Turtle.java and create a new method
In DrJava click the Open button. In the window that comes up double click on bookClasses. Find
the file Turtle.java and double click. You should now see Turtle.java in your definitions pane. Take
some time to look over the file. Don’t be surprised that you don’t see all the methods that you expect
here. Turtle.java extends SimpleTurtle.java, which means that some of the basic methods like forward and turn are stored there. Find the space at the end of the file between the comment line that
says “methods” and the final closing curly bracket. This is where you will put your new method.
For this lab you will create a method called drawRhombus(). As you may have guessed, this method will use the turtle to draw a rhombus. Recall that a rhombus is a quadrilateral with four equal
sides. Opposite angles in a rhombus are equal and all four angles add up to 360˚. For your first attempt, we will set all sides to 100 and interior angles to 60˚ and 120˚.
This method shouldn’t be to difficult to write. You will use the methods already defined in the Turtle
class to do the dirty work. Refer to section 3.5 in the book for help. Remember, you can invoke an
object method of the current class by using the keyword this, as in this.methodName().
Note this method will have no parameters and no return value, so the method declaration will be:
public void drawRhombus()
{
...
}
Be sure that when your method is done your turtle is in the same spot and facing the same direction
as before it began.
Once your method is written, compile Turtle.java. If you get any errors try to fix them or ask your TA
for assistance. Then you can test your work by creating a world and turtle and invoking your method
as you did before:
World world1 = new World();
Turtle turtle1 = new Turtle(world1);
turtle1.drawRhombus();
PARAMETERS
Page 2
Now lets make this method a little more interesting. Instead of fixing the length of the sides and the
angles you will modify your method to accept them as parameters. Your new method declaration will
look like this:
public void drawRhombus(int side, int angle)
{
...
}
Note that only one angle is passed. (Only one angle is needed, right...? Why?) Modify your method
to work for any side length and angle. Once you are done, re-compile and try it out by creating a
world and turtle (just like before) and then testing with some different side and angle values, e.g.
turtle1.drawRhombus(50,75);
turtle1.drawRhombus(150,30);
etc...
GET CREDIT
Don’t forget to show your TA what you’ve done before you leave so you get credit for attending and
participating in the lab.
POST-LAB
Get some more practice with the Turtle class and methods by trying this at home. Try some other
shapes. How about a square centered at the current location? Hint: Use the penUp() and penDown() methods.
Page 3
Download