+ All Categories
Home > Documents > CS 180 Recitation 9/20/07 - 9/21/07

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

Date post: 02-Jan-2016
Category:
Upload: brent-ford
View: 37 times
Download: 6 times
Share this document with a friend
Description:
CS 180 Recitation 9/20/07 - 9/21/07. Announcements. Exam 1 is Tuesday, September 25 th 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 - PowerPoint PPT Presentation
25
CS 180 Recitation 9/20/07 - 9/21/07
Transcript
Page 1: CS 180 Recitation 9/20/07 - 9/21/07

CS 180

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

Page 2: 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

Page 3: CS 180 Recitation 9/20/07 - 9/21/07

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;

Page 4: CS 180 Recitation 9/20/07 - 9/21/07

Boolean Operators

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

Page 5: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 6: CS 180 Recitation 9/20/07 - 9/21/07

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));

Page 7: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 8: CS 180 Recitation 9/20/07 - 9/21/07

Operator Precedence Rules

Page 9: CS 180 Recitation 9/20/07 - 9/21/07

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;

Page 10: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 11: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 12: CS 180 Recitation 9/20/07 - 9/21/07

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;

}

Page 13: CS 180 Recitation 9/20/07 - 9/21/07

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>

}

Page 14: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 15: CS 180 Recitation 9/20/07 - 9/21/07

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”);

Page 16: CS 180 Recitation 9/20/07 - 9/21/07

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;

}

Page 17: CS 180 Recitation 9/20/07 - 9/21/07

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;

Page 18: CS 180 Recitation 9/20/07 - 9/21/07

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>

Page 19: CS 180 Recitation 9/20/07 - 9/21/07

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

Page 20: CS 180 Recitation 9/20/07 - 9/21/07

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);

}

Page 21: CS 180 Recitation 9/20/07 - 9/21/07

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”);}

Page 22: CS 180 Recitation 9/20/07 - 9/21/07

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;

}

Page 23: CS 180 Recitation 9/20/07 - 9/21/07

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 */

Page 24: CS 180 Recitation 9/20/07 - 9/21/07

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.

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

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; }}


Recommended