what s going on here
play

Whats going on here? Results from multiple runs of the same program: - PowerPoint PPT Presentation

Whats going on here? Results from multiple runs of the same program: Flipping a coin: Heads! Flipping a coin: Tails! Flipping a coin: Tails! Flipping a coin: Tails! Each run flips Class #07: Flipping a coin: Heads! coin one time Basic


  1. What’s going on here? Results from multiple runs of the same program: Flipping a coin: Heads! Flipping a coin: Tails! Flipping a coin: Tails! Flipping a coin: Tails! Each run flips Class #07: Flipping a coin: Heads! coin one time Basic Selection Statements Flipping a coin: Tails! Flipping a coin: Heads! Flipping a coin: Heads! Software Design I (CS 120): D. Mathias Flipping a coin: Tails! 2 Software Design I (CS 120) Flexibility in Programs The if (Conditional Branch) Command } So far, our programs have been } Simplest form of Java control: if ( ConditionHolds ) { mainly one -directional 1. ConditionHolds : any execution Instructions ; } Given the same inputs and the input output expression with a boolean } same commands, the same thing is value ( true/false ) done every time it runs double netPay = 20000; double rate = 0.0; 2. Instructions : execute } For many applications we want if ( netPay < 30000 ) if ConditionHolds is programs to change their { execution behavior at times input output1 rate = 0.1; true (otherwise the code } } Different program results occur, doesn’t do anything) double taxes = rate * netPay; depending upon factors that are output2 } When Instructions is determined at runtime double netPay = 20000; double rate = 0.0; } That is, behavior isn’t known ahead only one line, the braces are if ( netPay < 30000 ) of time, and can change while the optional , and can be left off A conditional branch: depending upon rate = 0.1; program is running different conditions, we can get different execution paths & final outcomes double taxes = rate * netPay; 3 4 Software Design I (CS 120) Software Design I (CS 120) 1

  2. Using the if Command Adding Options with the else Command } Scanner class has a number of non- We often have multiple different options we sometimes want to run: } void methods that check input java.util.Scanner ! If ConditionHolds is true at runtime, then Instructions execute 1. format and return boolean values. W e can use them as the condition of If ConditionHolds is false , then OtherInstructions execute instead 2. an if -statement: << constructors >> ! Again, braces are optional when a set of instructions is only one line Scanner( InputStream ) ! } Scanner scan; << query >> ! scan = new Scanner( System.in ); if ( ConditionHolds ) Scanner scan = new Scanner( System.in ); String next() ! { String nextLine() ! double netPay = scan.nextDouble(); int i = 0; Instructions; double nextDouble() ! double taxRate = 0.0; if ( scan.hasNextInt() ) } int nextInt() ! if ( netPay < 30000 ) { else ... ! { i = scan.nextInt(); { boolean hasNext() ! taxRate = 0.1; OtherInstructions; } boolean hasNextLine() ! } } boolean hasNextDouble() ! else boolean hasNextInt() ! { If the user enters an int value, taxRate = 0.25; << update >> ! variable i will get that value. } void close() ! Otherwise, i stays 0. double taxes = taxRate * pay; 5 6 Software Design I (CS 120) Software Design I (CS 120) Using if-else Commands Order Matters! } When we add an else -clause, we are guaranteed that exactly one set of Scanner scan; instructions will be executed when the program runs This is similar to the scan = new Scanner( System.in ); prior code, but behaves } Either the if -clause or the else -clause will run, but never both differently when run. if ( scan.hasNextDouble() ) Scanner scan; { scan = new Scanner( System.in ); double num1 = scan.nextDouble(); If the user enters an int If the user enters either value, the code will read System.out.println( num1 / 2.0 ); integer or floating-point it in and do some output values, it will always be if ( scan.hasNextInt() ) } using integer division. read as a double. { else int num1 = scan.nextInt(); { If the user doesn’t enter Since every numeric type System.out.println( num1 / 2 ); an integer, it will try to int num2 = scan.nextInt(); can be widened to a read input as a double. double, hasNextDouble() } System.out.println( num2 / 2 ); returns true for any else } number you give it.* { (*The number cannot be too double num2 = scan.nextDouble(); If input is not a number large, but otherwise, System.out.println( num2 / 2.0 ); at all, then errors will anything works.) } still occur! 7 8 Software Design I (CS 120) Software Design I (CS 120) 2

  3. Relations between Java Primitives Relations between Java Primitives } When using primitives ( int , } We may combine primitive types when we compare them int i = 3; double , char , etc.), we int j = 4; } Java does an automatic widening of all types as needed so they if ( i <= j ) { can easily compare values are able to be compared meaningfully ( just like arithmetic ) System.out.println( i ); } } Use relational operators int is widened to a double } Result: a boolean value double n = 3.6; ( true/false ) Expression evaluates to true double m = 4.5; if ( n != m ) { System.out.println( n ); == equal to } int num1 = 3; != not equal to double num2 = 3.0; < less than String s = “Hello!”; if ( num1 == num2 ) <= less than/equal to char c1 = s.charAt( 2 ); { char c2 = s.charAt( 3 ); > greater than System.out.println( “Equal” ); if ( c1 == c2 ) { >= greater than/equal to System.out.println( “Same!” ); } } 9 10 Software Design I (CS 120) Software Design I (CS 120) Two Things to be Careful about This Week & Next Don’t confuse equality ( == ) with assignment ( = ) 1. } Due to the pandemic and the time lag between when I double x = 0.1 Error ! record the lectures and when you watch them, the if ( x = 0.1 ) { We want == here. System.out.println( “Lucky!” ); Coming Events slides are being discontinued. Too many } factors can change too quickly for these to be useful. When using non-primitive objects, like Rectangles or 2. } Instead, watch for email updates and check the Canvas Ovals or Strings , do not assume that relational announcements. operators will work! (May need class-specific methods.) String s1 = “Hello”; String s1 = “Hello”; String s2 = new String( “Hello” ); String s2 = new String( “Hello” ); if ( s1 == s2 ) { if ( s1.equals( s2 ) ) { System.out.println( “Same!” ); System.out.println( “Same!” ); // WRONG!! No output seen. // RIGHT!! Output is seen. } } 11 12 Software Design I (CS 120) Software Design I (CS 120) 3

Recommend


More recommend