Java Arena
Step 6 of 6

Several objects

One class can make as many objects as a program needs, and each object keeps its own state: calling a method on one of them never changes the others. The values for new objects often come from the user:

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("First plant:");
        String first = scanner.nextLine();
        System.out.println("Second plant:");
        String second = scanner.nextLine();

        Plant left = new Plant(first, 10);
        Plant right = new Plant(second, 10);
        left.grow(8);
        right.grow(3);
        left.cut(12);
        left.printInfo();
        right.printInfo();
    }
}
Plant.java
public class Plant {
    private String name;
    private int height;

    public Plant(String name, int height) {
        this.name = name;
        this.height = height;
    }

    public void printInfo() {
        System.out.println(this.name + ", " + this.height + " cm");
    }

    public void grow(int cm) {
        this.height = this.height + cm;
    }

    public void cut(int cm) {
        this.height = this.height - cm;
        if (this.height < 5) {
            this.height = 5;
        }
    }
}
Input
mint
basil
Output
First plant:
Second plant:
mint, 6 cm
basil, 13 cm
  • left and right are built from the same class, with names read from the user, and each has its own name and height.
  • left.grow(8) and left.cut(12) change only left: the variable in front of the dot decides which object a method works on. right only grew by 3.

A program can use as many classes as it needs, each in its own file next to Main.java, and main can make objects of all of them: a garden program could have a Plant class and a Lamp class, and create a few of each. Module 19 shows how to keep any number of objects in a list.

Your task

Challenge 1 of 3

It's a snail race, and Snail.java is ready. The program reads the names of two snails, one per line. Create a Snail for each. The race has three rounds: in each round, read how many centimeters the first snail crawls and then how many the second crawls, and give each number to the right snail with crawl. At the end, print where both snails are with printDistance(). For the input Speedy, Shelly, 3, 2, 1, 4, 5 and 1, one per line, the program prints:

Speedy has crawled 9 cm
Shelly has crawled 7 cm

Challenge 2

What does this program print? There are three candles, and two of them have the same scent.

Challenge 3

Now write the class yourself. Main.java is ready: it reads the names of two players and then dice rolls, one per line, until the line end. The rolls go to the players in turns, first player first, and at the end both positions are printed. Write the class Player in Player.java:

  • Player(String name) builds a player, who starts on square 1.
  • move(int squares) moves the player on. The board has 30 squares, and a move that would go past square 30 stops at square 30.
  • printPosition() prints where the player is, as in the output below.

For the input Ada, Ben, 6, 3, 5, 4 and end, one per line, the program prints:

Ada is on square 12
Ben is on square 8