Question | Value | Score |
1 | 10 | |
2 | 15 | |
3 | 10 | |
4 | 15 | |
5 | 15 | |
6 | 10 | |
7 | 10 | |
8 | 15 | |
Total | 100 |
1. Write a boolean method, called xyz, that returns true if and only if its int argument is divisible by 3, 5, 7, and 11.
2. Write a method, called magic, that has 3 boolean arguments and behaves according to the truth table below.
arg1 | arg2 | arg3 | returns |
true | true | true | false |
true | true | false | true |
true | false | true | true |
true | false | false | true |
false | true | true | false |
false | true | false | true |
false | false | true | false |
false | false | false | false |
3. What is the output of the applet below (approximate the String positions, based on the coordinates given)?
public
void paint( Graphics g ) {
int i = 7;
g.drawString( "i = " + i, 50, j += 15);
xyz(g);
abc(g);
xyz(g);
abc(g);
g.drawString( "i = " + i, 50, j += 15);
}
void xyz(Graphics g) {
g.drawString( "i = " + i, 50, j += 15);
i *= 9;
g.drawString( "i = " + i, 50, j += 15);
}
void
abc(Graphics g) {
int i = 25;
g.drawString( "i = " + i, 50, j += 15);
++i;
g.drawString( "i = " + i, 50, j += 15);
}
}
4. Write a Java expression that evaluates to a "random" int value in the set {17, 18, 19, ..., 100}. If it cannot be done, say "It cannot be done."
5. Convert
the following if statement
to equivalent Java statements without using boolean operators. If
it cannot be done, say "It cannot be done."
6. Write a method or pair of methods called magic that returns true, if its argument is an int and false if its argument is a double. If it cannot be done, say "It cannot be done."
public
void paint( Graphics g ) {
g.drawString(magic(1) + " | " + magic(1.0), 20, 20);
}
//
your code would go here
}
8. You have the following applet:
public void init() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
pots[i][j] = new Color(i*64, 0, j*64);
}
public void paint(Graphics g) {
// paint palette
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
g.setColor(pots[i][j]);
g.fillRect(paletteX + j*potSize, paletteY + i*potSize, potSize, potSize);
}
// paint active color
g.setColor(active);
g.fillRect(0, 0, potSize, potSize);
}
public boolean mouseUp(Event event, int x, int y) {
if (x >= paletteX && x <= paletteX + 4*potSize &&
y >= paletteY && y <= paletteY + 4*potSize)
active = select(x, y);
repaint();
return true;
}
public Color select(int x, int y) {
//your code goes here
}
}