COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Lab Wk 2- Getting to know BlueJ

Posted by on July 17th, 2012 · Lab

There are three tasks for this week’s lab:

  1. Activate your CSE account.
  2. Write your first program!
  3. 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.

  1. Login as ‘newuser’
  2. Follow the prompts to activate your account.
  3. Write your new username and password somewhere so you don’t lose it.
  4. Log out.
  5. 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.

  1. Shuffle the deck.
  2. Go through the deck to find the smallest card.
  3. 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.
  4. 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?
  5. Swap programs with another pair and check their code. Was it the same as yours?
  6. Consider: What are the objects in your program? What are the classes? What fields do they have? What methods do they provide?
  7. Now try writing instructions to sort the entire deck into order.

Experiment with BlueJ

Experiment with the BlueJ projects we saw in lectures.

  1. Login with your CSE login and password set up earlier
  2. Download the Turtle.zip
  3. Unzip this file to create the BlueJ project.
  4. Start BlueJ
  5. Select ‘Open Project’ from the File menu and open the ‘Turtle’ project.
  6. Create a new ‘MyTurtle’ object by right clicking on the MyTurtle class and selecting new MyTurtle()
  7. Give your turtle a name and press OK.

Simple shapes

  1. Right click on the red MyTurtle object and select the void drawSquare(int sideLength) method.
  2. Enter a side length of 100 and press OK.
  3. Observe what happens in the World window.
  4. Try drawing squares of different sizes.
  5. What direction does the turtle face before and after you call the method?
  6. Double-click on the MyTurtle class to open the source-code.
  7. Read the drawSquare method. Can you explain why the turtle has a different direction after the method is called?
  8. What could you do to fix this so the turtle is facing the same way before and after the method?
  9. Change the code so that it draws a triangle instead of a square.

Using the debugger

  1. Read the method drawPolygon and try to understand how it works.
  2. Run it with different parameters to see if it does what you expect.
  3. In the source window, click on the line number for the line “if (numberOfSides < 3) {“. A little red stop sign should appear.
  4. Run the method again with numberOfSides set to 6 and sideLength set to 100. The Debugger window should appear.
  5. Press the Step button and watch how the program steps through the code.
  6. Run the method again with numberOfSides set to 2. Can you predict what will happen?

Spirals

  1. Read the method drawSpiral and try to understand how it works.
  2. Try drawing a spiral with length = 10 on a piece of paper following these instructions.
  3. Use the debugger to step through the method. Does it do what you expected?
  4. Change the line ‘length = length – 2‘ to ‘length = length – 4‘. How will this change the spiral? Run it and see.
  5. 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.