Java Arena
Step 5 of 5

Finding in the list

Module 13 found the largest number in a list: keep the largest one so far in a variable, and replace it whenever a larger one comes along. With objects, the variable holds a whole object. After the loop you have the largest value and also whose it is:

Main.java
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Pet> pets = new ArrayList<>();
        pets.add(new Pet("Rex", 4));
        pets.add(new Pet("Misty", 2));
        pets.add(new Pet("Kiwi", 11));

        System.out.println("Oldest: " + findOldest(pets));
        printPet(pets, "Misty");
        printPet(pets, "Bo");
    }

    public static Pet findOldest(ArrayList<Pet> pets) {
        Pet oldest = pets.get(0);
        for (Pet pet : pets) {
            if (pet.getAge() > oldest.getAge()) {
                oldest = pet;
            }
        }
        return oldest;
    }

    public static void printPet(ArrayList<Pet> pets, String name) {
        for (Pet pet : pets) {
            if (pet.getName().equals(name)) {
                System.out.println("Found: " + pet);
                return;
            }
        }
        System.out.println("No pet called " + name);
    }
}
Pet.java
public class Pet {
    private String name;
    private int age;

    public Pet(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public String toString() {
        return this.name + ", " + this.age + " years old";
    }
}
Output
Oldest: Kiwi, 11 years old
Found: Misty, 2 years old
No pet called Bo
  • Pet oldest = pets.get(0); starts from the first pet, which is always a real candidate. The loop compares every pet's age with the oldest one so far, and when a pet is older, it becomes the new oldest.
  • findOldest returns a Pet: a method can return an object, like any other value. The caller gets the whole pet, and can print it or call its getters.
  • With >, the first of two pets of the same age stays the oldest; with >=, the later one would win.
  • get(0) needs at least one pet in the list. When the list might be empty, check pets.isEmpty() first.

Finding one by name

printPet goes through the list until a pet's name equals the name it's looking for (with ==, Strings aren't compared reliably). The return ends the method as soon as the pet is found. Only when the loop gets through the whole list without finding it does the last line run.

Your task

Challenge 1 of 3

Swimmer.java is ready. Write a method fastest(ArrayList<Swimmer> swimmers) that returns the swimmer with the smallest time. When two swimmers share the best time, return the one that comes first in the list. The list always has at least one swimmer. The check runs System.out.println(fastest(swimmers)); for the list in main and expects:

Tom (64 s)

Challenge 2

What does this program print? Watch the two comparisons when two pets are the same age.

Challenge 3

A program to end the module, with a class of your own. First write Crop: a crop has a name and a weight in kilos, kept in private object variables, and it prints like potatoes (42 kg). Then write main. Read crops, a name and then the kilos, until the name is an empty line. Then print:

  • 10 kg or more: and then every crop of at least 10 kilos, one per line;
  • Total: with the kilos of all the crops added up, and kg;
  • Best: and the heaviest crop (the first of them, if two weigh the same).

If no crops were entered, print only No crops. For the input potatoes, 42, beans, 3, carrots, 12 and an empty line, the program prints:

10 kg or more:
potatoes (42 kg)
carrots (12 kg)
Total: 57 kg
Best: potatoes (42 kg)