Java Arena
Step 8 of 8

Putting it all together

This module's tools often turn up together, even in a single class. Here they are side by side:

Tool Example
An object as a parameter printer.print(document)
An object as an instance variable private Address address;
A parameter of the class's own type big.isHeavierThan(small)
Equal contents, not the same object mine.equals(yours)
List methods that use equals booked.contains(new Seat("B", 7))
A method that returns a new object photo.rotated()

A position on a grid of squares uses three of them. A position can make the position next to it, tell how many steps away another position is, and tell whether it's equal to another:

Main.java
public class Main {
    public static void main(String[] args) {
        Position start = new Position(0, 0);
        Position shop = new Position(3, -2);
        Position next = start.moved(1, 0);
        System.out.println(next);
        System.out.println(start.stepsTo(shop));
        System.out.println(next.moved(2, -2).equals(shop));
        System.out.println(start);
    }
}
Position.java
public class Position {
    private int x;
    private int y;

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Position moved(int dx, int dy) {
        return new Position(this.x + dx, this.y + dy);
    }

    public int stepsTo(Position other) {
        return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
    }

    public boolean equals(Object compared) {
        if (this == compared) {
            return true;
        }
        if (!(compared instanceof Position)) {
            return false;
        }
        Position other = (Position) compared;
        return this.x == other.x && this.y == other.y;
    }

    public String toString() {
        return "(" + this.x + ", " + this.y + ")";
    }
}
Output
(1, 0)
5
true
(0, 0)
  • moved returns a new Position, so start stays at (0, 0), as the last line shows.
  • stepsTo takes another Position and reads its private x and y. It counts the steps from square to square along the rows and columns, without going diagonally: 3 steps across and 2 steps down make 5.
  • next.moved(2, -2) is a new object, never the one in shop, but its contents are the same, so equals says they're equal. With the equals every class gets, it would say false.

This step's challenges build on the class: a robot that moves around the grid, and a treasure hunt with a list of positions.

The end of part 5

This step ends the material of the MOOC's fifth part. Part 5 went deeper into object-oriented programming. You planned classes and gave methods and constructors several versions by overloading them. You saw that a variable of a primitive type, such as an int, holds its value, while a variable of a class type holds a reference to an object. Two variables can refer to the same object, and a reference can be null, which means no object at all. In this module, objects started working together: as parameters, as instance variables of other objects, compared with equals, found in lists and returned from methods. Next, part 6 puts lists inside objects. Part 5 closes with the MOOC's conclusion page on its own site, for reading only.

Your task

Challenge 1 of 3

A robot moves on the grid, and Position.java is the class from above. Robot.java has the robot's name and position. Write two methods in it:

  • move(String direction) moves the robot one square: up adds 1 to y, down takes 1 from y, right adds 1 to x and left takes 1 from x. Any other direction does nothing. Use the position's moved method, and keep the new position it returns.
  • isAt(Position place) returns true when the robot is at that place. Compare the positions with equals.

The check runs:

Robot robo = new Robot("Robo", new Position(0, 0));
robo.move("up");
robo.move("up");
robo.move("right");
System.out.println(robo);
System.out.println(robo.isAt(new Position(1, 2)));
System.out.println(robo.isAt(new Position(2, 1)));

and expects:

Robo at (1, 2)
true
false

Challenge 2

What does this program print? It uses the Position class from the lesson, and a list of the places visited so far.

Challenge 3

A treasure hunt. The list treasures in Main.java holds the positions of three hidden coins, and Position.java is the class from the lesson. The player starts at (0, 0). Read moves, up, down, left or right, one per line, until an empty line, and move the player one square each time, with moved. After each move, if a coin is at the new position, remove it from the list and print Found a coin at and the position. At the end, print how many coins are left. Let contains and remove do the searching, without a loop over the list. For the input shown below, the program prints:

Found a coin at (1, 0)
Found a coin at (2, 2)
Coins left: 1