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 {
// int version of max
public static int max(int a, int b) {
return (a > b) ? a : b;
}
// 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)); // expected: 2.0 (calls double version)
}
}
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 {
// int version
public static void print(int n) {
System.out.println("int:" + n);
}
// String version
public static void print(String s) {
System.out.println("str:" + 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);
int:42
str:hello
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)
Answer: C
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)
Answer: B
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
Answer: A
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
Answer: A
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.
Answer: D
Short answer
- Explain why
int sum(int a, int b)
anddouble sum(int a, int b)
cannot both exist.
Answer:
Both of these have the same parameter list. To overload, it would need different parameter lists. The return type here (int on the first and double on the second) does not count.
- In one sentence, distinguish parameters vs arguments.
Answer:
Parameters are the variables that are defined in a header for the method, while arguments are the actual values that the method takes in when it 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.
```Java
// QUESTION 1 and 2
public class OverloadTest {
public static void main(String[] args) {
// 1) abs overloads
System.out.println(abs(-10)); // int version → 10
System.out.println(abs(-5.5)); // double version → 5.5
System.out.println(abs(-100L)); // long version → 100
// 2) concat overloads
System.out.println(concat("Hello", "World")); // String + String → HelloWorld
System.out.println(concat("Ha", 4)); // String + int → HaHaHaHa
// 3) show overloads
show(7); // int → prints: int
show(7L); // long → prints: long
show(7.0); // double → prints: double
}
// abs overloads
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; }
// concat overloads
public static String concat(String a, String b) { return a + b; }
public static String concat(String a, int n) {
String result = "";
for (int i = 0; i < n; i++) result += a;
return result;
}
// show overloads
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"); }
}
OverloadTest.main(null);
10
5.5
100
HelloWorld
HaHaHaHa
int
long
double
QUESTION 3:
int // 7 is an int literal → calls show(int)
long // 7L is a long literal → calls show(long)
double // 7.0 is a double literal → calls show(double)
Java would choose the most specific matching overload based on the literal type.
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.
// FRQ 1
public class FRQ1 {
// Returns index of first occurrence of char in s, or -1 if not found
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;
}
// Returns index of first occurrence of substring in s, or -1 if not found
public static int indexOf(String target, String s) {
int tLen = target.length();
int sLen = s.length();
if (tLen == 0) return 0; // empty string found at index 0
for (int i = 0; i <= sLen - tLen; i++) {
boolean match = true;
for (int j = 0; j < tLen; j++) {
if (s.charAt(i + j) != target.charAt(j)) {
match = false;
break;
}
}
if (match) return i;
}
return -1;
}
// Test examples
public static void main(String[] args) {
System.out.println(indexOf('a', "banana")); // 1
System.out.println(indexOf('z', "banana")); // -1
System.out.println(indexOf("ana", "banana")); // 1
System.out.println(indexOf("xyz", "banana")); // -1
}
}
FRQ1.main(null);
1
-1
1
-1
// FRQ 2
public class FRQ2 {
// int clamp
public static int clamp(int value, int low, int high) {
if (low > high) { // swap
int temp = low;
low = high;
high = temp;
}
if (value < low) return low;
if (value > high) return high;
return value;
}
// double clamp
public static double clamp(double value, double low, double high) {
if (low > high) { // swap
double temp = low;
low = high;
high = temp;
}
if (value < low) return low;
if (value > high) return high;
return value;
}
// Test examples
public static void main(String[] args) {
System.out.println(clamp(5, 1, 10)); // 5
System.out.println(clamp(15, 1, 10)); // 10
System.out.println(clamp(-3, 1, 10)); // 1
System.out.println(clamp(7.5, 5.0, 7.0)); // swap → returns 7.0
}
}
FRQ2.main(null);
5
10
1
7.0
Submission checklist
- MCQs completed on the Google Form.
- This notebook executes top-to-bottom with outputs visible.
- Answers are clear and concise.