You are on page 1of 26

Fundamental Programming

in Java I
Instructor: Dr. Fahed Jubair
Let us Start with A Demo

Write a program that computes the average of three integers given by


the user from the console.

© All rights reserved. 2


Variables
• Variables hold values that may change during program execution
• Variables reside in main memory
• Variables are declared by a type and a name
• Syntax on variable declarations:
data_type variable_name ;
e.g., int count ;
e.g., double average ;

© All rights reserved. 3


Primitive Data Types in Java
Type Description Size in bytes Default value Literal examples
boolean true or false 1 false true, false
character Unicode characters 2 \u000 'a’, ‘b’, ‘?‘, ‘#’, ‘3’, ‘ ‘
byte 8-bit signed integer 1 0 -128 … 127
short 16-bit signed integer 2 0 -32,768 ... 32,767
integer 32-bit signed integer 4 0 -2,147,483,648 .. 2,147,483,647
-9,223,372,036,854,775,808 ..
long 64-bit signed integer 8 0
9,223,372,036,854,775,807
32-bit IEEE 754 ±1.40239846 x 10-45 … 3.40282347 x
float 4 0.0
floating point 1038
64-bit IEEE 754 ±4.9406564584124654 x 10-324 …
double 8 0.0
floating point 1.7976931348623157 x 10208

© All rights reserved. 4


Strings
• A string represents a sequence of characters
• Strings are not primitive data types but they have their predefined
class in Java
• Example:
String str = “Hello!”;
• The size (in bytes) allocated by a string depends on the number of
characters inside it
• More about strings later

© All rights reserved. 5


Assignments and Expressions
• Syntax
variable = expression ;
Arithmetic Operators
• Example:
+ add
int x, y; − subtract
double z ; ∗ multiply
x = 2; / divide

y = x + 22 ; % modulus
++ increment
z = 4.4 * 5.3 / x ;
−− decrement
x++ ;
--y;
© All rights reserved. 6
Increment and Decrement Statements

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;

© All rights reserved. 7


Bitwise operators

Bitwise Operators
int x = 10;
& Bitwise and
int y = 13; | Bitwise OR

int z = x & y ; ~ Bit complement


^ Bitwise XOR
System.out.println(z); << Shift left
>> Shift right

What is the printed value of z ?

© All rights reserved. 8


Assignment Operators
Assignment Operators
+= 𝐶 += 𝐴 is equivalent to 𝐶 = 𝐶 + 𝐴
−= 𝐶 −= 𝐴 is equivalent to 𝐶 = 𝐶 − 𝐴
∗= 𝐶 ∗= 𝐴 is equivalent to 𝐶 = 𝐶 ∗ 𝐴
/= 𝐶/= 𝐴 is equivalent to 𝐶 = 𝐶 / 𝐴
%= 𝐶 % = 𝐴 is equivalent to 𝐶 = 𝐶 % 𝐴
&= 𝐶 & = 𝐴 is equivalent to 𝐶 = 𝐶 & 𝐴
|= 𝐶 | = 𝐴 is equivalent to 𝐶 = 𝐶 | 𝐴
^= 𝐶 ^ = 𝐴 is equivalent to 𝐶 = 𝐶 ^ 𝐴

© All rights reserved. 9


Logical Operators
• Define operations between Boolean variables
boolean x = true ; Logical Operators
boolean y = false ; && logical and

boolean z = x || y ; || logical OR
! Logical not
System.out.println(z);

What is the printed value of z ?

© All rights reserved. 10


Constants
• We use the keyword final to declare constants, variables that have
permanent values during the execution

© All rights reserved. 11


Scanner Utility
• A built-in class for reading inputs from console
• Some of its supported methods (see Java documentation for more)
nextBoolean( ) Read the next token as boolean
nextByte( ) Read the next token as byte
nextShort( ) Read the next token as short
nextInt( ) Read the next token as integer
nextLong( ) Read the next token as long
nextFloat( ) Read the next token as float
nextDouble( ) Read the next token as double
next( ) Read the next token as string
nextLine( ) Read the whole next line as string

© All rights reserved. 12


Math Utility
• A built-in class for math operations
• Some supported methods (see Java documentation for more):
Math.abs(-1.2) Return the absolute value
Math.ceil(2.5) Return the ceiling function
Math.floor(2.5 ) Return the floor function
Math.exp(1.2 ) Return the exponential function (𝑒 ! )
Math.log(3.5 ) Return the natural logarithm (base e) function
Math.max(3.4 , 5.4) Return the maximum number
Math.min(3.4 , 5.4) Return the minimum number
Math.sqrt(10.4) Return the square root
Math.Random() Return a random double number between 0.0 and 1.0
Math.pow(3.0,4.0) Returns the value of the first argument raised to the power
of©the second
All rights argument
reserved. 13
Casting
• Java allows casting (with some restrictions) from one data type to
another
• Some examples:
int x = ‘A’ ; // the Unicode value of ‘A’ is stored in x
char c = 16; // the lower 16-bit are used as a Unicode character
long w = 2 * x ; // casting integers to double is straightforward
int u = w ; // casting longs to integers is illegal
int z = (int) Math.sqrt(8.0); // casting doubles to integers need to be explicit

© All rights reserved. 14


Selection Statements
• Decision points to determine execution paths based on an evaluation
of boolean conditions

if ( 𝑖 % 2 == 0 )
if ( 𝑥 > 100 ) System.out.println(“even number”);
System.out.println(“larger than 100”); else
System.out.println(“odd number”);

© All rights reserved. 15


Relational Operators
• Operators for evaluating conditions

Assignment Operators
𝐴 == 𝐵 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑒𝑞𝑢𝑎𝑙 𝑡𝑜 𝐵
𝐴!=𝐵 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑛𝑜𝑡 𝑒𝑞𝑢𝑎𝑙 𝑡𝑜 𝐵
𝐴>𝐵 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑙𝑎𝑟𝑔𝑒𝑟 𝑡ℎ𝑎𝑛 𝐵
𝐴<𝐵 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑠𝑚𝑎𝑙𝑙𝑒𝑟 𝑡ℎ𝑎𝑛 𝐵
A >= B 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑙𝑎𝑟𝑔𝑒𝑟 𝑡ℎ𝑎𝑛 𝑜𝑟 𝑒𝑞𝑢𝑎𝑙 𝑡𝑜 𝐵
A <= B 𝑟𝑒𝑡𝑢𝑟𝑛 𝑡𝑟𝑢𝑒 𝑖𝑓 𝐴 𝑖𝑠 𝑠𝑚𝑎𝑙𝑙𝑒𝑟 𝑡ℎ𝑎𝑛 𝑜𝑟 𝑒𝑞𝑢𝑎𝑙 𝑡𝑜 𝐵

© All rights reserved. 16


Nested If statements
Write Program to Print Grades

© All rights reserved. 17


Note
• The else clause matches the most recent if clause in the same block.

Question: how to force the else statement to match the first clause?

© All rights reserved. 18


Conditional Expressions
(boolean-expression) ? expression1 : expression2

e.g.,
if (x > 0)
y=1
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;

© All rights reserved. 19


Switch Case

© All rights reserved. 20


Loops
• Loops tell the program to repeatedly execute the same lines of code
• Example: write code for computing ∑#$$!"# 𝑖 %

// using a for-loop statement // using a while-loop statement


int sum = 0 ; int sum = 0 ;
for ( 𝑖 = 1; 𝑖 <= 100; 𝑖 = 𝑖 + 1 ) int 𝑖 = 1 ;
sum = sum + (int) Math.pow(𝑖, 2) ; while (𝑖 <= 100) {
sum += (int) Math.pow(𝑖, 2) ;
𝑖++;
}

© All rights reserved. 21


Nested loops
• Write code that computes ∑#$$ ∑ !
!"$ &"$(𝑖 ∗ 𝑗)

int sum = 0 ;
for ( 𝑖 = 0; 𝑖 <= 100; 𝑖 + + )
for ( 𝑗 = 0; 𝑗 <= 𝑖; 𝑗 + + )
sum = sum + 𝑖 ∗ 𝑗 ;

© All rights reserved. 22


Keywords: break and continue
• What is the output for each of the following programs

int sum = 0 ;
int sum = 0 ; int k = 1 ;
for ( 𝑖 = 0; 𝑖 <= 15; 𝑖 + + ) { while ( 𝑘 < 100) {
if ( 𝑖%3 == 0) sum += 𝑘 * 2;
continue ; if ( sum > 20)
sum += 𝑖 ; break ;
} ++k ;
System.out.println(sum); }
System.out.println(𝑘);

© All rights reserved. 23


Classroom Exercises

1. Write a Java program that prints “prime” if a given number n (read as


an input) is prime. Otherwise, the program prints “not prime”.

2. Write a java program that prints all square numbers up to 10000, i.e.,
1, 4, 9, 16, 25, 36, …, 10000

3. Write a java program that uses loops to print all positive integer pairs
x and y that satisfy the inequality: 2 ∗ 𝑥 + 3 ∗ 𝑦 ≤ 60

© All rights reserved. 24


Classroom Exercise: Triangular Patterns
4. Write a java program that reads an input integer N from the user,
and then prints a triangular pattern of stars, with height = N. See the
below examples.

N=3 N=5 N=8

* * *
** ** **
*** *** ***
**** ****
***** *****
******
*******
********
© All rights reserved. 25
Classroom Exercise: Zodiac Year
5. Write a program that prompts the user to enter a year and displays the
animal for the year.

© All rights reserved. 26

You might also like