|
predict your score. All accurate predictions will be rewarded! |
Number |
Points |
Score |
| 1 | 25 | ||
|
|
|
||
|
|
|
||
|
|
|
||
|
Total
|
|
These are shortcut links to the individual questions:
Number 1: Short Answer
Number 2: Code Interpretation
Number 3: Code Writing
Number 4: Code Interpretation
The answers appear in colored fonts most of the time.
count = count + 1; count += 1;
count++;
Mixing ints and doubles in an arithmetic operation
causes the int value to be cast as a double, yielding a double result.
getBankRoll is an accessor method, setBankRoll
a modifier method. These are nearly always one-liners:
public int getBankRoll() {
return bankRoll;
}
public void setBankRoll(int newValue) {
bankRoll = newValue;
}
Explain why this is so. In particular, 6 + 3 = 9 not 8, so what's up?
The second line accomplishes four things, more or less in this order (the fact that the increment to num is done last, is all that really matters): A memory location for an int variable num2 is set aside; then the value (num + 3) = 8 is calculated; then 8 is assigned to num2; and finally num is incremented (to 6).
public int getIntLowerOnly(String promptString, int lower) {
int userInput;
do {
userInput = getInt(promptString);
if (userInput < lower)
displayResult("Your
input is less than " + lower +
". Please try again.");
} while (userInput < lower);
return userInput;
}
The method repeatedly prompts the user (using the 1st argument as a text prompt) for an integer, unitl the user enters a value bigger than or equal to the second argument (lower). If the user enters a value which is too small, an error message is displayed and the loop body is repeated. This is accomplished using a do-while loop, since in all cases, we must ask the user for a value at least one time. A typical call might be:
if ( (N < 0) || ( (N % 2) == 0) )
N += 5;
else
N--;
switch(N) {
case 1: return "1st";
case 2: return "2nd";
case 3: return "3rd";
case 4: return "4th";
case 5: return "5th";
default: return "Your
input is out of range.";
}
(where "n" is the String equivalent of any number equal to 2 or more), under the respective conditions that (numberOfNickels is zero), (numberOfNickels is one), or (numberOfNickels is bigger than or equal to two). Assuming that all values of numberOfNickels from zero to 10 are equally likely, what is the code you would use to replace the one "slacker line of code"? Note: The last statement clearly indicates that the most common case is when numberOfNickels is larger than 1. Exceptional CS students will take advantage of this fact.
if (numberOfNickels >= 2)
displayResult("you need
" + numberOfNickels + " nickels.");
else if (numberOfNickels == 1)
displayResult("you need
1 nickel");
else
displayResult("you do
not need any nickels.");
public static void main(String [] args)
{
int n = 4;
int x = 6;
do {
switch (x) {
case 1: n *= 3;
break;
case 3: n -= 6;
break;
default: n++;
break;
}
x = methodA(n);
System.out.println("x= " + x + " and n = " + n);
} while (n > 0);
} // end main
All Java programs begin by executing the main method, which begins by setting n = 4 and x = 6. Since x = 6, the default case of the switch block is executed, so n is incremented to 5 and we break from the switch block. The value of x is set to methodA(5) = 7, since the if test is false (5 is NOT less than 5). We hit the print line, which yields x = 7 and n = 5. Since 5 > 0, we repeat the loop body. With less detail we proceed:
Event x n start of second loop: 7 5 switch(7) 7 6 x = methodA(6) = 2, then print these values and repeat 2 6 switch(2) 2 7 x = methodA(7) = 3, print and repeat 3 7 switch(3) 3 1 x = methodA(1) = 3, print and repeat 3 1 switch(3) 3 -5 x = methodA(-5) = -3, print and stop (n > 0) is false. -3 -5