Agenda Review types variables identifiers declaration initialization type

Agenda • Review – types, variables, identifiers, declaration, initialization, type casting, assignment, concatenation • Arithmetic Operators • Rational Operators • Logical Operators • Assignment Operators • Constant • Conditional Control Structure • Homework

Arithmetic Operators Operator Meaning Example + addition 3+x subtraction p–q * multiplication 6 * I / division 10 / 4(2, not 2. 5) % mod(remainder) 11 % 8 3. 0 / 4 -> ? 3 / 4. 0 -> ? (int) 3. 0 / 4 -> ? (double) 3 / 4 -> ? Int result = 13 – 3 * 6 / 4 % 3

Int num 1 = 5; Int num 2 = 3; Int result; double. Num 1 = 5; double. Num 2 = 3; double. Result; Result = num 1 / num 2; System. out. println("num 1 / num 2: " + result); double. Result = double. Num 1 / double. Num 2; System. out. println("double. Num 1 / double. Num 2: “ + double. Result); double. Result = num 1 / double. Num 2; System. out. println("num 1 / double. Num 2: " + double. Result); result = num 1 % num 2; System. out. println("num 1 % num 2: " + result); double. Result = double. Num 1 % double. Num 2; System. out. println("double. Num 1 % double. Num 2: “ + double. Result);

Rational operators Operator == != > < >= <= Meaning Example equal to not equal to greater than division if (x==100) if (x!=100) if (x > 100) if (x < 100) if (x >=100) if (x <=100) greater than or equal to less than or equal to

Logical Operators Operator ! && || && T T T F F Meaning Example NOT if (!found) AND if (x < 3 && y> 4) OR if (age<6 || age>70) F F F || T T T F T F ! T F F T

Assignment operators Operator = += -= *= /= %= ++ -- Example Meaning x = 100 simple assignment x += 4 x = x +4 y -= 6 y = y -6 p *= 5 p=p*5 n/= 10 n = n / 10 n %= 10 n = n % 10 i++ or ++I i = i +1 k-- or --k k = k -1

Conditional control structure If statement If (age < 6) System. out. println(“not eligible for elementary school”); If (age >= 6 && age < 12) System. out. println(“elementary school”); boolean game. Over = true; if (game. Over) { //(game. Over == true) System. out. println("Thanks for playing!"); }

Conditional control structure If – else statement If (age < 6) System. out. println(“not eligible for elementary school”); else System. out. println(“elementary school”); boolean game. Over = true; if (game. Over) { //(game. Over == true) System. out. println("Thanks for playing!"); else System. out. println(“Continue!");

Constants • is a identifier that stores a value that cannot be changed from its initial assignment. • Form: final <type> <identifier> final double PI = 3. 41; • Constant identifier are all uppercase

homework The Birthday. Game guesses a player’s birthday by having the player perform mathematics with the month and day of their birthday. The number computed by the player is then entered and the program displays the month and day of the player’s birthday. The player is given directions for computing a number that uses the player’s birth month and birth day to calculate 1. 2. 3. 4. 5. 6. 7. Determine your birth month(January=1, Feb=2. . ) Multiply that number by 5 Add 6 to that number. Multiply the number by 4. Add 9 to the number Multiply that number by 5 Add the birth day to the number(10 if born on the 10 th. . ) Then 165 is substracted from the number. This number is then divided by 100 - the quotient is the birth month, the remainder is the birth day. Use println to output the result.

Notes for the homework • Variables for the birth month and birth day, these two can be constants • Design the program by yourself • The output should have the following: 1. “The number before 165 is deducted: “ 2. “<explain what will be done to the number” 3. result-> “birthday: “
- Slides: 11