Deck 3: Introduction to Parameters and Objects
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 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);
}
}
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
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).
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).
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:
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:
(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;
}
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);
}
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);
}
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);
}
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);
}
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;
}
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;
}
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:
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:
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.
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