cs 180 recitation 9/20/07 - 9/21/07. announcements exam 1 is tuesday, september 25 th rooms: sec...

25
CS 180 Recitation 9/20/07 - 9/21/07

Post on 22-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

CS 180

Recitation 9/20/07 - 9/21/07

Announcements

Exam 1 is Tuesday, September 25th Rooms:

Sec 0101-0401 WTHR 104 Sec 0501-0801 MATH 175

Time: 7:00 pm If you have a conflict with the exam, contact

Dr. Van Zandt Sign the Academic Integrity Policy!! Mentoring with Armand Navabi

Mondays 7-9 pm in LWSN B134

Boolean Expressions

boolean is a primitive data type Two values: true or false Compares two values using a relational

operator <, >, ==, <=, >=, !=

Examples boolean isOK = true; boolean isLarge = num >= 100;

Boolean Operators

Boolean expressions can be combined using boolean operators And && Or || Not !

Boolean Operators bool1 && bool2

true if bool1 AND bool2 are true false if either bool1 or bool2 is false

bool1 || bool 2 true if either bool1 OR bool2 is true false if both bool1 AND bool2 are false

!bool1 true if bool1 is false false if bool1 is true

Boolean Operator Examples

What are the results of the above expressions?

int x = 20;

boolean isValid = x > 0 && x < 100;

int x = 5;

boolean isValid = x > 0 || x < -100;

int x = 5;

int y = 10;

boolean isDouble = (y == (x * 2));

Operator Precedence

Parenthesis should be used to indicate the order of operations

When there are no parenthesis, operator precedence is followed Higher precedence preformed before lower

precedence Equal precedence performed left-to-right, except

for unary operations which are performed right-to-left

Operator Precedence Rules

Operator Precedence Examples

What are the values of x, y, w, and z?

int x = 20 + 5 * 10;

int y = (20 + 5) * 10;

boolean w = true && true || false && false;

boolean z = true && (true || false) && false;

Selection Statements

Selection statements change the flow of control in a program

Use when the action to be preformed is dependent on the answer to a question E.g. If the value of x is less than zero, print an

error message. Otherwise add 1

If-else Statement

A branching statement used to choose between two actions

if ( <Boolean expression> )

<then block>

else

<else block>

The else branch is executed only when the if condition evaluates to false It is optional

If-else Statement To enclose multiple statements in a branch,

enclose the branch in bracesif ( <Boolean expression> )

{

line 1;

line 2;

line 3;

}

else

{

line 4;

line 5;

}

If-else Statement Single line blocks are not required to be

enclosed in braces, but it is a good ideaif ( <Boolean expression> )

<then block>

else

<else block>

Is equivalent to

if ( <Boolean expression> )

{

<then block>

}

else

{

<else block>

}

Multibranch If-else Statement

if ( <Boolean Expression 1> )

statement 1

else if ( <Boolean Expression 2> )

statement 2

else if ( <Boolean Expression 3> )

statement 3

else if ( <Boolean Expression 4> )

statement 4

else

Default Statement

If-else Statement Examplesint grade = 11;if (grade < 5 || grade > 10)

grade++;else

grade--;

int count = 70;if (count >= 90 && count < 100)

System.out.println(“A”);else if(count >= 80)

System.out.println(“B”);else if(count >= 70)

System.out.println(“C”);else if(count >= 60)

System.out.println(“D”);else

System.out.println(“F”);

If-else Statement Examples

What is the value of grade in each statement?

int grade = 5;if(grade < 5)

grade++;grade = 10;

int grade = 5;if(grade < 5){

grade++grade = 10;

}

If-else Statement Examples

What is the value of grade in each statement?

int grade = 5;if(grade < 5)

;grade = 10;

int grade = 5;if(grade > 0)

grade++;if(grade >= 5)

grade++;else

grade = 0;

Dangling else

Which if does the else match? Design decision. Java matches it to the most recent if.

if ( <Boolean expression> )

<then block> if ( <Boolean expression> )

<then block> else

<else block>

Switch Statements

Switch statements are a multiway branch which makes its decision based on an integer expression char, byte, short, or int

A list of cases is searched until a match is found

If no match is found, the default case is executed Optional

Switch Statement Syntax

The breaks and the default case label above are optional

What happens if we take the breaks out?

switch(Controlling_Expression){

case Label1:statement(s);<break;>

case Label2:statement(s);<break;>

<default:>statement(s);

}

Switch Statement Examples

What are the results of the above statements?

int section = 7;int room = 0;switch(section){

case 1:room = 101;break;

case 7:room = 102;break;

case 5:room = 103;break;

default:System.out.println(“invalid”);

}

int section = 7;int room = 0;switch(section){

case 1:room = 101;break;

case 7:room = 102;

case 5:room = 103;

default:System.out.println(“invalid”);}

Switch Statement Examples

What is the result of the above statement? Notice the empty case bodies

What if gender = ‘x’ ?

char gender = ‘f’switch(gender){

case ‘f’:case ‘F’:

System.out.println(“female”);break;

case ‘m’:case ‘M’:

System.out.println(“male”);break;

}

Unicode Encoding

The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages.

A UNICODE character takes up two bytes. ASCII characters take up one byte.char ch1 = ‘X’;

System.out.println(ch1); /* output X */

System.out.println((int) ch1); /* output 88 */

Character Processing

char ch1, ch2 = ‘X’;

System.out.println(“ASCII code of character

X is “ + (int) ‘X’);

System.out.println(“Character with ASCII code

88 is “ + (char) 88);

‘A’ < ‘c’

if ( ch1 < ‘A’ && ch2 == 99 )

System.out.println(“Done”);

Declaration and Initialization

Type conversion between int and char.

Comparison returns true because ASCII value of ‘A’ is 65 while that of ‘c’ is 99.

Can compare characters and numbers.

compareTo method for 2D pointsprivate double myx, myy;

public static int compareTo(double x, double y) {

double mydistance = Math.sqrt(Math.pow(myx, 2) + Math.pow(myy, 2)); double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); if(mydistance < distance) { return -1; } else if(mydistance == distance) { return 0; } else { return 1; }}