220 likes | 231 Views
Chapter 3:Decision Structures. Chapter 3:Decision Structures. 3.1 The if Statement 3.2 The if-else Statement 3.3 The if-else-if Statement 3.4 Nested if Statements 3.5 Logical Operators 3.6 Comparing String Objects 3.7 More About Variable Declaration and Scope 3.9 The switch Statement
E N D
Chapter 3:Decision Structures • 3.1 The if Statement • 3.2 The if-else Statement • 3.3 The if-else-if Statement • 3.4 Nested if Statements • 3.5 Logical Operators • 3.6 Comparing String Objects • 3.7 More About Variable Declaration and Scope • 3.9 The switch Statement • 3.10 Creating Objects with the DecimalFormat Class • 3.12 Common Errors to Avoid
Variable Scope • In Java, a local variable does not have to be declared at the beginning of the method. • The scope of a local variable begins at the point it is declared and terminates at the end of the method. • When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program.
VariableScope.java // Get the user's first name. String firstName; firstName = JOptionPane.showInputDialog("Enter your " + "first name."); String lastName; lastName = JOptionPane.showInputDialog("Enter your " + "last name."); JOptionPane.showMessageDialog(null, "Hello " + firstName + " " + lastName);
The Conditional Operator • The conditional operator is a ternary (three operand) operator. • The conditional operator allows a programmer to write a simple if-else type statement. • The format of the operators is: expression1 ? expression2 : expression3 • The conditional operator can also return a value.
The Conditional Operator • The conditional operator can be used as a shortened if-else statement: x > y ? z = 10 : z = 5; • This line is functionally equivalent to: if(x > ) z = 10; else z = 5;
The Conditional Operator • Many times, the conditional operator is used to supply a value. number = x > y ? 10 : 5; • This is functionally equivalent to: if(x > y) number = 10; else number = 5;
ConsultantCharges.java double hours, // To hold the hours worked charges; // To hold the charges String input; // To hold user input input = JOptionPane.showInputDialog("How many hours were worked? "); hours = Double.parseDouble(input); hours = hours < 5 ? 5 : hours; charges = 50.0 * hours; JOptionPane.showMessageDialog(null, "The charges are $" + charges);
The switch Statement • The if-else statements allow the programmer to make true / false branches. • The switch statement allows the programmer to use an ordinal value to determine how a program will branch. • The switch statement can evaluate an integer type or character type variable and make decisions based on the value.
The switch Statement • The switch statement takes the form: switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; default: // place one or more statements here }
The switch Statement • The switch statement takes an ordinal value (byte, short, int, long, char) as the SwitchExpression. switch (SwitchExpression) { … } • The switch statement will evaluate the expression. • If there is an associated case statement that matches that value, program execution will be transferred to that case statement.
The switch Statement • Each case statement will have a corresponding CaseExpression that must be unique. case CaseExpression: // place one or more statements here break; • If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.
The switch Case • The break statement ends the case statement. • The break statement is optional. • If a case does not contain a break, then program execution continues into the next case.
NoBreaks.java switch (number) { case 1: System.out.println("You entered 1."); case 2: System.out.println("You entered 2."); case 3: System.out.println("You entered 3."); default: System.out.println("That's not 1, 2, or 3!"); }
PetFood.java switch(foodGrade) { case 'a': case 'A': System.out.println("30 cents per lb."); break; case 'b':case 'B': System.out.println("20 cents per lb."); break; case 'c':case 'C': System.out.println("15 cents per lb."); break; default: System.out.println("Invalid choice."); }
The switch Case • The default case is optional and will be executed if no CaseExpression matches the SwitchExpression.
SwitchDemo.java // Determine the number entered. switch (number) { case 1: System.out.println("You entered 1."); break; case 2: System.out.println("You entered 2."); break; case 3: System.out.println("You entered 3."); break; default: System.out.println("That's not 1, 2, or 3!"); }
The DecimalFormat Class • When printing out double and float values, the full fractional value will be printed. • The DecimalFormat class can be used to format these values. • In order to use the DecimalFormat class, the Java import statement must be used. • At the top of the program the statement import java.text.DecimalFormat; must be used.
Format1.java double number1 = 0.166666666666667; double number2 = 1.666666666666667; double number3 = 16.666666666666667; double number4 = 166.666666666666667; DecimalFormat formatter = new DecimalFormat("#0.00"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4));
Format2.java double number1 = 0.166666666666667; double number2 = 1.666666666666667; double number3 = 16.666666666666667; double number4 = 166.666666666666667; // Create a DecimalFormat object. DecimalFormat formatter = new DecimalFormat("000.00"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4));
Format3.java double number1 = 123.899; double number2 = 1233.899; double number3 = 12345.899; double number4 = 123456.899; double number5 = 1234567.899; DecimalFormat formatter = new DecimalFormat("#,##0.00"); System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2)); System.out.println(formatter.format(number3)); System.out.println(formatter.format(number4)); System.out.println(formatter.format(number5));
Format4.java double number1 = 0.12; double number2 = 0.05; DecimalFormat formatter = new DecimalFormat("#0%"); // Display the formatted variable contents. System.out.println(formatter.format(number1)); System.out.println(formatter.format(number2));