Skip to main content
United StatesComputer ScienceSyllabus dot point

How do you design a method with parameters and a return value, and how does it use the object's own data?

Topic 5.6 Writing Methods: design and implement methods that take parameters, perform a computation possibly using the object's instance variables, and return a result or perform an action.

A focused answer to AP CSA Topic 5.6, covering the method header (modifiers, return type, name, parameters), how a method uses instance variables and parameters, returning a value versus void, calling one method from another, and tracing a method, with a fully worked example.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The method header
  3. Parameters versus instance variables
  4. Returning a value versus void
  5. Calling other methods
  6. Try this

What this topic is asking

The College Board (Topic 5.6) wants you to design and implement methods that go beyond simple getters and setters: methods that take parameters, do a computation (often using the object's instance variables), and either return a result or perform an action. This is the heart of the free-response section, where you implement a specified method. You need to handle the method header, the body, the return value, and calling one method from another.

The method header

public double scale(double factor) {
//  ^modifier ^return  ^name  ^parameter
  return value * factor;
}

Parameters versus instance variables

A parameter is a local variable that receives a copy of the argument when the method is called; it exists only during that call. An instance variable belongs to the object and persists between calls. A method can use both: it reads its parameters for the inputs of this call, and reads or writes instance variables for the object's lasting state. When a parameter and an instance variable share a name, the parameter hides the field unless you use this (Topic 5.9).

Returning a value versus void

If any path through a non-void method can reach the end without returning, the code does not compile ("missing return statement"). Guard clauses must each return, and the final fall-through must return too.

Calling other methods

A method can call another method of the same object by name, without a dot:

public double areaPlusBorder() {
  return area() + perimeter();   // calls this object's other methods
}

This breaks a complex method into small, testable helpers and avoids repeating logic.

Try this

Q1. Name the four parts of a method header. [1 point]

  • Cue. Modifiers, return type, name, and parameter list.

Q2. Explain why a non-void method that returns a value only inside an if may fail to compile. [2 points]

  • Cue. A non-void method must return a value on every path; if the if is false and no return follows, a path reaches the end without returning, so the compiler reports a missing return statement.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. Consider this class. ```java public class Counter { private int value; public Counter(int v) { value = v; } public int addAndGet(int n) { value = value + n; return value; } } Counter c = new Counter(10); int result = c.addAndGet(5); System.out.println(result + " " + c.addAndGet(2)); ``` What is printed? (A) `15 17` (B) `15 15` (C) `10 12` (D) `17 17` (E) `5 2`
Show worked answer →

The answer is (A).

addAndGet adds its parameter to the instance variable value and returns the new value. c starts with value = 10. The first call addAndGet(5) makes value = 15 and returns 15, so result is 15. The second call addAndGet(2) makes value = 17 and returns 17. So the print is 15 17. (B) forgets the second call also changes value; (C) ignores the mutation; (E) returns the parameters.

Markers reward tracing that the method both changes the instance variable and returns the updated value, across two calls.

AP 2020 (style)4 marksFree response (code writing). A class `Grades` has a private `int` instance variable `total` and a private `int` instance variable `count`. Write a method `average` that returns the average mark as a `double` (total divided by count), and returns 0.0 when `count` is 0 to avoid dividing by zero.
Show worked answer →

A 4-point question testing a method using instance variables with a guard.

public double average() {
  if (count == 0) {
    return 0.0;
  }
  return (double) total / count;
}

Point 1: the method has return type double. Point 2: the guard if (count == 0) return 0.0; prevents division by zero. Point 3: the computation uses the instance variables total and count. Point 4: casting total to double so the division is real, not integer division. Omitting the cast would truncate the average; omitting the guard risks an ArithmeticException.

Related dot points

Sources & how we know this