CS 145 Lab 3 – Methods and Logic
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:
- Create a
main
method that constructs an instance of theRobot
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. - Write a method name
drawDot
. Accept as parameters aRobot
and and an xy pixel location (twoint
s) as parameters. Use the variousRobot
methods to simulate a mouse click (press and release) at the given location. Check out the documentation for theRobot
class for details. Call your method frommain
. - 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! - 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. - Finally, in
main
, calldrawFiveDotsLine
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:
Checkpoint 2
Person B types.
Declare and assign the following variables, all describing some individual person:
int ageInYears
int salaryInDollars
boolean isMale
int kidCount
boolean isMarried
double avgWeeklyHoursWorked
int birthMonth
int birthYear
boolean drinksCoffee
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.
- A person is eligible for benefits if he/she works half-time (20 hours) or more on average each week.
boolean isBenefitsEligible =
- A person is a teenager if he/she is between 13 and 19 years old, inclusively.
boolean isTeenager =
- A person is a bachelor if he is an unmarried man.
boolean isBachelor =
- A person is busy if he/she has children or typically works over 45 hours a week on average.
boolean isBusy =
- See the variable name.
boolean notBornInOctober1990 =
- A person goes to Starbucks if he/she earns over $30,000 or drinks coffee.
boolean goesToStarbucks =
- 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 =