teaching machines

CS 148: Lab 4 – Methods

Welcome to lab 4!

If you have checkpoints from the last lab 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.

Our goal today is to learn more about methods, which let us extract reusable sequences of code into a separate block of code.

Checkpoint 1

Person A types. Open your Eclipse workspace and create a package named lab04.

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. Follow these steps to get it up and running.

  • Create in Eclipse a class named Square with a main method. Do not name your class Robot, otherwise it will hide the builtin Robot class.
  • Open up a paint program or find one online. Select a drawing tool like the paintbrush or pencil. Tile your windows so that both Eclipse and the paint program are visible, with the paint program on the left—nearer the origin of your display. Two-monitor extended desktops make this pretty easy!
  • In the main method of your Square class, construct an instance of the Robot class. We have been constructing instances for a while now—albeit for different types. They have looked something like this:
    Scanner in = new Scanner(System.in);
    Random generator = new Random();
    Here the type is Robot, and you’ll want to pick a more appropriate name. You’ll see some red in Eclipse when you try to make an instance. Hover over the red and you’ll learn that the code can throw something called an exception. Click the Add throws declaration quick fix.
  • 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.
  • 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 should see the cursor move and a dot appear on the canvas. Do not continue until you see this single dot in your paint program.
  • 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. Use only five statements to accomplish this—five calls to drawDot.
  • 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.
  • Write a method named drawSquare. Accept a Robot as the sole parameter. Have it draw a square using just four calls to drawLineWithFiveDots, and nothing else. Call it from main. The end result should look something like this, though your brush style and color may differ:

Checkpoint 2

Person B types.

You will design a blackbox and share it with another group. On a fun scale of 1 to 10, this will rank about a 7!

  • Design a method that does something interesting, but not impossible to deduce. Code it up in a class named Blackbox.
  • In a main method, prompt the user for the data that will serve as the actual parameters when you call the method. Grab the user input with a Scanner. Then call the method and print its result.
  • Now package up the code so that you can give it to another group. We’ll deliver it as a runnable JAR so that they can’t see the source code. (JAR is short for Java ARchive, the same type of file as the SpecCheckers.) Make a JAR by right-clicking on Blackbox.java in the Package Explorer. Click Export / Java / Runnable JAR file. Click Next. Select the Blackbox launch configuration and an export destination that you can locate easily (like in your Desktop folder). Click Finish. You should see your JAR file appear in the file system.
  • Swap your JAR with another group’s, via USB drive or email or some other sharing mechanism.
  • Drag their JAR into your lab04 package to import it. Right-click on it and select Build Path / Add to Build Path. Run it and try to reverse engineer what the other group’s method does.
  • Implement your own version of the other group’s method. When you have adequately tested your implementation and found it to behave identically to theirs, give each other high fives.


Leave a Reply

Your email address will not be published. Required fields are marked *