1 cs1001 lecture 15. 2 overview java programming java programming arrays arrays

Post on 18-Jan-2016

241 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

11

CS1001CS1001

Lecture 15Lecture 15

22

OverviewOverview

Java ProgrammingJava Programming ArraysArrays

33

GoalsGoals

Understand the basics of Java Understand the basics of Java programmingprogramming

Control Statements and ArraysControl Statements and Arrays

44

AssignmentsAssignments

Brookshear: Ch 4, Ch 5 (Read)Brookshear: Ch 4, Ch 5 (Read) Read linked documents on these Read linked documents on these

slides (slides will be posted in slides (slides will be posted in courseworks)courseworks)

http://http://java.sun.comjava.sun.com/docs/books/tutorial//docs/books/tutorial/

55

Objectives:Objectives: Learn about the Learn about the booleanboolean data type data type

Learn the syntax for Learn the syntax for if-elseif-else statements statements

Learn about relational and logical Learn about relational and logical operators, De Morgan’s laws, short-circuit operators, De Morgan’s laws, short-circuit evaluationevaluation

Learn when to use nested Learn when to use nested if-elseif-else statements, statements, if-else-ifif-else-if sequences, the sequences, the switchswitch statement statement

66

MidtermMidterm

History – Know people and the point of their History – Know people and the point of their contributions. Be prepared for short answers contributions. Be prepared for short answers (2-3 sentences)(2-3 sentences)

Hardware – Know the main parts of a Hardware – Know the main parts of a computer (processor, memory, etc). computer (processor, memory, etc). Understand that programs and data are Understand that programs and data are bothboth information and can be stored in some sort of information and can be stored in some sort of memory.memory.

Assembly – given a simple assembly Assembly – given a simple assembly language, write a short (very short) programlanguage, write a short (very short) program

Problem solving – identify a problem with a Problem solving – identify a problem with a given algorithmgiven algorithm

77

MidtermMidterm

Networking: TCP vs UDPNetworking: TCP vs UDP Good/bad design features: be able to Good/bad design features: be able to

name a few usability featuresname a few usability features Modern architecture: privacy, Modern architecture: privacy,

centralization (references on review centralization (references on review sheet)sheet)

Programming: given program x, what Programming: given program x, what does it output when run?does it output when run?

Find the error (Find the error (notnot syntax errors; logic syntax errors; logic errors only)errors only)

88

if-elseif-else Statement Statement

if ( <condition> ){ < statements >}else{ < other statements >}

if ( <condition> ){ < statements >}

else clause is optional

99

booleanboolean Data Type Data Type

George Boole (1815 - 1864)George Boole (1815 - 1864) Unified Logic and MathematicsUnified Logic and Mathematics booleanboolean variables may have variables may have

only two values, only two values, truetrue or or falsefalse.. You define You define booleanboolean fields or fields or

booleanboolean local variables the local variables the same way as other variables:same way as other variables:private boolean hasMiddleName;private boolean hasMiddleName;boolean isRolling = false;boolean isRolling = false;

boolean truefalse

Reserved words

1010

Boolean ExpressionsBoolean Expressions

In In if (<if (<conditioncondition> )> ) < <conditioncondition> is a > is a BooleanBoolean expression. expression.

A Boolean expression evaluates to A Boolean expression evaluates to either either truetrue or or false.false.

Boolean expressions are written using Boolean expressions are written using booleanboolean variables and variables and relationalrelational and and logicallogical operators. operators.

1111

Relational OperatorsRelational Operators

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

is equal to

is NOT equal to

1212

Relational Operators Relational Operators (cont’d)(cont’d) Apply to numbers or Apply to numbers or

charchars:s:if ( x <= y ) ...if ( x <= y ) ...if ( a != 0 ) ...if ( a != 0 ) ...if ( letter == ‘Y’ ) ...if ( letter == ‘Y’ ) ...

Do not use Do not use ==== or or !=!= with with doubledoubless because because they may have rounding they may have rounding errorserrors

double x = 7.0;double y = 3.5;if (x / y == 2.0) ...

May be false!

1313

Relational Operators Relational Operators (cont’d)(cont’d) Be careful using Be careful using ==== and and !=!= with with

objectsobjects (e.g., (e.g., StringStrings): they compare s): they compare references (addresses) rather than references (addresses) rather than values (the contents)values (the contents)

String cmd = console.readLine();String cmd = console.readLine();if ( cmd == "Help" ) ...if ( cmd == "Help" ) ...

Wrong!(always false)

1414

Relational Operators Relational Operators (cont’d)(cont’d) Use the Use the equalsequals method to compare method to compare

StringStrings:s:

ororif ( "Help".equals (cmd) ) ...

String cmd = console.readLine();

if ( cmd.equals ("Help") ) ...

1515

Relational Operators Relational Operators (cont’d)(cont’d) Use the Use the ==== or or !=!= operator with operator with

strings and other objects when you strings and other objects when you want to know whether or not this is want to know whether or not this is exactly the same objectexactly the same object..

Also use Also use ==== or or !=!= to compare to to compare to nullnull::

String text = file.readLine();

if ( text != null ) ...

1616

Logical OperatorsLogical Operators

&&&&, , ||||, , !!

and

or

not

1717

Logical Operators Logical Operators (cont’d)(cont’d) ( ( condition1condition1 &&&& condition2 condition2 )) is true is true

if and only if if and only if bothboth condition1condition1 andand condition2condition2 are true are true

( ( condition1condition1 |||| condition2 condition2 )) is true if is true if and only if and only if condition1condition1 oror condition2condition2 (or both) are true(or both) are true

!!condition1 condition1 is true if and only if is true if and only if condition1condition1 is false is false

1818

Logical Operators Logical Operators (cont’d)(cont’d) &&&&, , ||||, and , and !! obey the laws of formal obey the laws of formal

logic called logic called De Morgan's lawsDe Morgan's laws::

Example:Example:

if ( if ( !! ( x => ( x => --10 && x <= 10 ) ) ...10 && x <= 10 ) ) ...

if ( x < if ( x < --10 || x > 10 ) ... 10 || x > 10 ) ...

! (p && q) == ( !p || !q )

! (p || q) == ( !p && !q )

Easier to read

1919

Ranks of OperatorsRanks of Operators! -(unary) ++ -- (cast)

* / %

+ -

< <= > >= == !=

&&

||

if ( ( ( year % 4 ) == 0 ) && ( month == 2 ) ) ...

if ( year % 4 == 0 && month == 2 ) ...

Easier to read

2020

Short-Circuit Short-Circuit EvaluationEvaluationif (if (condition1condition1 &&&& condition2condition2) ...) ...

If If condition1condition1 is is falsefalse, , condition2condition2 is is not not evaluatedevaluated (the result is false anyway) (the result is false anyway)

if (if (condition1condition1 |||| condition2condition2) ...) ...

If If condition1condition1 is true, is true, condition2condition2 is is not not evaluatedevaluated (the result is true anyway) (the result is true anyway)

if ( x >= 0 && Math.sqrt (x) < 15.0) ...

Always OK: won’t get to sqrt if x < 0

2121

Nested Nested if-elseif-else

if ("forward".equals(cmd)){ if (slide >= numSlides) beep.play(); else slide++;}else{ if (slide <= 1) beep.play(); else slide--;}

2222

if-else-ifif-else-if

if (drinkSize.equals(”Large")){ price += 1.39;}else if (drinkSize.equals("Medium")){ price += 1.19;}else // if "Small" drink size{ price += 0.99;}

2323

Common Common if-elseif-else Errors Errors

if (...)if (...) if (...)if (...) statement1;statement1;elseelse statement2;statement2;......Missing braces

It is safer to always use braces in if-else

if (...)if (...)statement1;statement1;statement2;statement2;

......

if (...) if (...) ;;{{

statements;statements; ......}}

Extra semicolon

2424

TheThe switchswitchStatementStatement

switch (switch (expressionexpression)){{

case case value1value1:: ......

break;break;

case case value2value2:: ......

break;break; ...... ...... default:default: ...... break;break;}}

switch casedefaultbreak

Reserved words

Don’t forget breaks!

2525

The The CrapsCraps Applet Applet

2626

Review:Review: What are the possible values of a What are the possible values of a

booleanboolean variable? variable? What operators are used to compare What operators are used to compare

values of numbers and values of numbers and charchars?s? How can you test whether two How can you test whether two StringStrings s

have the same values? have the same values? Which binary operators have higher Which binary operators have higher

rank (are executed first), relational or rank (are executed first), relational or logical?logical?

2727

Review (cont’d):Review (cont’d): Can you have an Can you have an ifif statement statement

without an without an elseelse?? What are De Morgan’s laws?What are De Morgan’s laws? Explain short-circuit evaluation.Explain short-circuit evaluation. How long can an How long can an if-else-ifif-else-if sequence sequence

be?be? What are What are breakbreaks used for in s used for in switchswitch? ?

What happens if you forget one?What happens if you forget one? Can the same case in a Can the same case in a switchswitch have have

two breaks?two breaks?

top related