{"id":"variables-naming","slug":"naming-variables","title":"Naming variables","text":"You choose each variable's name. A good name says what the value means: `price`, `laps`, `firstName`. Java has a few rules for names:\n\n- Letters, digits, `_` and `$` are allowed, but a name can't **start** with a digit: `level2` is fine, `2ndLevel` isn't.\n- No spaces and no other symbols, like `!`, `-` or `.`.\n- Words that are part of Java itself, such as `class`, `int` or `public`, can't be names.\n- Capitals matter: `score` and `Score` are two different names.\n\nWhen 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`.\n\n```java\nString firstName = \"Grace\";\nint yearOfBirth = 1906;\nSystem.out.println(firstName + \" was born in \" + yearOfBirth + \".\");\n```\n\n```output\nGrace was born in 1906.\n```\n\nA 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.\n","task":"The names below contain spaces, so the program doesn't compile. Rename them to `firstName` and `lastName` (camelCase), so it prints `Grace Hopper`.\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        String first name = \"Grace\";\n        String last name = \"Hopper\";\n        System.out.println(first name + \" \" + last name);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        String firstName = \"Grace\";\n        String lastName = \"Hopper\";\n        System.out.println(firstName + \" \" + lastName);\n    }\n}\n","hints":["Remove the space and start the second word with a capital: `first name` becomes `firstName`.","Change the names everywhere they appear, in the print line too."],"tests":[{"name":"Output","stdin":"","expect":"Grace Hopper","hidden":false}],"require":[{"pattern":"\\bfirstName\\b","message":"Name the first variable `firstName`."},{"pattern":"\\blastName\\b","message":"Name the second variable `lastName`."}],"forbid":[],"more":[{"task":"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:\n\n```\nSecond place: 7\nBonus: 3\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int 2ndPlace = 7;\n        int bonus! = 3;\n        System.out.println(\"Second place: \" + 2ndPlace);\n        System.out.println(\"Bonus: \" + bonus!);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int secondPlace = 7;\n        int bonus = 3;\n        System.out.println(\"Second place: \" + secondPlace);\n        System.out.println(\"Bonus: \" + bonus);\n    }\n}\n","hints":["A name can't start with a digit, and `!` isn't allowed in names at all.","Change the names in all four lines."],"tests":[{"name":"Output","stdin":"","expect":"Second place: 7\nBonus: 3","hidden":false}],"require":[{"pattern":"\\bsecondPlace\\b","message":"Name the first variable `secondPlace`."},{"pattern":"int\\s+bonus\\s*=","message":"Name the second variable `bonus`."}],"forbid":[]},{"task":"This program works, but its names `a`, `b` and `c` say nothing. Rename them to `city`, `population` and `area`, keeping the output the same:\n\n```\nRiverton has 48200 people and covers 31.5 square km.\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        // Riverton is a made-up town.\n        String a = \"Riverton\";\n        int b = 48200;\n        double c = 31.5;\n        System.out.println(a + \" has \" + b + \" people and covers \" + c + \" square km.\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        // Riverton is a made-up town.\n        String city = \"Riverton\";\n        int population = 48200;\n        double area = 31.5;\n        System.out.println(city + \" has \" + population + \" people and covers \" + area + \" square km.\");\n    }\n}\n","hints":["`a` holds the town's name, `b` the number of people and `c` the size.","Rename each variable where it's created and where it's printed."],"tests":[{"name":"Output","stdin":"","expect":"Riverton has 48200 people and covers 31.5 square km.","hidden":false}],"require":[{"pattern":"String\\s+city\\s*=","message":"Name the text variable `city`."},{"pattern":"int\\s+population\\s*=","message":"Name the whole-number variable `population`."},{"pattern":"double\\s+area\\s*=","message":"Name the decimal variable `area`."}],"forbid":[{"pattern":"\\b[abc]\\b","message":"Replace every `a`, `b` and `c` with the new names."}]}]}