Deck 4: Conditional Execution
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 4: Conditional Execution
1
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 if (n < 10) {
n = n * 2;
}
if (n % 2 == 1) {
n++;
} else {
n--;
}
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 if (n < 10) {
n = n * 2;
}
if (n % 2 == 1) {
n++;
} else {
n--;
}
System.out.println(n);
}
2
Programming
Write a static method named randomRects that calculates and displays the area of randomly-generated rectangles. The width and height of each rectangle should be a randomly generated integer between 1 and 10 inclusive. Your method should keep generating random rectangles until an increasing sequence of four areas is printed. In other words, if the last four rectangles generated have areas of a1, a2, a3 and a4 such that a1 < a2 < a3 < a4, the method should print the final message and stop. So your method will generate at least 4 total rectangles but possibly many more, stopping only when it sees 4 in a row with areas in increasing order.
The following calls demonstrate your method's behavior. Your method should match this output format exactly:
Write a static method named randomRects that calculates and displays the area of randomly-generated rectangles. The width and height of each rectangle should be a randomly generated integer between 1 and 10 inclusive. Your method should keep generating random rectangles until an increasing sequence of four areas is printed. In other words, if the last four rectangles generated have areas of a1, a2, a3 and a4 such that a1 < a2 < a3 < a4, the method should print the final message and stop. So your method will generate at least 4 total rectangles but possibly many more, stopping only when it sees 4 in a row with areas in increasing order.
The following calls demonstrate your method's behavior. Your method should match this output format exactly:
(three solutions shown)
public static void randomRects() {
Random r = new Random();
int last = 100; // can also be 0
int count = 0;
while (count < 4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
System.out.println("w: " + w + ", h: " + h + ", area: " + w * h);
if (last < w * h) {
count++;
} else {
count = 1; // need to count first rect in sequence
}
last = w * h;
}
System.out.println("Four rectangles of increasing area.");
}
public static void randomRects() {
Random r = new Random();
int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
while (a1 == 0 || a1 >= a2 || a2 >= a3 || a3 >= a4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
a1 = a2;
a2 = a3;
a3 = a4;
a4 = w * h;
System.out.println("w: " + w + ", h: " + h + ", area: " + a4);
}
System.out.println("Four rectangles of increasing area.");
}
public static void randomRects() {
Random r = new Random();
int area = 0;
int last = 0;
int count = 0;
while (count != 4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
area = w * h;
if (area <= last) {
count = 1;
} else {
count++;
}
System.out.println("w: " + w + ", h: " + h + ", area: " + area);
last = area;
}
System.out.println("Four rectangles of increasing area.");
}
public static void randomRects() {
Random r = new Random();
int last = 100; // can also be 0
int count = 0;
while (count < 4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
System.out.println("w: " + w + ", h: " + h + ", area: " + w * h);
if (last < w * h) {
count++;
} else {
count = 1; // need to count first rect in sequence
}
last = w * h;
}
System.out.println("Four rectangles of increasing area.");
}
public static void randomRects() {
Random r = new Random();
int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
while (a1 == 0 || a1 >= a2 || a2 >= a3 || a3 >= a4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
a1 = a2;
a2 = a3;
a3 = a4;
a4 = w * h;
System.out.println("w: " + w + ", h: " + h + ", area: " + a4);
}
System.out.println("Four rectangles of increasing area.");
}
public static void randomRects() {
Random r = new Random();
int area = 0;
int last = 0;
int count = 0;
while (count != 4) {
int w = r.nextInt(10) + 1;
int h = r.nextInt(10) + 1;
area = w * h;
if (area <= last) {
count = 1;
} else {
count++;
}
System.out.println("w: " + w + ", h: " + h + ", area: " + area);
last = area;
}
System.out.println("Four rectangles of increasing area.");
}
3
Programming
Write a static method named graduation that takes a student's GPA, total credit count, and honors credit count as parameters, and returns a String representing that student's graduation status. The total credit count already includes the honors credits. The graduation status to return is determined by the following rules:
-Students must have completed at least 180 credits with a GPA of at least 2.0 to graduate. A student who does not meet both of these constraints should receive a return value of "not graduating".
-Students who do have enough credits to graduate and sufficiently high GPAs will receive one of four return values depending on the GPA and number of honors credits:
-All students with GPAs between 2.0 and 3.6 receive a return value of "graduating".
-Students with fewer than 15 honors credits receive a return of "cum laude" if their GPA is at least 3.6 but less than 3.8, and a return of "magna cum laude" if their GPA is at least 3.8.
-Students with 15 or more honors credits receive a return of "magna cum laude" if their GPA is at least 3.6 but less than 3.8, and a return of "summa cum laude" if their GPA is at least 3.8.
Here are some example calls to the method and their resulting return values:
You may assume that the GPA will be between 0.0 and 4.0 and that both credit counts will be non-negative integers.
Write a static method named graduation that takes a student's GPA, total credit count, and honors credit count as parameters, and returns a String representing that student's graduation status. The total credit count already includes the honors credits. The graduation status to return is determined by the following rules:
-Students must have completed at least 180 credits with a GPA of at least 2.0 to graduate. A student who does not meet both of these constraints should receive a return value of "not graduating".
-Students who do have enough credits to graduate and sufficiently high GPAs will receive one of four return values depending on the GPA and number of honors credits:
-All students with GPAs between 2.0 and 3.6 receive a return value of "graduating".
-Students with fewer than 15 honors credits receive a return of "cum laude" if their GPA is at least 3.6 but less than 3.8, and a return of "magna cum laude" if their GPA is at least 3.8.
-Students with 15 or more honors credits receive a return of "magna cum laude" if their GPA is at least 3.6 but less than 3.8, and a return of "summa cum laude" if their GPA is at least 3.8.
Here are some example calls to the method and their resulting return values:
You may assume that the GPA will be between 0.0 and 4.0 and that both credit counts will be non-negative integers.
(two solutions shown)
public static String graduation(double gpa, int credits, int honorsCredits) {
if (credits >= 180 && gpa >= 2.0) {
if (honorsCredits >= 15 && gpa >= 3.8) {
return "summa cum laude";
} else if ((honorsCredits >= 15 && gpa >= 3.6) || gpa >= 3.8) {
return "magna cum laude";
} else if (gpa >= 3.6) {
return "cum laude";
} else {
return "graduating";
}
} else {
return "not graduating";
}
}
public static String graduation(double gpa, int credits, int honorsCredits) {
if (credits < 180 || gpa < 2.0) {
return "not graduating";
} else if (honorsCredits >= 15 && gpa >= 3.8) {
return "summa cum laude";
} else if (honorsCredits >= 15 && gpa >= 3.6 || gpa >= 3.8) {
return "magna cum laude";
} else if (gpa >= 3.6) {
return "cum laude";
} else {
return "graduating";
}
}
public static String graduation(double gpa, int credits, int honorsCredits) {
if (credits >= 180 && gpa >= 2.0) {
if (honorsCredits >= 15 && gpa >= 3.8) {
return "summa cum laude";
} else if ((honorsCredits >= 15 && gpa >= 3.6) || gpa >= 3.8) {
return "magna cum laude";
} else if (gpa >= 3.6) {
return "cum laude";
} else {
return "graduating";
}
} else {
return "not graduating";
}
}
public static String graduation(double gpa, int credits, int honorsCredits) {
if (credits < 180 || gpa < 2.0) {
return "not graduating";
} else if (honorsCredits >= 15 && gpa >= 3.8) {
return "summa cum laude";
} else if (honorsCredits >= 15 && gpa >= 3.6 || gpa >= 3.8) {
return "magna cum laude";
} else if (gpa >= 3.6) {
return "cum laude";
} else {
return "graduating";
}
}
4
Parameter Mystery
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
(Though the program uses words related to arithmetic, the output does not necessarily follow the rules of addition.)
public class ParameterMystery {
public static void main(String[] args) {
int one = 4;
int two = 3;
int three = 10;
int num = 17;
int four = 3;
racket(one, two, three);
racket(three, four, 5);
racket(2, two * 2, num);
racket(num, three * one, four);
racket(three - four, one, two);
}
public static void racket(int two, int one, int three) {
System.out.println(three + " is roughly " + two + " plus " + one);
}
}
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
(Though the program uses words related to arithmetic, the output does not necessarily follow the rules of addition.)
public class ParameterMystery {
public static void main(String[] args) {
int one = 4;
int two = 3;
int three = 10;
int num = 17;
int four = 3;
racket(one, two, three);
racket(three, four, 5);
racket(2, two * 2, num);
racket(num, three * one, four);
racket(three - four, one, two);
}
public static void racket(int two, int one, int three) {
System.out.println(three + " is roughly " + two + " plus " + one);
}
}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
5
While Loop 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 x, int y) {
int s = 0;
while (x > 0 && 2 * y >= x) {
System.out.print(s + " ");
y = y - x;
x--;
s = s + x;
}
System.out.println(s);
}
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 x, int y) {
int s = 0;
while (x > 0 && 2 * y >= x) {
System.out.print(s + " ");
y = y - x;
x--;
s = s + x;
}
System.out.println(s);
}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
6
Assertions
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false.
public static int mystery(int a) {
int b = 0;
int c = 0;
// Point A
while (a != 0) {
// Point B
c = a % 10;
if (c % 2 == 0) {
b++;
} else {
b = 0;
// Point C
}
a = a / 10;
// Point D
}
// Point E
return b;
}
Fill in each box of the table below with one of the following words: ALWAYS, NEVER or SOMETIMES.
(You may abbreviate these choices as A, N, and S.)
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false.
public static int mystery(int a) {
int b = 0;
int c = 0;
// Point A
while (a != 0) {
// Point B
c = a % 10;
if (c % 2 == 0) {
b++;
} else {
b = 0;
// Point C
}
a = a / 10;
// Point D
}
// Point E
return b;
}
Fill in each box of the table below with one of the following words: ALWAYS, NEVER or SOMETIMES.
(You may abbreviate these choices as A, N, and S.)
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
7
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 and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, 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 and capitalization.
e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
8
Programming
Write a static method named cheerleader that accepts two integer parameters lines and cheers and prints a series of "cheer" lines at increasing levels of indentation. The first parameter represents the number of lines of output to print, and the second represents the number of "cheers" per line. For example, the call of cheerleader(2, 4) means that you should print 2 lines of output, each containing 4 "cheers." A "cheer" is an occurrence of the word "Go" in the output. Neighboring cheers are separated by the word "Team", so 1 cheer is printed as "Go", 2 cheers as "Go Team Go", 3 cheers are printed as "Go Team Go Team Go", and so on.
The lines you print should be displayed at increasing levels of indentation. The first line displayed should have no indentation, but each following line should be intended by 3 spaces more than the one before it. In other words, the 2nd line of output should be indented by 3 spaces, the 3rd line by 6 spaces, and so on.
You may assume that both parameters passed your method will have values of at least 1.
The following calls demonstrate your method's behavior. Your method should match this output format exactly:

Write a static method named cheerleader that accepts two integer parameters lines and cheers and prints a series of "cheer" lines at increasing levels of indentation. The first parameter represents the number of lines of output to print, and the second represents the number of "cheers" per line. For example, the call of cheerleader(2, 4) means that you should print 2 lines of output, each containing 4 "cheers." A "cheer" is an occurrence of the word "Go" in the output. Neighboring cheers are separated by the word "Team", so 1 cheer is printed as "Go", 2 cheers as "Go Team Go", 3 cheers are printed as "Go Team Go Team Go", and so on.
The lines you print should be displayed at increasing levels of indentation. The first line displayed should have no indentation, but each following line should be intended by 3 spaces more than the one before it. In other words, the 2nd line of output should be indented by 3 spaces, the 3rd line by 6 spaces, and so on.
You may assume that both parameters passed your method will have values of at least 1.
The following calls demonstrate your method's behavior. Your method should match this output format exactly:

Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck