You will be working in Quad lab 7. Experiment with the BlueJ project we saw in lectures.
- Login with your zPass login and password.
- Download the ‘Turtle’ project zip file from here .
- Unzip both files in your “My Documents” folder.
- In the ‘Start’ menu, select ‘Programs’ and click on BlueJ.
- Select ‘Open Project’ from the File menu and open the ‘Turtle’ project.
- Create a new ‘MyTurtle’ object by right clicking on the MyTurtle class and selecting new MyTurtle()
- Give your turtle a name and press OK.
Simple shapes
- Right click on the red MyTurtle object and select the void drawSquare(int sideLength) method.
- Enter a side length of 100 and press OK.
- Observe what happens in the World window.
- Try drawing squares of different sizes.
- What direction does the turtle face before and after you call the method?
- Double-click on the MyTurtle class to open the source-code.
- Read the drawSquare method. Can you explain why the turtle has a different direction after the method is called?
- What could you do to fix this so the turtle is facing the same way before and after the method?
- Change the code so that it draws a triangle instead of a square.
Using the debugger
- Read the method drawPolygon and try to understand how it works.
- Run it with different parameters to see if it does what you expect.
- In the source window, click on the line number for the line “if (numberOfSides < 3)“. A little red stop sign should appear.
- Run the method again with numberOfSides set to 6 and sideLength set to 100. The Debugger window should appear.
- Press the Step button and watch how the program steps through the code.
- Run the method again with numberOfSides set to 2. Can you predict what will happen?
Spirals
- Read the method drawSpiral and try to understand how it works.
- Try drawing a spiral with length = 10 on a piece of paper following these instructions.
- Use the debugger to step through the method. Does it do what you expected?
- Change the line ‘length = length – 2‘ to ‘length = length – 4‘. How will this change the spiral? Run it and see.
- Change the line to ‘length = length + 4‘. What happens now? Why?
- Change the line ‘rotate(90)‘ to ‘rotate(88)‘ and see what happens.
- Can you make other pretty patterns by making a few simple changes?
No Comments so far ↓
Comments are closed.