Unit 3 Cycle 1 Day 6: Writing Methods with Parameters
Share
Writing Methods with Parameters
Section 3.6 — Writing Methods
Key Concept
Methods can accept parameters of any type, including objects of the same class or different classes. When an object is passed as a parameter, the method receives a copy of the reference, meaning it can modify the object's state through the reference. The return type of a method determines what the caller receives back: a primitive value, an object reference, or void for no return. On the AP exam, methods with parameters and return values are tested through tracing exercises that require tracking both the method's local state and the caller's variables.
Consider the following class.
What is printed after executing: Rectangle r = new Rectangle(3, 4); r.scale(2); System.out.println(r.area());
Answer: (C) 48
Initial: width=3, height=4. scale(2): width=6, height=8. area(): 6*8=48.
Why Not the Others?
(A) 12 is the original area (3*4) before scaling.
(B) 24 would be the area if only one dimension were scaled (e.g., 6*4 or 3*8).
(D) 6 would be width after scaling, not the area.
Common Mistake
Scaling both dimensions by factor 2 multiplies the area by 4 (2*2), not 2. If width and height both double, area quadruples: 12 * 4 = 48.
AP Exam Tip
When a mutator changes multiple fields, trace ALL field changes before computing the result. Both width and height are modified by scale().