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
// TODO: write double version of max
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
}
}
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)
// TODO: write print(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!
}
}
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.
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)returnsarepeatedntimes. 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.
FRQ-style
- FRQ 1.
indexOf(char target, String s): return the index of firsttargetinsor-1if 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)returnsvalueconfined to[low, high].double clamp(double value, double low, double high)similarly for doubles. Handle the caselow > highby swapping.
Submission checklist
- MCQs completed on the Google Form.
- This notebook executes top-to-bottom with outputs visible.
- Answers are clear and concise.