Deck 5: Program Logic and Indefinite Loops

Full screen (f)
exit full mode
Question
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.
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.  <div style=padding-top: 35px>
Use Space or
up arrow
down arrow
to flip the card.
Question
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).
 Expression  Value 3+45/2      13%5+43%(11%3)      1.53.0+25.0/10.0      7/2!=123/12%7      5/2+123/10/10.0      5+2+"(1+1)"+4+23      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\3+4 * 5 / 2 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\13 \% 5+43\%(11 \%3) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\1.5 * 3.0+25.0 / 10.0 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\7 / 2 !=123 / 12 \%7 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5 / 2+123 / 10 / 10.0 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5+2+"(1+1) "+4+2 * 3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\end{array}

Question
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);
}
}
Question
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;
}
 Method Call  Value Returned  mystery (2);       mystery (1);       mystery (7);       mystery (18);       mystery (43);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Value Returned }} \\\text { mystery }(2) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(-1) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(7) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(18) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(43) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
Question
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.
 Call  Returns  consecutiveDigits (0) 1 consecutiveDigits (18) 1 consecutiveDigits (394) 1 consecutiveDigits (99) 2 consecutiveDigits (8229) 2 Call  Returns  consecutiveDigits (8823) 2 consecutiveDigits (777)3 consecutiveDigits (82888)3 consecutiveDigits (7111171)4 consecutiveDigits (233333888)5\begin{array}{l|c}\text { Call } & \text { Returns } \\\hline \text { consecutiveDigits (0) } & 1 \\\text { consecutiveDigits (18) } & 1 \\\text { consecutiveDigits (394) } & 1 \\\text { consecutiveDigits (99) } & 2 \\\text { consecutiveDigits (8229) } & 2\end{array}\begin{array}{}\\\\\\\\\\\\\end{array}\begin{array}{l|c}\text { Call } & \text { Returns } \\\hline \text { consecutiveDigits (8823) } & 2 \\\text { consecutiveDigits }(777) & 3 \\\text { consecutiveDigits }(82888) & 3 \\\text { consecutiveDigits }(7111171) & 4 \\\text { consecutiveDigits }(233333888) & 5\end{array}
Question
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;
}
flip==0 heads ==0 flip > heads  Point A  Point B  Point C  Point D  Point E \begin{array}{|l|l|l|l|}\hline & f l i p==0 & \text { heads }==0 & \text { flip }>\text { heads } \\\hline \text { Point A } & & & \\\hline \text { Point B } & & & \\\hline \text { Point C } & & & \\\hline \text { Point D } & & & \\\hline \text { Point E } & & & \\\hline\end{array}
Question
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:
 Call  Random r= new Random (); print TwoDigit (r,4); Random r= new Random ( ):  Orint TwoDigit (r,7); Output  next =52 next =83 next =10 next =29 next =96 next =42 next =86 next =22 no 42 was seen.  next =91 next =36 next =73 we saw a 42!\begin{array}{l|l|l}\text { Call } & \begin{array}{l}\text { Random } r=\text { new Random }() ; \\\text { print TwoDigit }(r, 4) ;\end{array} & \begin{array}{l}\text { Random } r=\text { new Random ( ): } \\\text { Orint TwoDigit }(r, 7) ;\end{array} \\\hline \text { Output } & \text { next }=52 & \text { next }=83 \\&\text { next }=10 & \text { next }=29 \\&\text { next }=96 & \text { next }=42 \\&\text { next }=86 & \text { next }=22 \\&\text { no } 42 \text { was seen. } & \text { next }=91 \\& & \text { next }=36 \\&&\text { next }=73 \\& & \text { we saw a } 42 !\end{array}
Question
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);
}
 Method Call  Output mystery(40);      mystery(8);      mystery(0);      mystery(12);      mystery(20);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output }} \\mystery (40); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\mystery (8); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\mystery (0); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\mystery (12); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\mystery (20); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/8
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
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.
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.
(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);
}
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).
 Expression  Value 3+45/2      13%5+43%(11%3)      1.53.0+25.0/10.0      7/2!=123/12%7      5/2+123/10/10.0      5+2+"(1+1)"+4+23      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\3+4 * 5 / 2 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\13 \% 5+43\%(11 \%3) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\1.5 * 3.0+25.0 / 10.0 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\7 / 2 !=123 / 12 \%7 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5 / 2+123 / 10 / 10.0 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5+2+"(1+1) "+4+2 * 3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\end{array}

 Expression  Value 3+45/21313%5+43%(11%3)41.53.0+25.0/10.07.07/2!=123/12%7false5/2+123/10/10.03.25+2+"(1+1)"+4+23"7(1+1)46"\begin{array}{l|c}\underline{ \text { Expression } }& \underline{ \text { Value }} \\3+4 * 5 / 2 & 13 \\13 \% 5+43\%(11 \%3) & 4\\1.5 * 3.0+25.0 / 10.0 & 7.0\\7 / 2 !=123 / 12 \%7 & false \\5 / 2+123 / 10 / 10.0 & 3.2\\5+2+"(1+1) "+4+2 * 3 & "7(1+1)46"\end{array}
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);
}
}
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
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;
}
 Method Call  Value Returned  mystery (2);       mystery (1);       mystery (7);       mystery (18);       mystery (43);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Value Returned }} \\\text { mystery }(2) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(-1) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(7) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(18) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(43) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
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.
 Call  Returns  consecutiveDigits (0) 1 consecutiveDigits (18) 1 consecutiveDigits (394) 1 consecutiveDigits (99) 2 consecutiveDigits (8229) 2 Call  Returns  consecutiveDigits (8823) 2 consecutiveDigits (777)3 consecutiveDigits (82888)3 consecutiveDigits (7111171)4 consecutiveDigits (233333888)5\begin{array}{l|c}\text { Call } & \text { Returns } \\\hline \text { consecutiveDigits (0) } & 1 \\\text { consecutiveDigits (18) } & 1 \\\text { consecutiveDigits (394) } & 1 \\\text { consecutiveDigits (99) } & 2 \\\text { consecutiveDigits (8229) } & 2\end{array}\begin{array}{}\\\\\\\\\\\\\end{array}\begin{array}{l|c}\text { Call } & \text { Returns } \\\hline \text { consecutiveDigits (8823) } & 2 \\\text { consecutiveDigits }(777) & 3 \\\text { consecutiveDigits }(82888) & 3 \\\text { consecutiveDigits }(7111171) & 4 \\\text { consecutiveDigits }(233333888) & 5\end{array}
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;
}
flip==0 heads ==0 flip > heads  Point A  Point B  Point C  Point D  Point E \begin{array}{|l|l|l|l|}\hline & f l i p==0 & \text { heads }==0 & \text { flip }>\text { heads } \\\hline \text { Point A } & & & \\\hline \text { Point B } & & & \\\hline \text { Point C } & & & \\\hline \text { Point D } & & & \\\hline \text { Point E } & & & \\\hline\end{array}
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:
 Call  Random r= new Random (); print TwoDigit (r,4); Random r= new Random ( ):  Orint TwoDigit (r,7); Output  next =52 next =83 next =10 next =29 next =96 next =42 next =86 next =22 no 42 was seen.  next =91 next =36 next =73 we saw a 42!\begin{array}{l|l|l}\text { Call } & \begin{array}{l}\text { Random } r=\text { new Random }() ; \\\text { print TwoDigit }(r, 4) ;\end{array} & \begin{array}{l}\text { Random } r=\text { new Random ( ): } \\\text { Orint TwoDigit }(r, 7) ;\end{array} \\\hline \text { Output } & \text { next }=52 & \text { next }=83 \\&\text { next }=10 & \text { next }=29 \\&\text { next }=96 & \text { next }=42 \\&\text { next }=86 & \text { next }=22 \\&\text { no } 42 \text { was seen. } & \text { next }=91 \\& & \text { next }=36 \\&&\text { next }=73 \\& & \text { we saw a } 42 !\end{array}
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);
}
 Method Call  Output mystery(40);      mystery(8);      mystery(0);      mystery(12);      mystery(20);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text { Output }} \\mystery (40); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\mystery (8); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\mystery (0); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\mystery (12); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\mystery (20); & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 8 flashcards in this deck.