Writing methods

java
public returnType methodName(parameterList) {
    // body
}

Void methods vs. return methods

Void methods — perform an action

java
public class Dog {
    private String name;
    private int tricks;
    
    // Void method — does something, returns nothing
    public void bark() {
        System.out.println(name + " says Woof!");
    }
    
    // Void method with parameter
    public void learnTrick(String trick) {
        tricks++;
        System.out.println(name + " learned " + trick + "!");
    }
}

Return methods — compute and return a value

java
public class Circle {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    // Returns the area
    public double getArea() {
        return Math.PI * radius * radius;
    }
    
    // Returns the circumference
    public double getCircumference() {
        return 2 * Math.PI * radius;
    }
    
    // Returns whether this circle is bigger than another
    public boolean isBiggerThan(Circle other) {
        return this.radius > other.radius;
    }
}

Using return methods

java
Circle c = new Circle(5.0);
double area = c.getArea();           // 78.5398...
boolean big = c.isBiggerThan(new Circle(3.0));  // true

Methods that use instance variables

java
public class Counter {
    private int count;
    
    public Counter() {
        count = 0;
    }
    
    public void increment() {
        count++;              // modifies instance variable
    }
    
    public void reset() {
        count = 0;            // modifies instance variable
    }
    
    public int getCount() {
        return count;         // reads instance variable
    }
    
    public boolean isZero() {
        return count == 0;    // reads instance variable
    }
}

Methods with parameters

java
public class Calculator {
    private double result;
    
    public Calculator() {
        result = 0;
    }
    
    // One parameter
    public void add(double value) {
        result += value;
    }
    
    // Two parameters
    public void addProduct(double a, double b) {
        result += a * b;
    }
    
    public double getResult() {
        return result;
    }
}

Calculator calc = new Calculator();
calc.add(10);
calc.addProduct(3, 4);   // adds 12
System.out.println(calc.getResult());  // 22.0

Methods calling other methods

java
public class Rectangle {
    private double width;
    private double height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    public double getArea() {
        return width * height;
    }
    
    public double getPerimeter() {
        return 2 * (width + height);
    }
    
    // This method calls getArea()
    public boolean isLargerThan(double minArea) {
        return getArea() > minArea;   // calls getArea() on THIS object
    }
    
    // This method calls getArea() on two objects
    public boolean isLargerThan(Rectangle other) {
        return this.getArea() > other.getArea();
    }
}

Objects as parameters

java
public class Point {
    private int x;
    private int y;
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    // Receives another Point object
    public double distanceTo(Point other) {
        int dx = this.x - other.x;
        int dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
}

Point p1 = new Point(0, 0);
Point p2 = new Point(3, 4);
System.out.println(p1.distanceTo(p2));  // 5.0

toString() method

java
public class Student {
    private String name;
    private double gpa;
    
    public Student(String name, double gpa) {
        this.name = name;
        this.gpa = gpa;
    }
    
    public String toString() {
        return name + " (GPA: " + gpa + ")";
    }
}

Student s = new Student("Alice", 3.8);
System.out.println(s);              // "Alice (GPA: 3.8)"
System.out.println("Student: " + s); // "Student: Alice (GPA: 3.8)"

AP Exam Tips

Common Mistakes

Key Vocabulary