Method Signatures — Homework (1.9)
AP CSA 1.9 — Method Signatures: Homework
Complete all parts. Submit this executed notebook on GitHub Pages and answer MCQs on the Google Form.
Popcorn Hack: Write and test max overloads
Instructions: Write two overloaded methods named max that each return the larger of two numbers.
One should accept two int values.
The other should accept two double values.
Then, test both versions in the main method.
public class PopcornMax {
// TODO: write int version of max
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
// TODO: write double version of max
public static double max(double a, double b) {
return (a >= b) ? a : b;
}
public static void main(String[] args) {
System.out.println(max(3, 9)); // expected: 9
System.out.println(max(-2, -7)); // expected: -2
System.out.println(max(3.5, 2.9)); // expected: 3.5
System.out.println(max(2, 2.0)); // should call double version → 2.0
}
}
PopcornMax.main(null)
9
-2
3.5
2.0
Popcorn Hack 1 Answer:
public class PopcornMax {
// int overload
public static int max(int a, int b) {
return a >= b ? a : b;
}
// double overload
public static double max(double a, double b) {
return a >= b ? a : b;
}
public static void main(String[] args) {
System.out.println(max(3, 9)); // 9
System.out.println(max(-2, -7)); // -2
System.out.println(max(3.5, 2.9)); // 3.5
System.out.println(max(2, 2.0)); // 2.0 (int 2 promoted to double → calls max(double,double))
}
}
Popcorn Hack: Overload print for int and String
Instructions: Implement two overloaded print methods — one that accepts an int and prints int:. Test with mixed types.
public class PopcornPrint {
// TODO: write print(int n)
public static void print(int n) {
System.out.println("Integer:" + n);
}
// TODO: write print(String s)
public static void print(String s) {
System.out.println("String:" + s);
}
public static void main(String[] args) {
print(42); // expected: int:42
print("hello"); // expected: str:hello
print('A' + "!"); // char + String → String → expected: str:A!
}
}
PopcornPrint.main(null)
Integer:42
String:hello
String:A!
Popcorn Hack 2 Answer:
public class PopcornPrint {
static void print(int n) {
System.out.println("int:" + n);
}
static void print(String s) {
System.out.println("str:" + s);
}
public static void main(String[] args) {
print(42); // int:42
print("hello"); // str:hello
print('A' + "!"); // str:A!
}
}
MCQs
1) Which pair is a valid overload?
- A)
int f(int a)
andint f(int x)
- B)
int f(int a)
anddouble f(int a)
- C)
int f(int a)
anddouble f(double a)
- D)
int f(int a, int b)
andint f(int a, int b)
2) Which is the AP CSA notion of a method signature?
- A)
public static int max(int a, int b)
- B)
max(int, int)
- C)
max(int a, int b) throws Exception
- D)
max(int, double)
3) With the overloads void p(int x)
and void p(double x)
, what prints?
p(5);
p(5.0);
- A)
int
,double
- B)
double
,double
- C)
int
,int
- D) Compile error
4) Which call is ambiguous with only these two methods?
void h(long x) {}
void h(float x) {}
- A)
h(5)
- B)
h(5.0)
- C)
h(5f)
- D) None are ambiguous
5) Which statement is TRUE?
- A) Parameter names affect the method signature.
- B) Return type affects the method signature.
- C) Access modifiers affect the method signature.
- D) Overloading requires different parameter lists.
Short answer
- Explain why
int sum(int a, int b)
anddouble sum(int a, int b)
cannot both exist. - In one sentence, distinguish parameters vs arguments.
int sum(int a, int b)
anddouble sum(int a, int b)
cannot both exist because this would overload the Java methods by return type. Since both have the same parameter list (int a, int b
), the compiler would not be able to determine which one to call so the overall program would fail.- Parameters are placeholders that are defined (similarly to variables), and arguments are the values passed into these parameters when the method is called.
Coding Tasks (write Java in code blocks; pseudo-Java acceptable)
1) Write three overloads of abs
: int abs(int x)
, double abs(double x)
, and long abs(long x)
.
2) Implement two overloads of concat
:
String concat(String a, String b)
returns concatenation.String concat(String a, int n)
returnsa
repeatedn
times. 3) Determine output: ```java static void show(int x) { System.out.println(“int”); } static void show(double x) { System.out.println(“double”); } static void show(long x) { System.out.println(“long”); }
show(7); show(7L); show(7.0);
Write the expected output and a one-line explanation for each.
### Homework Answer:
1.
```java
public static int abs(int x) {
return (x >= 0) ? x : -x;
}
public static double abs(double x) {
return (x >= 0) ? x : -x;
}
public static long abs(long x) {
return (x >= 0) ? x : -x;
}
public static String concat(String a, String b) {
return a + b;
}
public static String concat(String a, int n) {
if (n <= 0) return "";
StringBuilder sb = new StringBuilder(a.length() * n);
for (int i = 0; i < n; i++) sb.append(a);
return sb.toString();
}
- First output would be “int” because 7 is an integer, not a double. Second output would be “long” because 7L is a long literal value. Third output would be “double” because 7.0 has a decimal place, indicating that it is a double.
FRQ-style
- FRQ 1.
indexOf(char target, String s)
: return the index of firsttarget
ins
or-1
if not found. Overload withindexOf(String target, String s)
for first substring occurrence (without using library indexOf). State assumptions and constraints. - FRQ 2. Overload
clamp
:int clamp(int value, int low, int high)
returnsvalue
confined to[low, high]
.double clamp(double value, double low, double high)
similarly for doubles. Handle the caselow > high
by swapping.
Homework Submission
Question 1:
public class QuestionOne {
public static int indexOf(char target, String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) return i;
}
return -1;
}
public static int indexOf(String target, String s) {
if (target.length() == 0) return 0;
if (target.length() > s.length()) return -1;
for (int i = 0; i <= s.length() - target.length(); i++) {
int j = 0;
while (j < target.length() && s.charAt(i + j) == target.charAt(j)) j++;
if (j == target.length()) return i;
}
return -1;
}
}
Question 2:
```java
public class QuestionTwo { public static int clamp(int value, int low, int high) { if (low > high) { int t = low; low = high; high = t; } if (value < low) return low; if (value > high) return high; return value; }
public static double clamp(double value, double low, double high) {
if (low > high) { double t = low; low = high; high = t; }
if (value < low) return low;
if (value > high) return high;
return value;
}
public static void main(String[] args) {
System.out.println(indexOf('a', "banana")); // 1
System.out.println(indexOf("ana", "banana")); // 1
System.out.println(indexOf("xyz", "banana")); // -1
System.out.println(clamp(7, 0, 5)); // 5
System.out.println(clamp(3, 0, 5)); // 3
System.out.println(clamp(3, 5, 0)); // 3
System.out.println(clamp(7.5, 0.0, 5.0)); // 5.0
} }
Submission checklist
- MCQs completed on the Google Form.
- This notebook executes top-to-bottom with outputs visible.
- Answers are clear and concise.