ITI 1120 Lab 3 Tracing and Branching Contributors

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

Exercise 1 Terminal/Output Screen Program Memory /* 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 variables double number 1 = input. next. Double(); // read first double number 2 = input. next. Double(); // read second double number 3 = input. next. Double(); // read third double Enter three floating-point values separated by spaces: 23. 6 12. 9 105. 2 Maximum is: 105. 2 Working Memory number 1 Number 2 // determine the maximum value double result = maximum( number 1, number 2, number 3 ); Number 3 // display maximum value System. out. println( "Maximum is: " + result ); Result } public static void maximum(double x, double y, double z) { double maximum. Value = x; // assume x is the largest to start // determine whether y is greater than maximum. Value if ( y > maximum. Value ) maximum. Value = y; // determine whether z is greater than maximum. Value if ( z > maximum. Value ) maximum. Value = z; return maximum. Value; } } // Don't remove this brace bracket! x y z maximum. Value

Exercise 1 – Cont’d • Trace the main method using the tabular model • Trace the problem solving method using the tabular model

Boolean Expressions • Evaluate to true or false • Translations from pseudocode to Java for: Pseudocode AND OR NOT A=B A≤B A B Java = (not a Boolean expression) && || ! A == B A <= B A >= B A != B

Boolean Expressions, Example 1 • Write a test that returns TRUE if integer I is odd; the test should return FALSE otherwise. Algorithm: Java: I mod 2 = 0 ? false odd TRUE true odd FALSE // assume i has a value boolean odd; if (i % 2 == 0) { odd = false; } else { odd = true; }

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: Java: I mod K = 0 ? false multiple FALSE true multiple TRUE // assume i, k have values boolean multiple; if (i % k == 0) { multiple = true; } else { multiple = false; }

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 = STE 0131) OR (room = STE 2052)) AND (Lab = ITI 1220) TRUE (I am at home) OR (I am in the office) TRUE (I am at home) AND (I am in the office) FALSE

Boolean Expressions: • Write a test that returns TRUE if x is between 10 and 20 (inclusive); the test should return FALSE otherwise Algorithm: false in. Range FALSE Java: X 10 AND X 20 ? true in. Range TRUE // assume x has a value boolean in. Range; if ( (x>=10) && (x<=20) ) { in. Range = true; } else { in. Range = false; }

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.

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). Algorithm: Java: (((B A) AND (B C )) OR ((B C) AND (B A))) false true if (((b>=a) && (b<=c)) || ((b>=c) && (b<=a))) { // b is between a and c } else { // b is outside range }

Example 2 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) 11

Example 3 Suppose X = -1 and Y = 5. Expression (X > 0) AND (NOT (Y = 0)) (X > 0) AND ((X < Y) OR (Y = 0)) (NOT (X > 0)) OR ((X < Y) AND (Y = 0)) NOT ((X > 0) OR ((X < Y) AND (Y = 0))) Can you translate them to Java syntax? 12 Value TRUE FALSE

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

Algorithm Design GIVEN: num (an integer) INTERMEDIATE: None RESULT: sign (sign of the number) HEADER: sign Sign(num) BODY: 14

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

Exercise 6 • Translate the following algorithm to Java
- Slides: 16