Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ITI 1120 Lab #4 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 number1 = input.nextDouble(); // read first double double number2 = input.nextDouble(); // read second double double number3 = input.nextDouble(); // read third double Enter three floating-point values separated by spaces: 23.6 12.9 105.2 Maximum is: 105.2 Working Memory number1 Number2 // determine the maximum value double result = maximum( number1, number2, number3 ); Number3 // display maximum value System.out.println( "Maximum is: " + result ); 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! x y z maximumValue Boolean Expressions • Evaluate to true or false • Translations from pseudocode to Java for: Pseudocode AND OR NOT A=B A≤B AB AB 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 = STE0131) OR (room = STE2052)) AND (Lab = ITI1220) 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: Java: X 10 AND X 20 ? false inRange FALSE true inRange TRUE // assume x has a value boolean inRange; if ( (x>=10) && (x<=20) ) { inRange = true; } else { inRange = 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) 10 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)) Value TRUE TRUE FALSE NOT ((X > 0) OR ((X < Y) AND (Y = 0))) FALSE Can you translate them to Java syntax? 11 Exercise 4 • Write a Boolean expression that evaluates true if the age is between 18 and 55 inclusively. – Develop a problem solving method with one parameter (GIVEN), the age, and a boolean result – true if age is in the interval and false otherwise. – Complete the program with a main method that request an age from the user and prints “Transaction accepted” if the age is within the interval and “Transaction refused” otherwise. 12 Exercise 5 • Write a program that checks for an integer number if divisible by 2 and/or 3. The program should use the two methods template: – The main method should interact with the user to get the value of the integer and displays if this integer is divisible by 2 and 3. – The isDivisible method has one parameter (GIVEN) the value of the number and one result, an integer representing if the number divides 2 and 3, divides 2 or 3, or doesn’t divide both. 13 Example 6 • 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. 14 Exercise 7 • Translate the following algorithm to Java GIVEN: INTERMEDIATE: RESULT: HEADER: BODY: (none) Grade (none) LetterGrade()