Java Arena
Step 3 of 8

Comparing with another object of the same class

A parameter can also be an object of the method's own class. That's how an object compares itself with another of its kind. Is this parcel heavier than that one?

Main.java
public class Main {
    public static void main(String[] args) {
        Parcel small = new Parcel("Lina", 400);
        Parcel big = new Parcel("Omar", 2500);
        System.out.println(big.isHeavierThan(small));
        System.out.println(small.isHeavierThan(big));
        System.out.println(small.isHeavierThan(small));
        System.out.println(big.gramsMoreThan(small));
    }
}
Parcel.java
public class Parcel {
    private String recipient;
    private int grams;

    public Parcel(String recipient, int grams) {
        this.recipient = recipient;
        this.grams = grams;
    }

    public boolean isHeavierThan(Parcel other) {
        return this.grams > other.grams;
    }

    public int gramsMoreThan(Parcel other) {
        return this.grams - other.grams;
    }

    public String toString() {
        return "Parcel for " + this.recipient + ", " + this.grams + " g";
    }
}
Output
true
false
false
2100
  • In big.isHeavierThan(small), this is big, the object before the dot, and other is small, the one in the parentheses. The method compares the two.
  • other.grams reads the other parcel's private variable, and that's allowed. private means only the code of the class Parcel, not only this object: code written in Parcel may use the private variables of any Parcel. In Main, small.grams still doesn't compile.
  • small.isHeavierThan(small) compares the parcel with itself, and a parcel isn't heavier than itself: > is false for equal weights. The same goes for two different parcels of the same weight: neither is heavier than the other.
  • gramsMoreThan answers with a number instead of true or false: how many grams more this parcel weighs than the other one.

The parameter's name, other, is only a name: compared or another work just as well. If the class has getters, other.getGrams() works too, but inside the class, reading the variable directly is just as good.

Letting the class compare

With isHeavierThan, finding the heaviest parcel in a list is the loop from module 19, but the comparison is the parcel's own method:

Parcel heaviest = parcels.get(0);
for (Parcel parcel : parcels) {
    if (parcel.isHeavierThan(heaviest)) {
        heaviest = parcel;
    }
}

The loop doesn't need to know how parcels are compared, or even that they're weighed in grams. How to compare two parcels is written once, in the class, and every part of the program asks the class.

Your task

Challenge 1 of 3

In Mountain.java, write the method isHigherThan(Mountain other). It returns true when this mountain is higher than the other one, and false otherwise (also when both are equally high). The check runs:

Mountain grayPeak = new Mountain("Gray Peak", 2410);
Mountain redHill = new Mountain("Red Hill", 980);
System.out.println(grayPeak.isHigherThan(redHill));
System.out.println(redHill.isHigherThan(grayPeak));

and expects:

true
false

Challenge 2

Packing to move house: a box fits inside another box when it's smaller in all three measures, so that its width, its height and its depth are each less than the other box's. (The boxes aren't turned around.) In Box.java, write the method fitsInside(Box other). The check runs:

Box shoebox = new Box(30, 12, 18);
Box crate = new Box(50, 40, 40);
System.out.println(shoebox.fitsInside(crate));
System.out.println(crate.fitsInside(shoebox));

and expects:

true
false

Challenge 3

What does this program print? A racer beats another one with a smaller time, and the loop keeps the best racer so far.