teaching machines

CS 145 Lab 3 – Methods and Logic

October 2, 2015 by . Filed under cs145, fall 2015, 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! Please work with someone that you did not work with last week.

Objective

In this lab, we’ll focus on the two big ideas we’ve been discussing in lecture lately. First we visit methods, which let us compose subprograms—little machines that accomplish very narrow tasks but out of which larger programs emerge. Second, to make our software respond to the world around us, it must ask questions of data. We ask questions using relational and logical operators. We will look at these operators and conditional statements in this lab.

Checkpoint 1

Person A 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 input events even in other programs.

In this checkpoint, you’ll use Robot to hijack a drawing program. Let’s draw a square in Paint:

  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 as parameters a Robot and and an xy pixel location (two ints) as parameters. Use the various Robot methods to simulate a mouse click (press and release) at the given location. Check out the documentation for the Robot class for details. Call your method from main.
  3. Now let’s test this method in isolation. 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. Back in Eclipse, call your dot-drawing method so that it draws a dot on your canvas. As soon as you run your program, switch to Paint so that it’s the frontmost window and will receive the mouse click. However, you won’t be faster than the computer. Delay the Robot a bit using one of its available methods. Don’t move on until this works!
  4. Now, add a drawFiveDotsLine 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. It shouldn’t do any drawing directly—use your other methods. Test before moving on.
  5. Finally, in main, call drawFiveDotsLine so that it traces out a square. What will be the dx for the vertical lines? The dy for the horizontal lines? The end result should look something like this:

    A square traced out by Robot, with the paintbrush tweaked to draw a blue star.

    A square traced out by Robot, with the paintbrush tweaked to draw a blue star.

Checkpoint 2

Person B types.

Declare and assign the following variables, all describing some individual person:

Now, craft expressions using the variables above for the following derived attributes of this person. Do not write if statements; write only boolean expressions. Test them under various values of the person’s variables.

  1. A person is eligible for benefits if he/she works half-time (20 hours) or more on average each week.
    boolean isBenefitsEligible =
  2. A person is a teenager if he/she is between 13 and 19 years old, inclusively.
    boolean isTeenager =
  3. A person is a bachelor if he is an unmarried man.
    boolean isBachelor =
  4. A person is busy if he/she has children or typically works over 45 hours a week on average.
    boolean isBusy =
  5. See the variable name.
    boolean notBornInOctober1990 =
  6. A person goes to Starbucks if he/she earns over $30,000 or drinks coffee.
    boolean goesToStarbucks = 
  7. A person can have children if he is male. A female can also have children if she is less than 45 years old and does not drink coffee.
    boolean canHaveChildren =