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
Chapter 3 Decision Structures • • • • • Fall 2007 Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf ACS-1903 1 Variable scope Scope: a variable is only known within the block where it is declared. Local variable: a variable declared within a method. It is not known outside the method, and it can only be used from the point where it is declared to the end of the method. Aside: Java different kinds of variables are instance, static, final, local See VariableScope.java Fall 2007 ACS-1903 2 Conditional Operator General Syntax for a Conditional Expression expression1 ? expression2 : expression3 ; Consider: If sale is less than or equal to 300 Then reward points are dollar value / 25 Otherwise reward points are 50 plus (dollar value / 25) Java: sale <= 300 ? rewardPoints = dollars / 25 : rewardPoints = 50 + dollars / 25 ; Fall 2007 ACS-1903 3 Conditional Expression Can also appear in an assignment statement Java: rewardPoints = sale <= 300 ? dollars / 25 : 50 + dollars/25 ; See Checkpoint exercises on page 148 e.g. rewrite if (x > y) z = 1; else z = 20; as a conditional expression Fall 2007 ACS-1903 4 switch statement – an n-way decision structure Value of integer expression is … =case1 case1 actions Fall 2007 =case2 =casen case2 actions … casen actions ACS-1903 5 switch • As a UML activity diagram [expression=case1] Action 1 [expression=case2] Action 2 Notice use of break in the cases … [expression=casen] Action n SwitchDemo.java NoBreaks.java PetFood.java Fall 2007 ACS-1903 6 DecimalFormat Class • Used to control the way floating-point numbers are formatted Required to make the class available to the compiler: import java.text.DecimalFormat; Must instantiate a DecimalFormat object with a pattern: DecimalFormat formatter = new DecimalFormat(“#0.00”); Send this object the format() message & it returns a string System.out.println(formatter.format(number)); Fall 2007 ACS-1903 7 DecimalFormat Class • Formatting patterns # used to indicate leading zero is suppressed 0 used to indicate a digit position . used to specify decimal point % used to have value multiplied by 100 and obtain a percent sign , used to obtain a digits separator • Format1.java • Format2.java • Format3.java • Format4.java Fall 2007 ACS-1903 8 printf • This method uses a string with embedded format specifiers and other arguments that replace the specifiers in the output System.out.printf(“Hours worked is %d , pay rate is %d , and gross is %d”, hours, ratePay, grossPay); Examples %d %6d %f %8f %.2f %8.2f %12s Fall 2007 for an integer value … in 6 positions for a floating-point value … in 8 positions … with 2 decimal places … in 8 positions with 2 decimal places for a string in 12 positions ACS-1903 9