teaching machines

CS 145 Lab 3 – Methods

September 22, 2016 by . Filed under cs145, fall 2016, labs.

Welcome to lab 3!

If you have checkpoints from the last to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

You must work with a partner that you have not worked with before.

Our goal today is to learn more about methods, which let us extract reusable sequences of code into a separate block of code. Methods have some very nice properties:

Checkpoint 1

Person A types.

Play Lightbot. Complete the first two worlds: Basics and Procedures.

Checkpoint 2

Person B types. Open your Eclipse workspace and create a package named lab03.

The Java library includes a pretty fun class named Robot. With Robot, you can hijack the mouse pointer, grab screenshots, and programmatically issue keypresses. Essentially you can make your program act as a fake user, generating user input—even in other programs.

In this checkpoint, you’ll use Robot to control a drawing program:

  1. Open up a paint program or find one online. Select a drawing tool like the paintbrush or pencil.
  2. Tile your windows so that both Eclipse and the paint program are visible, with the paint program on the left. Two-monitor extended desktops make this pretty easy!
  3. Create in Eclipse a class named Square with a main method.
  4. In main, construct an instance of the Robot class. You’ll see some red in Eclipse when you try to do so. Hover over the red and you’ll learn that the code can throw something called an exception. Click the Add throws declaration quick fix.
  5. Write a method named drawDot. Accept as parameters a Robot and two ints, one for an x pixel coordinate and one for y. Use the methods of Robot to simulate a mouse click (which is a press followed by a release) at the given location. Check out the documentation to see what methods are available. Include a slight delay after pressing and releasing so that slow humans like us can see what the Robot is doing as your program executes.
  6. Test your drawDot method by calling it from main. Pass the Robot you made in step 4 and the location of a pixel somewhere in the drawing canvas of your paint program. (You’ll probably have to guess and refine.) Most window managers say the origin of the screen is in the upper-left corner. You should see the cursor move and a dot appear on the canvas.
  7. Write a method named drawLineWithFiveDots. Accept five parameters: a Robot, an x location, a y location, an int named deltaX, and an int named deltaY. It draws a first dot at (x, y), a second dot at (x + deltaX, y + deltaY), a third dot at (x + 2 * deltaX, y + 2 * deltaY), and so on. But don’t do any mouse clicking here! Call your drawDot method instead.
  8. Test your drawLineWithFiveDots method by calling it from your main method. Have it draw a horizontal line. (What is deltaY in a horizontal line?) You should see five splotches of color in the paint program.
  9. Write a method named drawSquare. Accept a Robot as the sole parameter. Have it draw a square using four calls to drawLineWithFiveDots. Call it from main. The end result should look something like this, though your brush style and color may differ: