Deck 3: Introduction to Parameters and Objects

Full screen (f)
exit full mode
Question
Parameter Mystery
At the bottom of the page, write the output produced by the following program.
public class ParameterMystery {
public static void main(String[] args) {
String a = "felt";
String b = "saw";
String c = "drew";
String saw = "sue";
String drew = "b";
mystery(a, b, c);
mystery(b, a, saw);
mystery(drew, c, saw);
mystery("a", saw, drew);
mystery(a, a, "drew");
}
public static void mystery(String b, String a, String c) {
System.out.println(c + " " + a + " the " + b);
}
}
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).
 Expression  Value 1+2345      5/2+9.0/2.021.25      29%2%5+34%3      8+62+4+"0 " +(2+5)      31/2/10.0+10/(5/2.0)      (1!=2)!=(2!=3)      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\1+2 * 3-4 * 5 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5 / 2+9.0 / 2.0-2 * 1.25 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\29\% 2\% 5+ 34 \%3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\8+6 *-2+4+" 0 \text { " }+(2+5) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\31 / 2 / 10.0+10 /(5 / 2.0) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\(1 !=2) !=(2 !=3) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\end{array}
Question
Programming
Write a static method named enoughTimeForLunch that accepts four integers hour1, minute1, hour2, and minute2 as parameters. Each pair of parameters represents a time on the 24-hour clock (for example, 1:36 PM would be represented as 13 and 36). The method should return true if the gap between the two times is long enough to eat lunch: that is, if the second time is at least 45 minutes after the first time. Otherwise the method should return false.
You may assume that all parameter values are valid: the hours are both between 0 and 23, and the minute parameters are between 0 and 59. You may also assume that both times represent times in the same day, e.g. the first time won't represent a time today while the second time represents a time tomorrow. Note that the second time might be earlier than the first time; in such a case, your method should return false.
Here are some example calls to your method and their expected return results:
 Call  Value Returned  enoughTimeForLunch (11,00,11,59) true  enoughTimeForLunch (12,30,13,00) false  enoughTimeForIunch (12,30,13,15) true  enoughTimeForLunch (14,20,17,02) true  enoughTimeForLunch (12,30,9,30) false  enoughTimeForLunch (12,00,11,55) false \begin{array}{l|l}\text { Call } & \text { Value Returned } \\\hline \text { enoughTimeForLunch }(11, \quad 00,11,59) & \text { true } \\\text { enoughTimeForLunch }(12, \quad 30,13,00) & \text { false } \\\text { enoughTimeForIunch }(12,\quad30,13,15) & \text { true } \\\text { enoughTimeForLunch }(14, \quad 20,17,02) & \text { true } \\\text { enoughTimeForLunch }(12, \quad 30,9,30) & \text { false } \\\text { enoughTimeForLunch }(12, \quad 00,11,55) & \text { false }\end{array}
Question
If/Else Simulation
For each call of the method below, write the output that is produced:
public static void mystery(int x, int y) {
if (x > y) {
x = x - 5;
y = y + 5;
}
if (x < y) {
x++;
y--;
} else {
x = y * 2;
}
System.out.println(x + " " + y);
}
 Method Call Output  mystery (4,7);       mystery (3,3);       mystery (10,5);       mystery (20,4);       mystery (1,1);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text {Output }} \\\text { mystery }(4,7) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(3,3) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(10,5) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(20,4) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(1,1) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \end{array}
Question
While Loop Simulation
For each call of the method below, write the output that is produced:
public static void mystery(int a, int b) {
while (b != 0) {
if (a > b) {
System.out.print(a + " ");
a = a - b;
} else {
System.out.print(b + " ");
b = b - a;
}
}
System.out.println(a);
}
 Method Call Output  mystery (42,0);       mystery (6,12);       mystery (18,27);       mystery (24,60);       mystery (50,15);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text {Output }} \\\text { mystery }(42,0) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(6,12) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(18,27) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(24,60) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(50,15) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \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. You may abbreviate these choices as A/N/S respectively.
public static int assertions(int n) {
int x = 2;
// Point A
while (x < n) {
// Point B
if (n % x == 0) {
n = n / x;
x = 2;
// Point C
} else {
x++;
// Point D
}
}
// Point E
return n;
}
x>2x<nn%×0==0 Point A Point B Point C  Point D  Point E \begin{array}{|c|c|c|c|} \hline& x>2 & x<\mathrm{n} & \mathrm{n} \% \times 0==0 \\\hline \text { Point } A & & & \\\hline \text { Point } \mathrm{B} & & & \\\hline \text { Point C } & & & \\\hline \text { Point D } & & & \\\hline \text { Point E } & & & \\\hline\end{array}
Question
Programming
Write a static method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached.
Assume that rows and cols are greater than 0. Here are some example calls to your method and their expected results:
 Call  printGrid (3,6);pr intGrid (5,3);printGrid(4,1);printGrid(1,3); Output 1,4,7,10,13,161,6,1111,2,32,5,8,11,14,172,7,1223,6,9,12,15,183,8,1334,9,1445,10,15\begin{array}{l|l|l|l|l}\text { Call } & \text { printGrid }(3,6) ; & \operatorname{pr} \text { intGrid }(5,3) ; & \operatorname{printGrid}(4,1) ; & \operatorname{printGrid}(1,3) ; \\\hline \text { Output } & 1,4,7,10,13,16 & 1,6,11 &1 &1,2,3 \\& 2,5,8,11,14,17 & 2,7,12 &2& \\& 3,6,9,12,15,18 & 3,8,13 & 3 \\& & 4,9,14 & 4 \\& & 5,10,15 & \end{array}
Question
Programming (10 points)
Write a static method named countEvenDigits that accepts an integer as its parameter and returns the number of even-valued digits in that number. An even-valued digit is either 0, 2, 4, 6, or 8.
For example, the number 8546587 has four even digits (the two 8s, the 4, and the 6),
so the call countEvenDigits(8346387) should return 4.
You may assume that the value passed to your method is non-negative.
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 3: Introduction to Parameters and Objects
1
Parameter Mystery
At the bottom of the page, write the output produced by the following program.
public class ParameterMystery {
public static void main(String[] args) {
String a = "felt";
String b = "saw";
String c = "drew";
String saw = "sue";
String drew = "b";
mystery(a, b, c);
mystery(b, a, saw);
mystery(drew, c, saw);
mystery("a", saw, drew);
mystery(a, a, "drew");
}
public static void mystery(String b, String a, String c) {
System.out.println(c + " " + a + " the " + b);
}
}
drew saw the felt
sue felt the saw
sue drew the b
b sue the a
drew felt the felt
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).
 Expression  Value 1+2345      5/2+9.0/2.021.25      29%2%5+34%3      8+62+4+"0 " +(2+5)      31/2/10.0+10/(5/2.0)      (1!=2)!=(2!=3)      \begin{array}{l}\underline{ \text { Expression } }& \underline{ \text { Value }} \\1+2 * 3-4 * 5 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\5 / 2+9.0 / 2.0-2 * 1.25 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\29\% 2\% 5+ 34 \%3 & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\8+6 *-2+4+" 0 \text { " }+(2+5) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\31 / 2 / 10.0+10 /(5 / 2.0) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\(1 !=2) !=(2 !=3) & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\end{array}
 Expression  Value 1+2345135/2+9.0/2.021.254.029%2%5+34%328+62+4+"0 " +(2+5)"007"31/2/10.0+10/(5/2.0)5.5(1!=2)!=(2!=3)false\begin{array}{l|c}\underline{ \text { Expression } }& \underline{ \text { Value }} \\1+2 * 3-4 * 5 & -13 \\5 / 2+9.0 / 2.0-2 * 1.25 & 4.0\\29\% 2\% 5+ 34 \%3 & 2\\8+6 *-2+4+" 0 \text { " }+(2+5) & "007" \\31 / 2 / 10.0+10 /(5 / 2.0) & 5.5 \\(1 !=2) !=(2 !=3) & false\end{array}
3
Programming
Write a static method named enoughTimeForLunch that accepts four integers hour1, minute1, hour2, and minute2 as parameters. Each pair of parameters represents a time on the 24-hour clock (for example, 1:36 PM would be represented as 13 and 36). The method should return true if the gap between the two times is long enough to eat lunch: that is, if the second time is at least 45 minutes after the first time. Otherwise the method should return false.
You may assume that all parameter values are valid: the hours are both between 0 and 23, and the minute parameters are between 0 and 59. You may also assume that both times represent times in the same day, e.g. the first time won't represent a time today while the second time represents a time tomorrow. Note that the second time might be earlier than the first time; in such a case, your method should return false.
Here are some example calls to your method and their expected return results:
 Call  Value Returned  enoughTimeForLunch (11,00,11,59) true  enoughTimeForLunch (12,30,13,00) false  enoughTimeForIunch (12,30,13,15) true  enoughTimeForLunch (14,20,17,02) true  enoughTimeForLunch (12,30,9,30) false  enoughTimeForLunch (12,00,11,55) false \begin{array}{l|l}\text { Call } & \text { Value Returned } \\\hline \text { enoughTimeForLunch }(11, \quad 00,11,59) & \text { true } \\\text { enoughTimeForLunch }(12, \quad 30,13,00) & \text { false } \\\text { enoughTimeForIunch }(12,\quad30,13,15) & \text { true } \\\text { enoughTimeForLunch }(14, \quad 20,17,02) & \text { true } \\\text { enoughTimeForLunch }(12, \quad 30,9,30) & \text { false } \\\text { enoughTimeForLunch }(12, \quad 00,11,55) & \text { false }\end{array}
(five solutions shown)
public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) {
if (h1 > h2) {
return false;
} else if (h1 == h2) {
return m2 - m1 >= 45;
} else if (h1 == h2 - 1) {
return 60 + m2 - m1 >= 45;
} else {
return true;
}
}
public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) {
if (h2 > h1 + 1) {
return true;
} else if (h2 == h1 && m1 + 45 <= m2) {
return true;
} else if (h2 == h1 + 1 && m1 - 15 <= m2) {
return true;
} else {
return false;
}
}
public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) {
if (h1 > h2) {
return false;
} else if (h1 == h2) { // same hour
if (m1 + 45 <= m2) { // must be >= 45 min apart
return true;
} else {
return false;
}
} else if (h2 == h1 + 1) { // h1 is just before h2
if (m1 - 15 <= m2) { // must be >= -15 min apart
return true;
} else {
return false;
}
} else { // time 1 is > 1 hour before time 2
return true;
}
}
public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) {
if ((h1 == h2 && m1 + 45 <= m2) ||
(h2 == h1 + 1 && m1 - 15 <= m2) || (h1 < h2 - 1)) {
return true;
} else {
return false;
}
}
public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) {
return 60 * h1 + m1 + 45 <= 60 * h2 + m2;
}
4
If/Else Simulation
For each call of the method below, write the output that is produced:
public static void mystery(int x, int y) {
if (x > y) {
x = x - 5;
y = y + 5;
}
if (x < y) {
x++;
y--;
} else {
x = y * 2;
}
System.out.println(x + " " + y);
}
 Method Call Output  mystery (4,7);       mystery (3,3);       mystery (10,5);       mystery (20,4);       mystery (1,1);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text {Output }} \\\text { mystery }(4,7) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(3,3) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(10,5) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(20,4) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(1,1) ; & \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
While Loop Simulation
For each call of the method below, write the output that is produced:
public static void mystery(int a, int b) {
while (b != 0) {
if (a > b) {
System.out.print(a + " ");
a = a - b;
} else {
System.out.print(b + " ");
b = b - a;
}
}
System.out.println(a);
}
 Method Call Output  mystery (42,0);       mystery (6,12);       mystery (18,27);       mystery (24,60);       mystery (50,15);      \begin{array}{l}\underline{ \text { Method Call } }& \underline{ \text {Output }} \\\text { mystery }(42,0) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(6,12) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(18,27) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }}\\\text { mystery }(24,60) ; & \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \underline{ \text { }} \\\text { mystery }(50,15) ; & \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
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. You may abbreviate these choices as A/N/S respectively.
public static int assertions(int n) {
int x = 2;
// Point A
while (x < n) {
// Point B
if (n % x == 0) {
n = n / x;
x = 2;
// Point C
} else {
x++;
// Point D
}
}
// Point E
return n;
}
x>2x<nn%×0==0 Point A Point B Point C  Point D  Point E \begin{array}{|c|c|c|c|} \hline& x>2 & x<\mathrm{n} & \mathrm{n} \% \times 0==0 \\\hline \text { Point } A & & & \\\hline \text { Point } \mathrm{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 printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached.
Assume that rows and cols are greater than 0. Here are some example calls to your method and their expected results:
 Call  printGrid (3,6);pr intGrid (5,3);printGrid(4,1);printGrid(1,3); Output 1,4,7,10,13,161,6,1111,2,32,5,8,11,14,172,7,1223,6,9,12,15,183,8,1334,9,1445,10,15\begin{array}{l|l|l|l|l}\text { Call } & \text { printGrid }(3,6) ; & \operatorname{pr} \text { intGrid }(5,3) ; & \operatorname{printGrid}(4,1) ; & \operatorname{printGrid}(1,3) ; \\\hline \text { Output } & 1,4,7,10,13,16 & 1,6,11 &1 &1,2,3 \\& 2,5,8,11,14,17 & 2,7,12 &2& \\& 3,6,9,12,15,18 & 3,8,13 & 3 \\& & 4,9,14 & 4 \\& & 5,10,15 & \end{array}
Unlock Deck
Unlock for access to all 8 flashcards in this deck.
Unlock Deck
k this deck
8
Programming (10 points)
Write a static method named countEvenDigits that accepts an integer as its parameter and returns the number of even-valued digits in that number. An even-valued digit is either 0, 2, 4, 6, or 8.
For example, the number 8546587 has four even digits (the two 8s, the 4, and the 6),
so the call countEvenDigits(8346387) should return 4.
You may assume that the value passed to your method is non-negative.
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.