Unit 3 Cycle 1 Day 6: Writing Methods with Parameters

Unit 3 Foundation (Cycle 1) Day 6 of 28 Foundation

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.

public class Rectangle { private int width; private int height; public Rectangle(int w, int h) { width = w; height = h; } public int area() { return width * height; } public void scale(int factor) { width *= factor; height *= factor; } }

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().

Review this topic: Section 3.6 — Writing Methods • Unit 3 Study Guide

More Practice

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.