introduction to oop with java - abu khleif · notes •the boolean expression is enclosed in...

46
Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 www.abukhleif.com

Upload: others

Post on 21-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Introduction to OOP with Java

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Page 2: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Control Flow Statements:Selection

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Lecture 03:

Page 3: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Instructor

• AbuKhleif, ‘Mohammad Noor’• Computer Engineer (JU 2012-2017)• Software Automation Engineer @ Atypon – John Wiley and

Sons Company - Jordan Branch

• Reach me at:• www.abukhleif.com• [email protected]• facebook.com/moh.noor94• twitter.com/moh_noor94

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

3

Page 4: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Course

• Java SE Basics• Object Oriented Programming• Course Page:

www.abukhleif.com/courses/java-101-sep-2017• Or, go to: www.abukhleif.com Courses Java 101 Course – Sep 2017

• Course Facebook Group:www.facebook.com/groups/AKF2017Java

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

4

Page 5: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s Start!

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

5

Page 6: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Quick Review

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

6

• Relational (Comparison) Operators: > >= < <= == !=

• Logical Operators: && || !

• A boolean expression is an expression that evaluates to a booleanvalue: true or false

Page 7: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Selection

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

7

Page 8: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Motivations

If you assigned a negative value for radius in Lecture 02 -Task 2, ‘ComputeVolumeOfCylinder’, the program would print an invalid result. If the radius is negative, you don't want the program to compute the volume. How can you deal with this situation?

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

8

Page 9: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

One-way if Statements

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

9

Page 10: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

One-way if Statements

•A one-way if statement executes an action if an only if the condition is true.

• If the condition is false, nothing is done.

• The syntax for a one-way if statement is:

if (boolean-expression) {

statement(s);

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

10

Page 11: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Notes

• The boolean expression is enclosed in parentheses.

• The block braces can be omitted if they enclose a single statement.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

11

if i > 0 { System.out.println("i is positive");

}

(a) Wrong (b) Correct

if (i > 0) {

System.out.println("i is positive");

}

if (i > 0) { System.out.println("i is positive");

}

(a)

Equivalent

(b)

if (i > 0)

System.out.println("i is positive");

Page 12: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Example

•Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If the number is divisible by 2, print HiEven.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

12

Page 13: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Example Solution

import java.util.Scanner;

public class SimpleIfDemo {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter an integer: ");int number = input.nextInt();

if (number % 5 == 0)System.out.println("HiFive");

if (number % 2 == 0)System.out.println("HiEven");

}

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

13

Page 14: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s Code

• Write a program that prompts the user to enter a string. If the string length is divisible by 2, print ‘EvenString’.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

14

Page 15: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Two-way if Statements(if – else)

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

15

Page 16: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Two-way if Statements

• A two-way if-else statement executes an action if the condition is true and another action if the condition is false.• The syntax for a two-way if-else statement is:if (boolean-expression) {statement(s); // for the true case} else {statement(s); // for the false case}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

16

Page 17: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Two-way if Statements

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

17

Boolean

Expression

false true

Statement(s) for the false case Statement(s) for the true case

Page 18: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Example

if (radius >= 0) {

area = radius * radius * 3.14159;

System.out.println("The area for the "

+ "circle of radius " + radius +

" is " + area);

}

else {

System.out.println("Negative input");

}

Introduction to OOP with Java - AbuKhleiFwww.abukhleif.com

18

Page 19: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s Code

• Write a program that prompts the user to enter his/her name and birth year. If the user is 18 years or older print ‘Welcome [user-name]’. Otherwise, print ‘Sorry, you are not eligible yet, please come back in (18-[user-age]) years’.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

19

Page 20: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Multi-way if Statements(Nested if)

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

20

Page 21: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Multi-way if Statements

• An if statement can be inside another if statement to form a nested if statement.• Example:if (i > k) {if (j > k)System.out.println(“i and j are greater than k”);}elseSystem.out.println(“i is less than or equal to k”);

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

21

Page 22: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Multiple Alternative if Statements - Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

22

score >= 90

true

grade = 'A'

false

score >= 80

true

grade = 'B'

false

score >= 70

rue

grade = 'C'

score >= 60

true

grade = 'D'

false

false

grade = 'F'

Page 23: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

• Equivalent, but?!

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

23

if (score >= 90.0) grade = 'A';

else

if (score >= 80.0)

grade = 'B';

else

if (score >= 70.0)

grade = 'C';

else

if (score >= 60.0)

grade = 'D';

else

grade = 'F';

Equivalent

if (score >= 90.0)

grade = 'A';

else if (score >= 80.0)

grade = 'B';

else if (score >= 70.0)

grade = 'C';

else if (score >= 60.0)

grade = 'D';

else

grade = 'F';

Multiple Alternative if Statements – Example Solution

Page 24: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s Code

• Write a full working program for the previous example, the mark should be entered by the user.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

24

Page 25: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Note

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

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

25

int i = 1; int j = 2;

int k = 3;

if (i > j)

if (i > k)

System.out.println("A");

else

System.out.println("B");

(a)

Equivalent

(b)

int i = 1;

int j = 2;

int k = 3;

if (i > j)

if (i > k)

System.out.println("A");

else

System.out.println("B");

Page 26: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Note, cont.

• Nothing is printed from the preceding statement. To force the elseclause to match the first if clause, you must add a pair of braces: int i = 1;

int j = 2;

int k = 3;

if (i > j) {

if (i > k)

System.out.println("A");

}

else

System.out.println("B");

This statement prints B.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

26

Page 27: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Common Error

• Adding a semicolon at the end of an if clause is a common mistake.

if (radius >= 0);{

area = radius*radius*PI;System.out.println(

"The area for the circle of radius " +radius + " is " + area);

}

• This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error.

• This error often occurs when you use the next-line block style.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

27

Page 28: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Tip

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 28

if (number % 2 == 0) even = true;

else

even = false;

(a)

Equivalent

boolean even

= (number % 2 == 0);

(b)

Page 29: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Tip

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 29

if (even == true)

System.out.println(

"It is even.");

(a)

Equivalent if (even)

System.out.println(

"It is even.");

(b)

Page 30: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

switch Statements

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

30

Page 31: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

switch Statements

•Nested if can be used to write code for multiple conditions.•However, it makes the program difficult to read.

•A switch statement simplifies coding for multiple conditions.

•A switch statement executes statements based on the value of a variable or an expression.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

31

Page 32: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

32

switch (switch-expression) {

case value1: statement(s)1;

break;

case value2: statement(s)2;

break;

case valueN: statement(s)N;

break;

default: statement(s)-for-default;

}

The switch-expressionmust yield a value of char, byte, short, or int type and must always be enclosed in parentheses.

The value1, ..., and valueN must

have the same data type as the

value of the switch-expression.

The resulting statements in the

case statement are executed when

the value in the case statement

matches the value of the switch-

expression. Note that value1, ...,

and valueN are constant

expressions, meaning that they

cannot contain variables in the

expression, such as 1 + x.

switch Statements Rules

Page 33: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

33

switch (switch-expression) {

case value1: statement(s)1;

break;

case value2: statement(s)2;

break;

case valueN: statement(s)N;

break;

default: statement(s)-for-default;

}

switch Statements Rules

The keyword break is optional,

but it should be used at the end of each case in order to terminate the remainder of the switchstatement. If the break statement is not present, the next casestatement will be executed.

The default case, which is

optional, can be used to perform

actions when none of the

specified cases matches the

switch-expression.

Page 34: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

switch Statement Rules

• The case statements are executed in sequential order, but the order of the cases (including the default case) does not matter.

• However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

34

Page 35: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s CodeProblem: Chinese Zodiac

• Write a program that prompts the user to enter a year and displays the animal for the year.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

35

rat

ox

tiger

rabbit

dragon

snake horse

sheep

monkey

rooster

dog

pig 0: monkey

1: rooster

2: dog 3: pig

4: rat

5: ox 6: tiger

7: rabbit

8: dragon

9: snake

10: horse

11: sheep

year % 12 =

Page 36: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Ternary Operator(Conditional Operator)

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

36

Page 37: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Ternary Operator (Conditional Operator)

•A conditional operator evaluates an expression based on a condition.• The syntax is:

booleanExpression ? expressionIfTrue : expressionIfFalse;

• The result of the conditional operator is expressionIfTrue if booleanExpression is true, otherwise the result is expressionIfFalse.• Example: max = (num1 > num2) ? num1 : num2;

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

37

Page 38: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Let’s Code

• Rewrite the following if-else statement using the ternary operator:

if (num % 2 == 0)

System.out.println(num +

“ is even”);

else

System.out.println(num +

“ is odd”);

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

38

Page 39: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

TasksAll tasks should be well-documented, well-designed, and well-styled.

Page 40: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Task 01

• (Sort three integers) Write a program that prompts the user to enter three integers and display the integers in non-decreasing order.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

40

Page 41: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Task 02

• (Find the number of days in a month) Write a program that prompts the user to enter the month and displays the number of days in the month.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

41

Page 42: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Task 03

• (Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to JOD.• Prompt the user to enter 0 to convert from U.S. dollars to JOD and 1 to

convert from JOD to U.S. dollars.

• Prompt the user to enter the amount in U.S. dollars or JOD to convert it to JOD or U.S. dollars, respectively.

• Sample runs in next slide.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

42

Page 43: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Task 03 – Sample Runs

Enter the exchange rate from dollars to JOD: 0.71Enter 0 to convert dollars to JOD and 1 vice versa: 0Enter the dollar amount: 100$100 is 71 JOD

Enter the exchange rate from dollars to JOD: 0.71Enter 0 to convert dollars to JOD and 1 vice versa: 5Enter the JOD amount: 1000010000 JOD is $14100

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

43

Page 44: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Tasks Submission• Submit a zipped file contains all the 3 .java files, 1 file for each task.

• Name your zipped file as follow [Lect3_YourName.zip].

• Upload your zipped file to the Facebook group.

• Submission due: Monday, Sep 18 - 10:00 PM

• Late submission will not be reviewed by the instructor.

• Public solutions upload goal is to share knowledge, you can see other’s solutions, but, please, don’t cheat yourself!

• Don’t forget, all tasks should be well-documented, well-designed, and well-styled.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

44

Page 45: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

Test Yourself

•Answer all questions (exclude 3.1 & 3.2):

http://www.cs.armstrong.edu/liang/interactivequiz/public_html/Chapter3.html

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

45

Page 46: Introduction to OOP with Java - Abu Khleif · Notes •The boolean expression is enclosed in parentheses. ... byte, short, or int type and must always be enclosed in parentheses

- Liang, Introduction to Java Programming 10/e- Eng. Asma Abdel Karim Computer Engineering Department, JU Slides.

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

References:

End of Lecture =D