Java Arena
Step 4 of 5

A class of your own

A class bundles values that belong together with the methods that work on them. Most classes in this course have had the same parts: private instance variables, a constructor, getters, methods that change the object, and a toString. Some also have an equals.

The tools

Tool What it does Taught in
public class Plank in the file Plank.java, with private instance variables a class of your own module 17, Classes and objects
a constructor, with this.length = length; sets up a new object module 17, Classes and objects
a method that changes an instance variable changes the object module 17, Classes and objects
getters such as getLength(), and toString() give values out, and the text println shows module 18, Getters, toString and bigger classes
== on two objects are they the very same object? module 22, Primitive and reference variables
public boolean equals(Object compared) what "equal" means for this class module 23, Objects working together

A worked example

At a climbing gym, each wall has one route of each color, so a route is known by its wall and the color of its holds. It also has a grade, and the gym counts its ascents: how many times someone has climbed it to the top.

Main.java
public class Main {
    public static void main(String[] args) {
        ClimbingRoute route = new ClimbingRoute("north wall", "red", 5);
        route.climb();
        route.climb();
        System.out.println(route);

        ClimbingRoute written = new ClimbingRoute("north wall", "red", 5);
        System.out.println(route == written);
        System.out.println(route.equals(written));
        System.out.println(route.equals(new ClimbingRoute("north wall", "blue", 5)));
    }
}
ClimbingRoute.java
public class ClimbingRoute {
    private String wall;
    private String color;
    private int grade;
    private int ascents;

    public ClimbingRoute(String wall, String color, int grade) {
        this.wall = wall;
        this.color = color;
        this.grade = grade;
        this.ascents = 0;
    }

    public String getWall() {
        return this.wall;
    }

    public String getColor() {
        return this.color;
    }

    public int getGrade() {
        return this.grade;
    }

    public int getAscents() {
        return this.ascents;
    }

    public void climb() {
        this.ascents++;
    }

    public boolean equals(Object compared) {
        if (this == compared) {
            return true;
        }
        if (!(compared instanceof ClimbingRoute)) {
            return false;
        }
        ClimbingRoute other = (ClimbingRoute) compared;
        return this.wall.equals(other.wall) && this.color.equals(other.color);
    }

    public String toString() {
        return this.color + " route on the " + this.wall + ", grade " + this.grade + ", ascents: " + this.ascents;
    }
}
Output
red route on the north wall, grade 5, ascents: 2
false
true
false
  • The constructor's parameters have the same names as the instance variables. this.wall is the object's variable, and wall alone is the parameter (module 18).
  • climb() returns nothing: it changes the object's state. The getters only hand values out.
  • route == written is false: two news make two objects, whatever they hold. equals compares what's in them, in the way the class decides.
  • The equals is module 23's, step by step: the exact header public boolean equals(Object compared), the check for the very same object, the instanceof check, the cast, and then the comparison. Strings are compared with equals, and numbers with ==.
  • This equals compares only the wall and the color, the values that tell routes apart. The ascents change every day, and they don't make it another route.
  • The list methods contains, indexOf and remove(value) use equals too (module 23), so a list of routes can find a route by its wall and color.

Module 32 comes back to equals, and adds its partner, hashCode.

Your task

Challenge 1 of 3

A woodworking class keeps its planks in a rack. In Plank.java, write the class Plank, with private instance variables for the wood and the length:

  • a constructor Plank(String wood, int length): the kind of wood, such as oak, and the length in centimeters,
  • the getters getWood() and getLength(),
  • a method cut(int piece) that saws a piece of that many centimeters off the plank, so the plank gets shorter by that much. If the piece is longer than the plank, nothing happens.
  • toString(), which returns the wood and the length, like oak, 120 cm.

Main.java is a test drive. Until Plank has the constructor, cut and both getters, Main.java doesn't compile: each error names a part that's still missing. When it's done, the test drive prints:

oak, 120 cm
oak, 75 cm
oak, 75 cm
75 oak

Challenge 2

The gym's staff have regraded a route, so a second object for it has a different grade. The class is the lesson's ClimbingRoute, shortened to the parts this program uses. What does this program print?

Challenge 3

A school play keeps its cast in a list of Role objects: the play, the character, and the student who plays the character. Two roles are equal when they're the same character in the same play, whoever plays it. (Some names are in more than one play: Shakespeare has a Sebastian in both Twelfth Night and The Tempest.) In Role.java, write the method equals, the way module 23 did. The check runs:

ArrayList<Role> cast = new ArrayList<>();
cast.add(new Role("The Tempest", "Ariel", "Noor"));
cast.add(new Role("The Tempest", "Miranda", "Liv"));
Role understudy = new Role("The Tempest", "Ariel", "Theo");
System.out.println(understudy.equals(cast.get(0)));
System.out.println(cast.contains(new Role("Twelfth Night", "Viola", "Liv")));
System.out.println(cast.indexOf(new Role("The Tempest", "Miranda", "Sam")));

and expects:

true
false
1