Maths and user input
Doing sums in Java, the one division rule that trips everyone, and asking the user questions.
DogYears.java and Greeter.java.
Maths
| Symbol | Does | Example |
|---|---|---|
+ | Add | 5 + 2 is 7 |
- | Subtract | 5 - 2 is 3 |
* | Multiply | 5 * 2 is 10 |
/ | Divide | 6 / 2 is 3 |
% | Remainder after dividing | 7 % 2 is 1 |
The division trap
Divide two whole numbers and Java throws the remainder away. Drag it.
The fix is to make one side a decimal. Any of these work:
5 / 2.0
5.0 / 2
(double) 5 / 2
When a number comes out 0.0 and the reason isn't clear, this is almost always it.
Casting
Casting changes a value from one type to another. Put the desired type in brackets in front of it.
(int) 3.4 // 3
(int) 3.9 // 3, not 4
(double) 4 // 4.0
(int) 3.9 is 3. Casting a decimal to a whole number cuts the decimal part off and keeps what is left.
User input
A Scanner reads what the user types. Three parts.
// 1. at the very top of the file
import java.util.Scanner;
public class Greeter {
public static void main(String[] args) {
// 2. make one Scanner
Scanner scanner = new Scanner(System.in);
System.out.println("What is your name?");
// 3. read whatever they type
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}
| To read | Use |
|---|---|
| A line of words | scanner.nextLine() |
| A whole number | scanner.nextInt() |
| A decimal | scanner.nextDouble() |
Make the Scanner once, at the top of main. A new one is not needed for each question.
Problems
1. Work them out on paper
Write the answer for each, then run them and check.
5 / 2 2.5 * 2
5 / 2.0 (int) (2.5 * 3)
(int) (5.0 / 2.0) 2.5 + 2.5
(int) (5 / 2) 3.0 + 7
5 * 2 (int) (2.5 - 5)
Hint
For each one ask two questions. Are both sides whole numbers? And is there a cast on the outside? The cast happens last, after everything in the brackets.
2. Dog years
DogYears.javaStore a human age in an int, convert it to dog years, and print the result. One dog year is about seven human years.
A 12 year old human is 84 in dog years.
Hint 1
Two variables are needed: one for the human age, one for the dog age. The second is worked out from the first.
Hint 2
Build the sentence by joining pieces with +, the same way "Hello, " + name + "!" works.
3. Ask first
DogYears.javaModify the dog years program so it asks for the user's age instead of having it fixed in the code.
Example runHow old are you?
12
A 12 year old human is 84 in dog years.
Hint 1
Three things: the import line at the top, one Scanner at the start of main, and nextInt() where the fixed number used to be.
Hint 2
nextInt() returns a number, so it goes on the right of an =, like int age = scanner.nextInt();
4. Split the change
Greeter.javaStart from Greeter.java, which already has a Scanner set up. Ask for a number of sweets and a number of people. Print how many each person gets and how many are left over.
Sweets? 17
People? 5
3 each, 2 left over.
Hint
This is the one place whole-number division is what's needed. / gives the share, % gives the leftovers.