Sample #2 Answer Key
Part | A | B | C |
(a) | 67 | ||
(b) | 1 | ||
(c) | -14 | ||
(d) | 5 | 3 | |
(e) | 5 | 7 | |
(f) | 4 | 2 | |
(g) | 4 |
[Two of many possible solutions are below. If you did poorly on this question, make sure that you understand your mistakes.]
[solution 1]
// computes the pay of a worker
public void computePay (int hours, int dollars_per_hour)
{
int total; // total pay earned
// if worked <= 40 hours then no overtime pay
if ( hours <= 40) {
total = hours * dollars_per_hour;
}
// otherwise comute pay including overtime
else {
total = (hours – 40) * 2 * dollars_per_hour + 40 * dollars_per_hour;
}
// print the result
System.out.println("The total pay earned was:" + total);
}
[solution 2]
// computes the pay of a worker
public void computePay (int hours, int dollars_per_hour)
{
// double the overtime hours
if (hours > 40) {
hours = hours + (hours – 40);
}
// compute pay and print the result
System.out.println(hours * dollars_per_hour);
}
[Remember: the upperleft corner of the drawing window is (0,0); drawOval requires the coordinate of the upperleft corner of the box that surrounds the oval.]
public void drawNextBall(Graphics g, int r, int x, int y)
{
g.drawOval(x+3, y+3, 2*r, 2*r);
}
[If you did poorly on this question, be sure to understand (a) WHY your solution was wrong and (b) how the solution below works.]
// Simulates an ATM session
public class Question {
public static void main(String args[])
{
Atm my_atm;
// create new Atm instance
my_atm = new Atm();
// initialize and print balance
my_atm.balance = 1000;
my_atm.printBalance();
// deposit $50 and print new balance
my_atm.deposit(50);
my_atm.printBalance();
}
}
[The trickiest part here is the last two lines of output. Be sure that you understand them.]
The Output would be:
0 0
0 0
5 10
10 5
5 10
5 10
7 8
7 8