0% found this document useful (0 votes)
5 views17 pages

Lec 4

Uploaded by

z9mohamed74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Lec 4

Uploaded by

z9mohamed74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Computer Programming

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.

Multiplies values on either side of the


* (Multiplication) A * B will give 200
operator.

Divides left-hand operand by right-hand


/ (Division) B / A will give 2
operand.

Divides left-hand operand by right-hand


% (Modulus) B % A will give 0
operand and returns remainder.
++ (Increment) Increases the value of operand by 1. B++ gives 21
-- (Decrement) Decreases the value of operand by 1. B-- gives 19

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

A literal is a constant value that appears directly in the


program.
For example, 34, 1,000,000, and 5.0 are literals in the
following statements:
– int i = 34;
– long x = 1000000;
– double d = 5.0;

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

Floating-point literals are written with a decimal point.


By default, a floating-point literal is treated as a double type
value.
For example, 5.0 is considered a double value, not a float
value.
You can make a number a float by appending the letter f or
F, and make a number a double by appending the letter d
or D.
For example, you can use 100.2f or 100.2F for a float
number, and 100.2d or 100.2D for a double number.

6
Scientific Notation

Floating-point literals can also be specified in scientific


notation,
For example, 1.23456e+2, same as 1.23456e2, is
equivalent to 123.456
1.23456e-2 is equivalent to 0.0123456.
E (or e) represents an exponent and it can be either in
lowercase or uppercase.

7
Arithmetic Expressions

3 + 4 x 10( y − 5)(a + b + c ) 4 9+ x
− + 9( + )
5 x x y

is translated to

(3 + 4*x) / 5 – 10*(y - 5) * (a + b + c) / x + 9*(4/x + (9+x)/y)

8
How to Evaluate an Expression

• Here is an example of how an expression is evaluated:

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

Write a program that converts a Fahrenheit degree to Celsius


using the formula:

celsius = ( 59 )( fahrenheit− 32)


Note: you have to write
celsius = (5.0 / 9) * (fahrenheit – 32)

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

You might also like