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.
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)
My 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)
My 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
My 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
My 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.
My Answer: D
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.
My Answers:
Both of these sum methods cannot exist because they have the same method signature and therefore Java cannot differentiate between them when one of them is called.
Parameters are essentially variables that the method requires / asks for and arguments are the variables that are given to it in response.
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.
```java
public class codingTasks {
static int abs(int x) {
if (x > 0) {
return x;
} else {
return -x;
}
}
static double abs(double x) {
if (x > 0) {
return x;
} else {
return -x;
}
}
static long abs(long x) {
if (x > 0) {
return x;
} else {
return -x;
}
}
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"); }
public static void main(String[] args) {
show(7); // prints "int"
show(7L); // prints "long"
show(7.0); // prints "double"
}
}
codingTasks.main(new String[]{})
int
long
double
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.
public class FRQTasks {
static int indexOf(char target, String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) return i;
}
return -1;
}
static int indexOf(String target, String s) {
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;
}
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;
}
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"));
System.out.println(indexOf("ana", "banana"));
System.out.println(clamp(15, 10, 20));
System.out.println(clamp(25.5, 10.0, 20.0));
System.out.println(clamp(5, 20, 10));
}
}
FRQTasks.main(new String[]{})
1
1
15
20.0
10
Submission checklist
- MCQs completed on the Google Form.
- This notebook executes top-to-bottom with outputs visible.
- Answers are clear and concise.