© 2013 ken howard, southern methodist university cse 1341 topic 4 hex/binary/decimal conversion...

64
© 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Upload: lilian-cunningham

Post on 28-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 2013 Ken Howard, Southern Methodist University

CSE 1341Topic 4

Hex/Binary/Decimal ConversionControl Statements Part II

Page 2: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Summary of topics covered:• for repetition statement• do…while repetition statement • switch multiple-selection statement• break statement• continue statement• Logical operators• Pseudocode Practice• Number conversions• Binary• Hexadecimal• Decimal

Page 3: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

We previously learned…

• if• if…else• while

More control statements…

• for repetition statement• do…while repetition statement • switch multiple-selection statement• break statement• continue statement

More Control Statements

Page 4: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• Counter-controlled repetition requires– a control variable (or loop counter)– the initial value of the control variable– the increment (or decrement) by which the

control variable is modified each time through the loop (also known as each iteration of the loop)

– the loop-continuation condition that determines if looping should continue.

Repetition with Counters

Page 5: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Counter Controlled Repetition while

Page 6: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Counter Controlled Repetition for

Page 7: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

for ( int counter = 1 ; counter <= 10 ; counter++) { //block of code that will loop }

forkeyword

counter variable

starting value of counter variable

required semicolons

required semicolons

incrementsthe

counter variable

loop continuation

condition

Page 8: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• All three expressions in a for header are optional. – If the loopContinuationCondition is omitted, the condition is always

true, thus creating an infinite loop. – You might omit the initialization expression if the program

initializes the control variable before the loop. – You might omit the increment if the program calculates it with

statements in the loop’s body or if no increment is needed.

• The increment expression in a for acts as if it were a standalone statement at the end of the for’s body, so

counter = counter + 1counter += 1++countercounter++

are equivalent increment expressions in a for statement.

for Repetition Statement

Page 9: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• a)Vary the control variable from 1 to 100 in increments of 1.

for ( int i = 1; i <= 100; i++ )

• b)Vary the control variable from 100 to 1 in decrements of 1.

for ( int i = 100; i >= 1; i-- )

• c)Vary the control variable from 7 to 77 in increments of 7.

for ( int i = 7; i <= 77; i += 7 )

for Repetition Statementexamples:

Page 10: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• d)Vary the control variable from 20 to 2 in decrements of 2.

for ( int i = 20; i >= 2; i -= 2 )

• e)Vary the control variable over the values 2, 5, 8, 11, 14, 17, 20.

for ( int i = 2; i <= 20; i += 3 )

• f)Vary the control variable over the values 99, 88, 77, 66, 55, 44, 33, 22, 11, 0.

for ( int i = 99; i >= 0; i -= 11 )

for Repetition Statementexamples:

Page 11: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

for Repetition Statementexample:

Page 12: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Compound interest application

A person invests $1000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts:

a = p (1 + r)n

where

p is the original amount invested (i.e., the principal)r is the annual interest rate (e.g., use 0.05 for 5%)n is the number of yearsa is the amount on deposit at the end of the nth year.

for Repetition StatementSample Problem:

Page 13: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• Loop performs the indicated calculation for each of the 10 years the money remains on deposit.

• Java treats floating-point constants like 1000.0 and 0.05 as type double.

• Java treats whole-number constants like 7 and -22 as type int.

Strategy and tips…

Page 14: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 15: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

String substitution with printf

System.out.printf(“%s%20s\n”,”Year”,”Amount on deposit”);

%s

%20s

%-20s

Insert String at this point

Insert right justified String at this point Fill will spaces (if necessary) to occupy 20 character spaces.

Insert left justified String at this point. Fill will spaces (if necessary) to occupy 20 character spaces.

Page 16: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Integer substitution with printf

System.out.printf(“%4d%,20.2f\n”,year,amount);

%d

%4d

%-,4d

Insert integer at this point

Insert right justified integer at this point fill will spaces (if necessary) to occupy 4 spaces.

Insert left justified integer at this point fill will spaces (if necessary) to occupy 4 spaces. Format with commas.

%,4d Insert right justified integer at this point fill will spaces (if necessary) to occupy 4 spaces. Format with commas at thousandth’s place.

Page 17: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Floating point substitution with printf

System.out.printf(“%4.2f%”,price);

%5.2f

%-,5.2f

Include up to two digits to the right of the decimal. Fill with zeros, round if needed.

All of the previous formatting, left justified.

%,5.2f Include commas at the thousanth’s.

%5f Insert double or float at this point with up to 5 digits to the left of the decimal and six (default) digits to the right of the decimal. Round if needed.

Page 18: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Math.pow(x, y) calculates the value of x raised to the yth power. The method receives two double arguments and returns a double value.

• Classes provide methods that perform common tasks on objects. • Most methods must be called on a specific object. • Many classes also provide methods that perform common tasks and do not require objects. These

are called static methods. • Java does not include an exponentiation operator—Math class static method pow can be used

for raising a value to a power. • You can call a static method by specifying the class name followed by a dot (.) and the method

name, as in

• ClassName.methodName( arguments )

Utilitarian Class Math

double myResult = Math.pow(10,2);

Page 19: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• The do…while repetition statement is similar to the while statement.

• In the while, the program tests the loop-continuation condition at the beginning of the loop, before executing the loop’s body; if the condition is false, the body never executes.

• The do…while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once.

• When a do…while statement terminates, execution continues with the next statement in sequence.

do..while Repetition Statement

Page 20: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 21: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• switch multiple-selection statement performs different actions based on the possible values of a constant integral expression of type byte, short, int or char.

switch Multiple Selection Statement

Page 22: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 23: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 24: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 25: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 26: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 27: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• Scanner method hasNext determine whether there is more data to input. This method returns the boolean value true if there is more data; otherwise, it returns false.

• As long as the end-of-file indicator has not been typed, method hasNext will return true.

switch Multiple Selection Statement

Page 28: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• The switch statement consists of a block that contains a sequence of case labels and an optional default case.

• The program evaluates the controlling expression in the parentheses following keyword switch.

• The program compares the controlling expression’s value (which must evaluate to an integral value of type byte, char, short or int) with each case label.

• If a match occurs, the program executes that case’s statements.

• The break statement causes program control to proceed with the first statement after the switch.

switch Multiple Selection Statement

Page 29: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

© 1992-2010 by Pearson Education, Inc. All Rights Reserved.

• switch does not provide a mechanism for testing ranges of values—every value must be listed in a separate case label.

• Note that each case can have multiple statements.• switch differs from other control statements in that it does not

require braces around multiple statements in a case. • Without break, the statements for a matching case and

subsequent cases execute until a break or the end of the switch is encountered. This is called “falling through.”

• If no match occurs between the controlling expression’s value and a case label, the default case executes.

• If no match occurs and there is no default case, program control simply continues with the first statement after the switch.

switch Multiple Selection Statement

Page 30: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• The break statement, when executed in a while, for, do…while or switch, causes immediate exit from that statement.

• Execution continues with the first statement after the control statement.

• Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch.

break and continue Statements

Page 31: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 32: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

• Evaluates the loop-continuation test immediately after the continue statement executes.

continue Statementwith while or do..while:

int i = 1;while(int i < 10) {

if(i%2 == 0) System.out.print(i);

else continue;

System.out.print("X");}

Page 33: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

• The increment expression executes, then the program evaluates the loop-continuation test.

continue Statementwith for

for(int i = 1; i < 10; i++){

i++; if(i%2 == 0)

System.out.print(i); else

continue; System.out.print("X");

}

Page 34: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 35: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Java’s logical operators enable you to form more complex conditions by combining simple conditions.

• The logical operators are – && (conditional AND)– || (conditional OR)– & (boolean logical AND)– | (boolean logical inclusive OR)– ^ (boolean logical exclusive OR)– ! (logical NOT).

• [Note: The &, | and ^ operators are also bitwise operators when they are applied to integral operands.]

Logical Operators

Page 36: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Ensures that two conditions are both true before choosing a certain path of execution.

• Java evaluates to false or true all expressions that include relational operators, equality operators or logical operators.

Conditional AND&&

short circuits

Page 37: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Ensures that either or both of two conditions are true before choosing a certain path of execution.

• Operator && has a higher precedence than operator ||. • Both operators associate from left to right.

Conditional ORII

Which will short circuit?

Page 38: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Identical to the && and || operators, except that the & and | operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation).

• This is useful if the right operand of the boolean logical AND or boolean logical inclusive OR operator has a required side effect—a modification of a variable’s value.

Boolean Logical Operators| &

Page 39: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Is true if and only if one of its operands is true and the other is false.

• If both are true or both are false, the entire condition is false.

• This operator is guaranteed to evaluate both of its operands.

Boolean Logical Exclusive OR^

Page 40: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Reverses the meaning of a condition. • The logical negation operator (!) is a unary operator

that has only a single condition as an operand. • The logical negation operator is placed before a

condition to choose a path of execution if the original condition (without the logical negation operator) is false.

• In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational or equality operator.

Logical NOT!

Page 41: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Boolean substitution with printf

int x = 5;int y = 10;boolean myBoolean = (x < y);System.out.printf(“x < y is %b”,myBoolean);

%b Insert boolean (true or false at this point

Page 42: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 43: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 44: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 45: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II
Page 46: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Pseudocode is an informal language that helps you develop algorithms without having to worry about the strict details of Java language syntax.

• Particularly useful for developing algorithms that will be converted to structured portions of Java programs.

• Similar to everyday English.• Helps you “think out” a program before attempting to write it in a

programming language, such as Java. • You can type pseudocode conveniently, using any text-editor program. • Carefully prepared pseudocode can easily be converted to a corresponding

Java program. • Pseudocode normally describes only statements representing the actions

that occur after you convert a program from pseudocode to Java and the program is run on a computer. – e.g., input, output or calculations.

Pseudocode

Page 47: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Write pseudocode for a routine that will calculate the average age of the students

in this class

Page 48: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Implement a simple cash register program for a Dallas toy store. A customer will bring a basket of toys to the cashier to check out. Each item has a price tag with the price on it. All items of the same type will have the same price. The system should prompt the cashier for the quantity and price of each product.

When there are no more items to enter, the cashier will enter a quantity of zero. When this happens, the system will calculate and display the subtotal, sales tax (8.25%) and grand total owed.

The customer hands payment to the cashier, who enters the payment amount in the system. The system calculates and displays change due to the customer.

Together we’ll convert the pseudocodeinto a Java application.

Write pseudocode for the process of handling a sales transaction.

Page 49: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Numbering Systems

16Decmal32

64128

• Decimal• Base 10

• Binary• Base 2• On/off 0/1• Accommodates electronic pulses

• Hexadecimal• Base 16• Accommodates large numbers with fewer digits than Base 10• Uses digits 0-9 then A-F to represent decimal 0-16

Page 50: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

0 1 1 0 1 0 1

01001010011000010111011001100001

etc…

ASCII Translator Java

Page 51: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Translation

16Decmal32

64128

Morse Code (e.g.) converts pulses to letters

The next step is to organize the letters into words.

Page 52: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Translation and Computers

16Decmal32

64128

Operating systems require a translation system.ASCII is a popular translation system.

Binary pulses are translated to characters.

Page 53: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Translation and Computers

16Decmal32

64128

Hexadecimal numbers are more efficient.More information can be communicated in each pulse.

Shorter word length allows faster communication.

Page 54: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Binary NumbersBase 2

8 Bits = 1 Byte

163264128 128 64 32 16 8 4 2 1

Each bit contains 0 or 1 (off or on)

What is the highest possible value in a byte?

Page 55: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Value?

163264128 128 64 32 16 8 4 2 11 0 1 0 1

Page 56: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Converting Decimal to Binary

1664128

75

75 / 2 = 37 rem 137 / 2 = 18 rem 118 / 2 = 9 rem 09 / 2 = 4 rem 14 / 2 = 2 rem 02 / 2 = 1 rem 01 / 2 = 0 rem 1

1 0 0 1 0 1 1

Converting to Base 2(binary)

Page 57: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Hexadecimal NumbersBase 16

Shorthand for expressing numbers

1632641280123456789ABCDEF

0123456789ABCDEF

0123456789ABCDEF

0123456789ABCDEF

Each digit contains 0-F (values 0 – 15)

Page 58: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Hexadecimal NumbersBase 16

Shorthand for expressing numbers

163264128

65,536’s

4096’s 256’s 16’s 1’s

Each bit contains 0 – F

Page 59: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

65,536’s

4096’s 256’s 16’s 1’s

F 2 A 4 C

Page 60: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Converting Decimal to Hex

1664128

75

75 / 16 = 4 rem 11 (B)4 / 16 = 0 rem 4

4 B

Converting to Base 16

(binary)

Page 61: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

Converting Decimal to Hex

1664128

1962

1962 / 16 = 122 rem 10 (A) 122 / 16 = 7 rem 10 (A) 7 / 16 = 0 rem 7

7 A A

Converting to Base 16

(binary)

Page 62: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Selection is implemented in one of three ways:– if statement (single selection)– if…else statement (double selection)– switch statement (multiple selection)

• The simple if statement is sufficient to provide any form of selection—everything that can be done with the if…else statement and the switch statement can be implemented by combining if statements.

Summary

Page 63: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Repetition is implemented in one of three ways:– while statement– do…while statement– for statement

• The while statement is sufficient to provide any form of repetition. Everything that can be done with do…while and for can be done with the while statement.

Summary

Page 64: © 2013 Ken Howard, Southern Methodist University CSE 1341 Topic 4 Hex/Binary/Decimal Conversion Control Statements Part II

• Combining these results illustrates that any form of control ever needed in a Java program can be expressed in terms of– sequence– if statement (selection)– while statement (repetition)

and that these can be combined in only two ways—stacking and nesting.

Summary