Java Arena
Step 4 of 5

Choosing from the list

A loop over the list doesn't have to print every object. An if inside it picks the ones to print, and the getters give the values to test:

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("At least 3 years old:");
        printAtLeast(pets, 3);
        System.out.println("At least 10 years old:");
        printAtLeast(pets, 10);
    }

    public static void printAtLeast(ArrayList<Pet> pets, int minAge) {
        for (Pet pet : pets) {
            if (pet.getAge() >= minAge) {
                System.out.println(pet);
            }
        }
    }
}
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
At least 3 years old:
Rex, 4 years old
Kiwi, 11 years old
At least 10 years old:
Kiwi, 11 years old
  • pet.getAge() is the age of this round's pet. The condition can use any getter, and anything that works on the value it returns: pet.getName().startsWith("K"), or pet.getName().equals(name).
  • The getter is needed because age is private. pet.age in Main doesn't compile: javac says age has private access in Pet.
  • The list can be a method's parameter, as in module 14. The method then works for any list of pets and any age.

The limit from the user

Often the list is read first, and then the user says what to look for. The loop that reads the list stops at the empty line, so the next nextLine() reads the line after it:

// ... the loop that reads the pets, until an empty line
int minAge = Integer.valueOf(scanner.nextLine());
printAtLeast(pets, minAge);

When nothing matches

To say so when no object matched, count the matches while printing, and check the count after the loop:

int count = 0;
for (Pet pet : pets) {
    if (pet.getAge() >= minAge) {
        System.out.println(pet);
        count++;
    }
}
if (count == 0) {
    System.out.println("No pets that old");
}

Your task

Challenge 1 of 3

What does this program print? Check the condition for each pet in turn.

Challenge 2

A yearly bird count. Each line of the input holds a species, last year's count and this year's count, separated by commas, like robin,15,12. The input ends with an empty line. BirdCount.java is ready. Read the lines into the list counts. Then print every species that was seen less this year than last year, with how many fewer birds, like robin: 3 fewer, in the order they were read. Last, print how many of the species went down, like 2 of 4 species went down. For the input robin,15,12, heron,4,6, sparrow,40,31, owl,2,2 and an empty line, the program prints:

robin: 3 fewer
sparrow: 9 fewer
2 of 4 species went down

Challenge 3

Write a method printFrom(ArrayList<Friend> friends, String city) that prints the name of every friend who lives in city, one per line, and then how many there were, like 2 in Lisbon. Friend.java is ready. The check calls the method with the list in main (Ines and Mia in Lisbon, Tom in Leeds) and the city Lisbon, and expects:

Ines
Mia
2 in Lisbon