Deck 5: Program Logic and Indefinite Loops
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/8
Play
Full screen (f)
Deck 5: Program Logic and Indefinite Loops
1
Programming (15 points)
In this question, we'll address the following problem: Can a cash register containing a given amount of pennies (1-cent coins) and a given amount of nickels (5-cent coins) give a customer a given exact amount of cents of change? For example, if there are 3 pennies and 5 nickels in the cash register, is it possible to give exactly 19 cents of change? (No.) If there are 2 pennies and 7 nickels in the register, is it possible to give exactly 26 cents of change? (Yes.)
Write a static method named canMakeChange that accepts three integer parameters representing the number of pennies in the cash register, the number of nickels in the cash register, and the desired amount of change to make. The method should return true if the coins in the register can produce this exact amount of change, and false if not. The coins in the register must be able to exactly produce the desired amount of change in order to return true; for example, if the register contains 0 pennies and 100 nickels, it is not able to exactly produce 8 cents of change.
The following are several sample calls to your method and the values they should return. You may assume that no negative parameter values are passed, but otherwise your method should work with any values passed.

In this question, we'll address the following problem: Can a cash register containing a given amount of pennies (1-cent coins) and a given amount of nickels (5-cent coins) give a customer a given exact amount of cents of change? For example, if there are 3 pennies and 5 nickels in the cash register, is it possible to give exactly 19 cents of change? (No.) If there are 2 pennies and 7 nickels in the register, is it possible to give exactly 26 cents of change? (Yes.)
Write a static method named canMakeChange that accepts three integer parameters representing the number of pennies in the cash register, the number of nickels in the cash register, and the desired amount of change to make. The method should return true if the coins in the register can produce this exact amount of change, and false if not. The coins in the register must be able to exactly produce the desired amount of change in order to return true; for example, if the register contains 0 pennies and 100 nickels, it is not able to exactly produce 8 cents of change.
The following are several sample calls to your method and the values they should return. You may assume that no negative parameter values are passed, but otherwise your method should work with any values passed.

(six solutions shown)
public static boolean canMakeChange(int pennies, int nickels, int cents) {
if (cents % 5 > pennies) {
return false;
} else if (pennies + 5 * nickels < cents) {
return false;
} else {
return true;
}
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
while (cents >= 5 && nickels > 0) {
cents -= 5;
nickels--;
}
while (cents > 0 && pennies > 0) {
cents--;
pennies--;
}
return cents == 0;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
for (int p = 0; p <= pennies; p++) {
for (int n = 0; n <= nickels; n++) {
if (p + 5 * n == cents) {
return true;
}
}
}
return false;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
if (nickels * 5 >= cents) {
return (pennies >= cents % 5); // enough nickels to cover all except % 5 part
} else {
return (pennies >= cents - nickels * 5); // not enough nickels; need pennies
}
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
cents -= Math.min(nickels * 5, cents - cents % 5);
return cents - pennies <= 0;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
return (pennies >= cents % 5) && (pennies + 5 * nickels >= cents);
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
if (cents % 5 > pennies) {
return false;
} else if (pennies + 5 * nickels < cents) {
return false;
} else {
return true;
}
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
while (cents >= 5 && nickels > 0) {
cents -= 5;
nickels--;
}
while (cents > 0 && pennies > 0) {
cents--;
pennies--;
}
return cents == 0;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
for (int p = 0; p <= pennies; p++) {
for (int n = 0; n <= nickels; n++) {
if (p + 5 * n == cents) {
return true;
}
}
}
return false;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
if (nickels * 5 >= cents) {
return (pennies >= cents % 5); // enough nickels to cover all except % 5 part
} else {
return (pennies >= cents - nickels * 5); // not enough nickels; need pennies
}
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
cents -= Math.min(nickels * 5, cents - cents % 5);
return cents - pennies <= 0;
}
public static boolean canMakeChange(int pennies, int nickels, int cents) {
return (pennies >= cents % 5) && (pennies + 5 * nickels >= cents);
}
2
Expressions
For each expression in the left-hand column, indicate its value in the right-hand column.
Be sure to list a constant of appropriate type.
(e.g., 7.0 rather than 7 for a double, Strings in quotes, true or false for a boolean).
For each expression in the left-hand column, indicate its value in the right-hand column.
Be sure to list a constant of appropriate type.
(e.g., 7.0 rather than 7 for a double, Strings in quotes, true or false for a boolean).
3
Parameter Mystery
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
public class ParameterMystery {
public static void main(String[] args) {
String p = "cause";
String q = "support";
String r = "troops";
String support = "hillary";
String cause = "rudy";
troops(p, q, r);
troops(q, r, p);
troops(support, p, cause);
troops(r, "p", support);
troops(q, "cause", q);
}
public static void troops(String r, String p, String q) {
System.out.println(q + " gave " + r + " to the " + p);
}
}
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
public class ParameterMystery {
public static void main(String[] args) {
String p = "cause";
String q = "support";
String r = "troops";
String support = "hillary";
String cause = "rudy";
troops(p, q, r);
troops(q, r, p);
troops(support, p, cause);
troops(r, "p", support);
troops(q, "cause", q);
}
public static void troops(String r, String p, String q) {
System.out.println(q + " gave " + r + " to the " + p);
}
}
troops gave cause to the support
cause gave support to the troops
rudy gave hillary to the cause
hillary gave troops to the p
support gave support to the cause
cause gave support to the troops
rudy gave hillary to the cause
hillary gave troops to the p
support gave support to the cause
4
While Loop Simulation
For each call below to the following method, write the value that is returned:
public static int mystery(int x) {
int a = 1;
int c = 0;
while (x > 0) {
a = x % 2;
if (a == 1) {
c++;
}
x = x / 2;
}
return c;
}
For each call below to the following method, write the value that is returned:
public static int mystery(int x) {
int a = 1;
int c = 0;
while (x > 0) {
a = x % 2;
if (a == 1) {
c++;
}
x = x / 2;
}
return c;
}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
5
Programming
Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4.
For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on the method. You are not allowed to use a String to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.
Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4.
For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on the method. You are not allowed to use a String to solve this problem. You may assume that the value passed to the method is greater than or equal to 0.
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
6
Assertions
For the following method, identify each of the three assertions in the table below as being either ALWAYS true, NEVER true or SOMETIMES true / sometimes false at each labeled point in the code. (can abbreviate as A/N/S)
public static int threeHeads() { // Counts coin tosses till we get heads 3x in a row.
Random rand = new Random();
int flip = 1;
int heads = 0;
int count = 0;
// Point A
while (heads < 3) {
// Point B
flip = rand.nextInt(2); // flip coin
if (flip == 0) { // heads
heads++;
// Point C
} else { // tails
// Point D
heads = 0;
}
count++;
}
// Point E
return count;
}
For the following method, identify each of the three assertions in the table below as being either ALWAYS true, NEVER true or SOMETIMES true / sometimes false at each labeled point in the code. (can abbreviate as A/N/S)
public static int threeHeads() { // Counts coin tosses till we get heads 3x in a row.
Random rand = new Random();
int flip = 1;
int heads = 0;
int count = 0;
// Point A
while (heads < 3) {
// Point B
flip = rand.nextInt(2); // flip coin
if (flip == 0) { // heads
heads++;
// Point C
} else { // tails
// Point D
heads = 0;
}
count++;
}
// Point E
return count;
}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
7
Programming
Write a static method named printTwoDigit that accepts a Random object and an integer n as parameters and that prints a series of n randomly generated two-digit numbers. The method should use the Random object to select numbers in the range of 10 to 99 inclusive where each number is equally likely to be chosen. After displaying each number that was produced, the method should indicate whether the number 42 was ever selected ("we saw a 42!") or not ("no 42 was seen."). You may assume that the value of n passed is at least 0.
The following table shows two sample calls and their output:
Write a static method named printTwoDigit that accepts a Random object and an integer n as parameters and that prints a series of n randomly generated two-digit numbers. The method should use the Random object to select numbers in the range of 10 to 99 inclusive where each number is equally likely to be chosen. After displaying each number that was produced, the method should indicate whether the number 42 was ever selected ("we saw a 42!") or not ("no 42 was seen."). You may assume that the value of n passed is at least 0.
The following table shows two sample calls and their output:
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
8
If/Else Simulation
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void mystery(int n) {
System.out.print(n + " ");
if (n > 10) {
n = n / 2;
} else {
n = n + 7;
}
if (n * 2 < 25) {
n = n + 10;
}
System.out.println(n);
}
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void mystery(int n) {
System.out.print(n + " ");
if (n > 10) {
n = n / 2;
} else {
n = n + 7;
}
if (n * 2 < 25) {
n = n + 10;
}
System.out.println(n);
}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck