teaching machines

CS 145 Lab 3 – Methods

September 19, 2014 by . Filed under cs145, fall 2014, labs.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.

Don’t forget to work in pairs! Where possible, please work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.


Pushing our code into methods helps us divide our software into independent units, which is good for reusing, testing, and organizing our jumble of ideas. In this lab, you’ll write your own methods.

Checkpoint 1

Person A types.

Web pages are identified by uniform resource locators (URLs), which have the form protocol://domain/pathcomponent1/pathcomponent2/pathcomponent3. For example, this article from The Onion is identified by the URL http://www.theonion.com/articles/new-remote-control-can-be-operated-by-remote,1666/.

Write a method getDomain that accepts a String url as a parameter and returns only the domain. Returning is not the same as printing. The main method may print, but getDomain may not.

There are several ways to solve this. I suggest you use a Scanner, which can break up text on a delimiter of your choosing. What delimits the domain name?

We’ve mostly used Scanner to parse keyboard input. You can also have it parse a plain old String. Just feed it the String when you construct it:

Scanner in = new Scanner(yourString);

The control how the Scanner parses text, see the useDelimiter and next methods.

Test your code on a few example URLs.

Checkpoint 2

Person B types.

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

In this checkpoint, you’ll use Robot to hijack a drawing program.

  1. Create a main method that constructs an instance of the Robot class. You’ll see some red in Eclipse, stating that the code can throw an exception. Hover and click the “Add throws declaration” quick fix.
  2. Write a method name drawDot. Accept a Robot and and x-y pixel location as parameters. Use the various Robot methods to simulate a mouse click (press and release) at the given location. You’ll likely want to check out the documentation for the Robot class.
  3. Open up a drawing program like Paint and make sure you’ve selected a tool like the paintbrush, which can place marks on the canvas.
  4. Back in Eclipse, call your dot-drawing method so that it draws a dot on your canvas. Before calling it, have the robot delay a bit so that, when you run the program, you have time to switch to the paint program. Test before moving on.
  5. Now, add a drawFiveDots method that accepts five parameters: a robot, an x location, a y location, a delta x, and a delta y. It draws a first dot at (x, y), a second dot at (x + dx, y + dy), a third dot at (x + 2dx, y + 2dy), and so on. Test before moving on.
  6. In main, call drawFiveDots so that it traces out a square.