1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture An introduction to computer science using java (2nd Edition): Chapter 4 - S.N. Kamin, D. Mickunas, E. Reingold

29 24 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 504,21 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 4 - Decision making. In this chapter we will: discuss the use of decision making in computer programming, describe the use of the Java if and switch statements, describe the use of Boolean expressions in Java if statements, discuss the use of integer selector variables in Java switch statements.

Trang 1

   

Chapter 4

Decision Making

Lecture Slides to Accompany

An Introduction to Computer Science

Using Java (2nd Edition)

by S.N Kamin, D Mickunas, E Reingold

Trang 2

   

Chapter Preview

In this chapter we will:

• discuss the use of decision making in

• discuss the use of integer selector variables

in Java switch statements

Trang 4

   

Trang 5

   

Relational Operators

< < less than

> > greater than

<= less than or equal to

>= greater than or equal to

= == equal to

!= not equal to

Trang 6

   

if Statement with Optional else

• An if statement may have an optional else clause that will only be executed when the condition is false

Trang 7

   

Trang 8

quotient = secondNumber / firstNumber;

remainder = secondNumber % firstNumber;

}

else {

quotient = firstNumber / secondNumber;

remainder = firstNumber % secondNumber;

}

Trang 9

else if (condition-2) statement-2;

else

statement-3;

Trang 10

   

Trang 11

else

statement-2;

Trang 12

   

Trang 13

   

Trang 14

else if (condition-2) statement-1;

Trang 15

   

Boolean Operators

• Logical “and” (conjunction)

– true only when both expressions are true

(MINIMUM_WAGE <= wages) && (wages <= MAXIMUM_WAGE)

• Logical inclusive “or” (disjunction)

– true when either or both expressions are true

(MINIMUM_WAGE < wages) || (MINIMUM_WAGE == wages)

• Logical exclusive “or”

– true when exactly one of the expressions is true

(MINIMUM_WAGE < wages) ^ (MINIMUM_WAGE == wages)

• Logical “not” (negation)

(MINIMUM_WAGE != wages)

Trang 16

   

Boolean Operator Truth Table

Trang 18

   

Complicated Boolean Expressions

boolean isLeapYear = ((year % 4) == 0)

&& ((year % 100) != 0)

|| ((year % 400) == 0); // Assume all months have 31 days

dayNumber = (month - 1) * 31 + day;

// Correct for months beyond February

Trang 19

   

Trang 20

   

Trang 21

   

Comparing Strings

• Comparison is done by comparing strings by-character left to right, the first character that differs

character-dictates which string is smaller lexi co graphically

• What if one string is a prefix of the other?

– The prefix is smaller than the longer string

• How do upper- and lower-case characters compare?

– If a case-sensitive comparison is used lower-case

is always less than upper case

• How do special characters (e.g %) compare?

– Decided by the character ASCII representation

Trang 22

lexigraphically, ignoring caseconsiderations

boolean equals(String a) Returns true is the strings

are equal and falseotherwise

Boolan equalsIgnoreCase(String a) Returns true is the strings

are equal, ignoring case, andfalse otherwise

Trang 24

   

Trang 26

   

Symbolic Constants in switch Statements

final int

SUNDAY = 1, MONDAY = 2, TUESDAY = 3,

WEDNESDAY = 4, THURSDAY = 5, FRIDAY = 6,

SATURDAY = 7;

ind d;

switch (d) {

case SUNDAY: out.print(“Sunday”); break;

case MONDAY: out.print(“Monday”); break;

case TUESDAY: out.print(“Tuesday”); break;

case WEDNESDAY: out.print(“Wednesday”); break;

case THURSDAY: out.print(“Thursday”); break;

case FRIDAY: out.print(“Friday”); break;

case SUNDAY: out.print(“Sunday”); break;

}

Trang 27

out.println(“C.S meets at 9:00 today”);

out.println(“Math meets at 10:00 today”);

break;

case TUESDAY:

case THURSDAY:

out.println(“English meets at 9:00 today”);

out.println(“Chemistry meets at 10:00 today”);

Trang 28

   

Comparing switch and if statements

• switch statement

switch (expression) {

case value-1: statement-1; break;

case value-2: statement-2; break;

else if (switchValue == value-2) statement-2;

…else if (switchValue == value-i) statement-i;

else statement-(i+1);

Trang 29

   

Building Classes with Multiple Methods

public class Classname {

// Author, date, explanation

declarations of public variables

public void methodName ( parameters ) { declarations of “local” variables

executable statements with relevant comments

Ngày đăng: 15/05/2020, 22:54

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN