Java Arena
Step 3 of 6

Naming variables

You choose each variable's name. A good name says what the value means: price, laps, firstName. Java has a few rules for names:

  • Letters, digits, _ and $ are allowed, but a name can't start with a digit: level2 is fine, 2ndLevel isn't.
  • No spaces and no other symbols, like !, - or ..
  • Words that are part of Java itself, such as class, int or public, can't be names.
  • Capitals matter: score and Score are two different names.

When a name is made of several words, Java programmers write it in camelCase: the first word in lower case, and every following word starting with a capital: firstName, numberOfLaps, isRaining.

String firstName = "Grace";
int yearOfBirth = 1906;
System.out.println(firstName + " was born in " + yearOfBirth + ".");
Output
Grace was born in 1906.

A name that breaks the rules confuses the compiler, and its errors can look strange. String first name gives ';' expected, because Java reads first as the whole name and then doesn't know what name is doing there.

Your task

Challenge 1 of 3

The names below contain spaces, so the program doesn't compile. Rename them to firstName and lastName (camelCase), so it prints Grace Hopper.

Challenge 2

Two names break the rules: one starts with a digit and one contains !. Rename 2ndPlace to secondPlace and bonus! to bonus, so the program prints:

Second place: 7
Bonus: 3

Challenge 3

This program works, but its names a, b and c say nothing. Rename them to city, population and area, keeping the output the same:

Riverton has 48200 people and covers 31.5 square km.