There are three tasks for this week’s lab:
- Activate your CSE account.
- Write your first program!
- Play with BlueJ
Activate your CSE account
The first thing we will do in this week’s lab is activate your CSE account, if you haven’t done so already.
- Login as ‘newuser’
- Follow the prompts to activate your account.
- Write your new username and password somewhere so you don’t lose it.
- Log out.
- Log back in with your new CSE login and password. Make sure you select ‘Windows’ as your desktop.
Write your first program.
This is an exercise in breaking down a task into a sequence of simple instructions. Work in pairs. Your tutor will give each of you a small, incomplete pack of cards.
- Shuffle the deck.
- Go through the deck to find the smallest card.
- On a sheet of paper, write a set of instructions so that someone else can repeat the same procedure. Assume that they are very stupid and need to have ever last detail spelled out.
- Test your instructions on different size decks:
- What if there is only 2 cards in the deck? Or only one? Or none?
- What if all the cards in the deck were the same?
- Swap programs with another pair and check their code. Was it the same as yours?
- Consider: What are the objects in your program? What are the classes? What fields do they have? What methods do they provide?
- Now try writing instructions to sort the entire deck into order.
Experiment with BlueJ
Experiment with the BlueJ projects we saw in lectures.
- Login with your CSE login and password set up earlier
- Download the Turtle.zip
- Unzip this file to create the BlueJ project.
- Start 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?
No Comments so far ↓
There are no comments yet...Kick things off by filling out the form below.
You must log in to post a comment.