A. Introduction
Other than printing out Christmas trees and graphics, System.out.println statement can also do simple calculations such as 3.1415*2*3. Numeric values such as "3.1415", "2" or "2.71828" are frequently used to compute. These numeric values are called "literals". Sometimes literals maybe fractions or power of tens. How would you write fractions or exponents? See if you can figure out from the table below.
B. Arithmetic equations are made of variables, literals and -- operators
You always need operators to compute. +, -, * are straight forward. But why 1729/10 = 172? what is 1729%10? See the table below:
C. Try this:
1. What will happen if you run the following code?
class HelloWorld {
public static void main(String[] args) {
System.out.print("5+4 is:");
System.out.println(5+4);
}
}
2. What's the difference between System.out.print("5+4 is:") and System.out.print(5+4 )?
3. What's the difference between System.out.print("5+4 is:") and System.out.println("5+4 is:")?
4. How can you print out the values of 15 - 4? 15 x 4 ? 15 ➗4?
5. What will “System.out.print(5/2);” print out?
6. How to make sure 15 ➗4 prints correct decimal values in Java?
7. Explain why 1729%10 is9, why 1729/10 is 172.
If you write n%10, it will always print out the last digit of the variable, or n. If you write n/10, it will print out n without the last digit.