Advanced Arithmetic Operation
Math class (docs)
Math is a class that provides common mathematical operations:
- Power and root
- Logarithm
- Max, Min
- Trigonometry
- Random
- …
Program.java
Data Type: Integer and Double
Integer [+,-,*,/] Integer = Integer
Integer [+,-,*,/] Integer = Integer
Double [+,-,*,/] Double = Double
Double [+,-,*,/] Double = Double
Double [+,-,*,/] Integer = Double
Double [+,-,*,/] Integer = Double
Integer / Integer
Integer / Integer
Integer → Double
Integer → Double
Double → Integer
Double → Integer
Input
Basic Methods of Scanner
Reading Strings
- nextLine()
- next()
One line
Reading Numners
- nextInt()
- nextDouble()
- nextLong()
Integer
Special Case
Output Formatting
Too Many Trailing Digits!
Try use this!
System.out.printf()
- System.out.format()
- Example
Format Works
Converters & Flags
| Converters | Flag | Description |
|---|---|---|
| d | Integer | |
| f | Float/Double | |
| n | Newline | |
| s | String | |
| + | Includes sign, whether positive or negative. | |
| , | Includes locale-specific grouping characters (thousands) | |
| - | Left-justified. | |
| 8 | Eight characters in width, right justified | |
| 08 | Eight characters in width, with leading zeroes as necessary. | |
| .3 | Three places after decimal point. | |
| 10.3 | Ten characters in width, right justified, with three places after decimal point. |
Saving Formatted String in Variables
- Example: