Java Arena
Step 1 of 6

Objects you already know

You've been using objects since module 2. A Scanner is an object, every String is an object, and so is every ArrayList. An object holds some data, and it has methods, which you call with a dot after it: scanner.nextLine(), word.length(), list.add("tea").

Two objects of the same kind each have their own data:

String city = "Lima";
String river = "Mississippi";
System.out.println(city.length());
System.out.println(river.length());

ArrayList<String> groceries = new ArrayList<>();
ArrayList<String> chores = new ArrayList<>();
groceries.add("milk");
groceries.add("eggs");
chores.add("dishes");
System.out.println(groceries.size());
System.out.println(chores.size());
Output
4
11
2
1

city.length() and river.length() run the same method, but each answers for its own text. groceries and chores are two separate lists: adding to one doesn't touch the other.

Classes and new

Every object is made from a class. The class is the blueprint: it says what data each object holds and which methods it has. One class can make as many objects as you like, just as one blueprint can be used for a whole street of houses. Each house is built to the same plan, and painting one front door doesn't paint the others.

  • ArrayList is a class. new ArrayList<>() builds a new, empty list object from it.
  • Scanner is a class too, and new Scanner(System.in) builds a scanner that reads what the user types. The value in the parentheses is given to the new object as it's built.
  • String is a class. Strings are usually made by writing text in quotes, without new, but each one is still an object.

The type of a variable that holds an object is the name of its class: Scanner scanner is a variable that holds a Scanner.

A class in its own file

Java comes with a large library of ready-made classes, and a program can also have classes of its own. Each one is written in a file of its own, named after the class: the class Lamp is in the file Lamp.java. Here's a program of two files. You don't need to read Lamp.java closely yet: the next steps show how a class like it is written, line by line.

Main.java
public class Main {
    public static void main(String[] args) {
        Lamp desk = new Lamp("desk");
        Lamp porch = new Lamp("porch");
        desk.turnOn();
        desk.printState();
        porch.printState();
    }
}
Lamp.java
public class Lamp {
    private String place;
    private boolean on;

    public Lamp(String place) {
        this.place = place;
        this.on = false;
    }

    public void turnOn() {
        this.on = true;
    }

    public void turnOff() {
        this.on = false;
    }

    public void printState() {
        if (this.on) {
            System.out.println("The " + this.place + " lamp is on");
        } else {
            System.out.println("The " + this.place + " lamp is off");
        }
    }
}
Output
The desk lamp is on
The porch lamp is off
  • new Lamp("desk") builds a new Lamp object and gives it the text "desk". Lamp desk is a variable that holds a Lamp.
  • desk.turnOn() calls turnOn on the desk lamp only. The porch lamp is a separate object, so it's still off.
  • To use a class, you only need to know its methods and what they do, the same way you use an ArrayList without ever seeing its code.

In a challenge with several files, the editor shows a tab for each file, and Check compiles them all together.

Your task

Challenge 1 of 3

The program reads the name of a room. In Main.java, create a Lamp for that room, print its state, turn it on, and print its state again. Use the Lamp class in the other tab as it is. For the input kitchen the program prints:

The kitchen lamp is off
The kitchen lamp is on

Challenge 2

What does this program print? Keep track of each lamp separately.

Challenge 3

Poll.java holds a class for a quick vote. Write Main: the first line of input is the question, and every line after it is a vote, y or n, until the line end. Create a Poll for the question, give it each vote with voteYes() or voteNo(), and at the end call printResult(). The poll does the counting, so Main needs no counters of its own. For the input Pizza on Friday?, y, n, y and end, one per line, the program prints:

Pizza on Friday?
Yes 2, no 1