iti 1120 lab #3 tracing and branching contributors: g. arbez, m. eid, d. inkpen, a. williams, d....

16
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Upload: bonnie-lucas

Post on 21-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

ITI 1120Lab #3

Tracing and Branching

Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Page 2: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Exercise 1

/* Lab 2, Exercise 1. */class Tracing // Replace 'Template' with your own algorithm name.{ // the main method contains all interactions with the user public static void main (String[] args) { // prompt the user to enter 3 numbers

System.out.print( "Enter three floating-point values separated by spaces: " );

// receive the numbers from keyboard and save in 3 variablesdouble number1 = input.nextDouble(); // read first doubledouble number2 = input.nextDouble(); // read second doubledouble number3 = input.nextDouble(); // read third double

// determine the maximum valuedouble result = maximum( number1, number2, number3 );

// display maximum value System.out.println( "Maximum is: " + result );

} public static void maximum(double x, double y,double z) { double maximumValue = x; // assume x is the largest to start

// determine whether y is greater than maximumValue if ( y > maximumValue ) maximumValue = y;

// determine whether z is greater than maximumValue if ( z > maximumValue ) maximumValue = z;

return maximumValue;

}} // Don't remove this brace bracket!

Program Memory Terminal/Output Screen

number1

Number2

Number3

Result

Working Memory

x

y

z

maximumValue

Enter three floating-point values separated by spaces:23.6 12.9 105.2Maximum is: 105.2

Page 3: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Exercise 1 – Cont’d

• Trace the main method using the tabular model

• Trace the problem solving method using the tabular model

Page 4: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Boolean Expressions

• Evaluate to true or false• Translations from pseudocode to Java for:

Pseudocode Java = (not a Boolean expression)AND &&OR ||NOT !A = B A == BA ≤ B A <= BA B A >= BA B A != B

Page 5: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Boolean Expressions, Example 1

• Write a test that returns TRUE if integer I is odd; the test should return FALSE otherwise.

Algorithm: Java:// assume i has a valueboolean odd;if (i % 2 == 0){ odd = false;}else{ odd = true;}

odd TRUE

odd FALSE

false trueI mod 2 = 0 ?

Page 6: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Boolean Expressions:

• Write a test that returns TRUE if integer I is a multiple of positive integer K; the test should return FALSE otherwise.

Algorithm:

multiple FALSE

multiple TRUE

false trueI mod K = 0 ?

Java:// assume i, k have valuesboolean multiple;if (i % k == 0){ multiple = true;}else{ multiple = false;}

Page 7: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

AND and OR

• Used for combining conditions• Use brackets to make sure compound

expressions mean what you want them to mean. • Anywhere our pseudocode language calls for a

"test" you may use ANY Boolean expression• What is the value of the following expressions?

((room = STE0131) OR (room = STE2052)) AND (Lab = ITI1220)

(I am at home) AND (I am in the office)

(I am at home) OR (I am in the office)

TRUE

TRUE

FALSE

Page 8: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Boolean Expressions:

• Write a test that returns TRUE if x is between 10 and 20 (inclusive); the test should return FALSE otherwise

Algorithm:

inRange FALSE

inRange TRUE

false true

X 10 ANDX 20 ?

Java:// assume x has a valueboolean inRange;if ( (x>=10) && (x<=20) ){ inRange = true;}else{ inRange = false;}

Page 9: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

AND versus OR

• In the last slide:– We used: ((x>=10) && (x<=20)) to

test whether x is between 10 and 20.

• What if we used OR || instead of AND &&– Suppose x is 7. – If we had ((x>=10) || (x<=20)):

x<=20 is TRUE, and so the entire expression is TRUE: but x is not between 10 and 20.

Page 10: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Boolean Expressions

• Write a test that is TRUE if B's value is in between A's value and C's value (but, we don't know whether A is bigger than C or vice versa).

false true

(((B A) AND (B C ))OR

((B C) AND (B A)))

Algorithm: Java:

if (((b>=a) && (b<=c)) || ((b>=c) && (b<=a))){ // b is between a and c}else{ // b is outside range}

Page 11: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

11

int i = 10, j = 15, k = 20;double x = 10.0, y = 3.333333, z = 100.0;

Compute the following logical expressions

i < j || j < k && x <= y

(i / 3) == y

(x / 3) == y

!(x != i)

Example 2

Page 12: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

12

Example 3

Expression Value

(X > 0) AND (NOT (Y = 0))TRUE

(X > 0) AND ((X < Y) OR (Y = 0))TRUE

(NOT (X > 0)) OR ((X < Y) AND (Y = 0))FALSE

NOT ((X > 0) OR ((X < Y) AND (Y = 0)))FALSE

Suppose X = -1 and Y = 5.

Can you translate them to Java syntax?

Page 13: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Example 4 - Branching

• Design the algorithm that receives an integer number and returns -1 if the argument is less than zero, returns +1 if the argument is greater than zero and returns zero if the argument is zero.

13

Page 14: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Algorithm Design

GIVEN: num (an integer)

INTERMEDIATE: None

RESULT: sign (sign of the number)

HEADER: sign Sign(num)

BODY:

14

Page 15: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Exercise 5

• It is decided to base the fine for speeding in a built up area as follows - 50 dollars if speed is between 31 and 40 mph, 75 dollars if the speed is between 41 and 50 mph and 100 dollars if the speed is above 50 mph.– Design the problem solving algorithm that

receives the speed and returns the fine– Design the main algorithm for this software–  Translate the algorithms to Java code– Trace your algorithm with the following input:

• When the user inputs 50 mph

15

Page 16: ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot

Exercise 6

• Translate the following algorithm to Java