Download presentation
Presentation is loading. Please wait.
Published byBonnie Lucas Modified over 9 years ago
1
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot
2
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 variables double number1 = input.nextDouble(); // read first double double number2 = input.nextDouble(); // read second double double number3 = input.nextDouble(); // read third double // determine the maximum value double 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.2 Maximum is: 105.2
3
Exercise 1 – Cont’d Trace the main method using the tabular model Trace the problem solving method using the tabular model
4
Boolean Expressions Evaluate to true or false Translations from pseudocode to Java for: PseudocodeJava = (not a Boolean expression) AND && OR || NOT ! A = B A == B A ≤ B A <= B A B A >= B A B A != B
5
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 value boolean odd; if (i % 2 == 0) { odd = false; } else { odd = true; } odd TRUE odd FALSE falsetrue I mod 2 = 0 ?
6
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 falsetrue I mod K = 0 ? Java: // assume i, k have values boolean multiple; if (i % k == 0) { multiple = true; } else { multiple = false; }
7
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 FALSE
8
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 AND X 20 ? Java: // assume x has a value boolean inRange; if ( (x>=10) && (x<=20) ) { inRange = true; } else { inRange = false; }
9
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.
10
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). falsetrue (((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 }
11
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
12
12 Example 3 ExpressionValue (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?
13
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
14
Algorithm Design GIVEN: num (an integer) INTERMEDIATE:None RESULT: sign (sign of the number) HEADER: sign Sign(num) BODY: 14
15
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
16
Exercise 6 Translate the following algorithm to Java
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.