Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf.

Similar presentations


Presentation on theme: "1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf."— Presentation transcript:

1 1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf

2 2 Fall 2007ACS-1903 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.javaVariableScope.java

3 3 Fall 2007ACS-1903 Conditional Operator 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 ; General Syntax for a Conditional Expression expression1 ? expression2 : expression3 ;

4 4 Fall 2007ACS-1903 Conditional Expression 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 Can also appear in an assignment statement

5 5 Fall 2007ACS-1903 switch statement – an n-way decision structure Value of integer expression is case 1 actions =case 1 =case 2 =case n case 2 actionscase n actions … …

6 6 Fall 2007ACS-1903 switch As a UML activity diagram [expression=case 1 ] Action 2Action 1 [expression=case 2 ][expression=case n ] Action n … SwitchDemo.java NoBreaks.java PetFood.java Notice use of break in the cases

7 7 Fall 2007ACS-1903 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));

8 8 Fall 2007ACS-1903 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

9 9 Fall 2007ACS-1903 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 %dfor an integer value %6d… in 6 positions %ffor a floating-point value %8f… in 8 positions %.2f… with 2 decimal places %8.2f… in 8 positions with 2 decimal places %12sfor a string in 12 positions


Download ppt "1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf."

Similar presentations


Ads by Google