Unit 3 · Lesson 3.5 · 5 minutes

A Java method defines a behavior inside a class. In five minutes, you will read a method header one part at a time, write methods with and without return values, and call them with the correct arguments.

Learning Targets

✓ Identify the access modifier, return type, method name, and parameters in a method header
✓ Write a void method and a value-returning method
✓ Call a method with arguments that match its parameters

Minute 1 — Read a Method Header

Method headers put several pieces of information on one line. If that feels dense, begin with one question: does the calling code need a value returned? That determines whether the return type is a data type such as int or the keyword void. Then read the rest from left to right.

public int calculateArea(int length, int width)
Part Purpose Example
Access modifier Controls where the method can be called public
Return type Type of value sent back; use void for no value int
Method name Describes the action calculateArea
Parameters Typed inputs available inside the method int length, int width

The first line is the method header, and the braces contain its body. In Java, the method signature specifically means the method name and parameter types. If the declared return type is not void, every possible path through the method must execute a return statement with a compatible value.

Minutes 2–3 — Write Two Methods

Run the code cell. Read the two methods before reading main: printDimensions() prints but does not return a value, so it uses void. calculateArea() computes an integer, so it declares int and returns one.

Code Runner Challenge

Run the example, then locate each method's return type, name, parameters, and return statement.

View IPYNB Source
// CODE_RUNNER: Run the example, then locate each method's return type, name, parameters, and return statement.
public class Shape {
    // void means this method does not return a value.
    public void printDimensions(int length, int width) {
        System.out.println("Dimensions: " + length + " by " + width);
    }

    // The two int parameters make this method reusable with different inputs.
    public int calculateArea(int shapeLength, int shapeWidth) {
        return shapeLength * shapeWidth;
    }

    public static void main(String[] args) {
        Shape rectangle = new Shape();
        rectangle.printDimensions(8, 5);

        int area = rectangle.calculateArea(8, 5);
        System.out.println("Area: " + area);
    }
}
Shape.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Minutes 4–5 — Call and Check

The call rectangle.calculateArea(8, 5) uses two arguments. Java assigns them to shapeLength and shapeWidth in order. The returned 40 is stored in area.

Quick check: why would this method fail to compile?
public int doubleNumber(int number) {
    System.out.println(number * 2);
}

Its declared return type is int, but the method never returns an integer. Fix it with return number * 2;, or change the return type to void if the method should only print.

Five-minute recap: choose a return type, give the method a name that describes its behavior, declare typed parameters, write one focused behavior, and use return when the declared type requires a value.

Homework Hacks

Three hacks · 5 minutes each

Complete each starter code cell. Keep the given class and method names so the test calls continue to work.

Homework Hack 1 — Complete a Returning Method (5 minutes)

header practice Complete perimeter. It must accept length and width, then return 2 * length + 2 * width. The expected output is 26.

Code Runner Challenge

Complete the perimeter method, then run the provided test.

View IPYNB Source
// CODE_RUNNER: Complete the perimeter method, then run the provided test.
public class PerimeterHack {
    // TODO: add the return type, parameters, and method body.
    public _____ perimeter(_____) {
        // your code here
    }

    public static void main(String[] args) {
        PerimeterHack hack = new PerimeterHack();
        System.out.println(hack.perimeter(8, 5)); // expected: 26
    }
}
PerimeterHack.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack 2 — Write a void Method (5 minutes)

parameter practice Write printRectangle with two int parameters. It should print Rectangle: 8 by 5. Call it from main.

Code Runner Challenge

Write and call a void method that prints the required output.

View IPYNB Source
// CODE_RUNNER: Write and call a void method that prints the required output.
public class DescriptionHack {
    // TODO: write printRectangle here.


    public static void main(String[] args) {
        DescriptionHack hack = new DescriptionHack();
        // TODO: call printRectangle on hack with 8 and 5.
    }
}
DescriptionHack.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack 3 — Return a Decision (5 minutes)

boolean practice Write isSquare. It accepts length and width and returns true when they are equal. Both provided checks should print true.

Code Runner Challenge

Write a boolean method, then run both provided checks.

View IPYNB Source
// CODE_RUNNER: Write a boolean method, then run both provided checks.
public class SquareHack {
    // TODO: write isSquare here.


    public static void main(String[] args) {
        SquareHack hack = new SquareHack();
        System.out.println(hack.isSquare(4, 4));  // expected: true
        System.out.println(!hack.isSquare(8, 5)); // expected: true
    }
}
SquareHack.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Submission Checklist and Grading

Each homework hack is worth 1 point.

  • Hack 1: correct int return type, two int parameters, and perimeter formula
  • Hack 2: correct void method, two int parameters, exact output, and a working method call
  • Hack 3: correct boolean return type, two int parameters, and equality check

Before submitting, run every code cell and confirm its output matches the comments. Do not rename the supplied classes or methods.

References

  • College Board. (2025). AP Computer Science A course and exam description.
  • Oracle. (n.d.). Defining methods. The Java Tutorials. https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Submit Assignment

Click to upload or drag and drop
PDF, ZIP, images, documents, or Jupyter notebooks (.ipynb) (Max 10MB per file)

Need to update a submission later? Open the submissions dashboard.