Prerequisites

This set of exercises is intended to be completed after the Primitive types vs references exercises. The exercises below build on the concepts practiced in the earlier set. Specifically, the exercises below pass values when calling methods.

This set of exercises assumes familiarity with Methods with parameters.

Coordinate2D class

The exercises on this page use the Coordinate2D class. The previous set of exercises has additional information about the Coordinate2D class.

A link to the complete class is below.

Coordinate2D.java

For each exercise, give the output of the code segment.

Primitive exercise with method call

public static void main(String[] args)
{
    int a = 10;
    primitive(a);
    System.out.println(a);
}

private static void primitive(int b)
{
    b = 15;
}

Primitive exercise with call solution & explanation

Reference exercise 1 with method call

public static void main(String[] args)
{
    Coordinate2D c = new Coordinate2D(1,1);
    reference1(c);
    System.out.println(c);
}

private static void reference1(Coordinate2D d)
{
    d.setX(2);
}

Reference exercise 1 with call solution & explanation

Reference exercise 2 with method call

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    reference2(e);
    System.out.println(e);
}

private static void reference2(Coordinate2D f)
{
    f = new Coordinate2D(2,2);
}

Reference exercise 2 with call solution & explanation

Additional classes & objects resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Primitive types vs references with method calls