Lec 4
Lec 4
chapter2
Java Terminologies
Lecture 4
1
Arithmetic Operators
Operator Description Example
Adds values on either side of the
+ (Addition) A + B will give 30
operator.
Subtracts right-hand operand from left-
- (Subtraction) A - B will give -10
hand operand.
2
Exponent Operations
The Math.pow(a, b) method can be used to compute The pow
method is defined in the Math class in the Java API.
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
3
Number Literals
4
Integer Literals
A compilation error would occur if the literal were too large
for the variable to hold. For example, the statement byte b =
1000 would cause a compilation error, because 1000 cannot
be stored in a variable of the byte type.
An integer literal is assumed to be of the int type, whose
value is between -231 (-2147483648) to 231–1
(2147483647).
To denote an integer literal of the long type, append it with
the letter L or l. L is preferred because l (lowercase L) can
easily be confused with 1 (the digit one).
For example, 564332l, 566778L
5
Floating-Point Literals
6
Scientific Notation
7
Arithmetic Expressions
3 + 4 x 10( y − 5)(a + b + c ) 4 9+ x
− + 9( + )
5 x x y
is translated to
8
How to Evaluate an Expression
9
Reading Input from the Console
• Reading input from the console enables the program to accept
input from the user.
• Java uses System.out to refer to the standard output device
and System.in to the standard input device.
• By default, the output device is the display monitor and the input
device is the keyboard.
• Console input is not directly supported in Java, but you can use
the Scanner class to create an object to read input from
System.in, as follows:
Scanner input = new Scanner(System.in);
• The syntax new Scanner(System.in) creates an object of the
Scanner type. The syntax Scanner input declares that input is a
variable whose type is Scanner.
10
Reading Input from the Console
• The whole line Scanner input = new Scanner(System.in) creates a
Scanner object and assigns its reference to the variable input.
• An object may invoke its methods. To invoke a method on an
object is to ask the object to perform a task. You can invoke the
methods listed in the next table to read various types of input.
11
Reading Input from the Console
12
Reading Input from the Console
13
Problem: Converting Temperatures
14
Problem: Displaying Current Time
• Write a program that displays current time in GMT in the
format hour:minute:second such as 1:45:19.
• The currentTimeMillis method in the System class returns the
current time in milliseconds since the midnight, January 1,
1970 GMT. (1970 was the year when the Unix operating
system was formally introduced.) You can use this method to
obtain the current time, and then compute the current second,
minute, and hour as follows.
Elapsed
time
Time
Unix Epoch Current Time
01-01-1970 System.currentTimeMills()
00:00:00 GMT
15
Problem: Displaying Current Time
16
Problem: Displaying Current Time
17