Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Similar presentations


Presentation on theme: "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."— Presentation transcript:

1 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 2 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement

3 3 Relational Expressions Relational expressions –Expressions that compare operands –Sometimes called conditions –Evaluated to yield result –Typically used as part of selection statement Simple relational expression –Consists of relational operator connecting two variable and/or constant operands

4 4 Relational Expressions (continued) Figure 5.1: The anatomy of a simple relational expression

5 5 Relational Expressions (continued) Table 5.1: Relational Operators for Primitive Data Types

6 6 Logical Operators Complex conditions can be created using Boolean logical operations: –AND –OR –NOT AND operator –&& –True only if both expressions are true

7 7 Logical Operators (continued) OR operator –|| –True if either (or both) expressions are true NOT operator –! –Changes expression to opposite state Relational operators have higher precedence than logical operators

8 8 Logical Operators (continued) && and || operators: –Can only be used with Boolean operands –Second operand not evaluated if evaluation of first is sufficient to determine final value of logical operation (called short-circuit evaluation)

9 9 Logical Operators (continued) Table 5.2: Operator Precedence

10 10 A Numerical Accuracy Problem Tests for equality of numbers using relational operator == should be avoided –Applies to: Floating-point Double-precision Many decimal numbers cannot be represented exactly in binary using finite number of bits Require that absolute value of difference between operands be less than extremely small value

11 11 The if-else Statement Directs computer to select sequence of instructions based on result of comparison Condition is evaluated first –If value of condition is true Statement in the if clause is executed –If value is false Statement after reserved word else executed

12 12 The if-else Statement (continued) if (condition) <---------------no semicolon here statement1; else <-----------------no semicolon here statement2;

13 13 The if-else Statement (continued) Figure 5.2: The if-else flowchart

14 14 Compound Statements Any number of single statements contained between braces Takes place of single statement Semicolon is not placed after braces that define compound statement

15 15 Compound Statements (continued) Figure 5.5: A compound statement consists of individual statements enclosed within braces

16 16 The Boolean Data Type Tested condition in if-else statement must always evaluate to Boolean value Value of condition must be either true or false Boolean data type –Restricted in usage as value of relational expression –Boolean values can be: Displayed Compared Assigned

17 17 One-Way Selection No else expression Syntax: if (condition) statement; Statement following if (condition) –Only executed if condition has true value

18 18 Placement of Braces in a Compound Statement Common practice for some programmers –Place opening brace of compound statement on same line as if and else statements if (condition) { statement1; } else { statement2; }

19 19 Nested if Statements One or more if-else statements can be included within either part of if-else statement Last else with closest unpaired if –Unless braces alter default pairing Process of nesting if statements can be extended indefinitely

20 20 The if-else Chain Else part of an if statement contains another if-else Syntax: if (expression1) statement1; else if (expression2) statement2; else statement3;

21 21 The if-else Chain (continued) Alternate syntax: if (expression1) statement1; else if (expression2) statement2; else statement3; Used extensively in programming applications

22 22 import javax.swing.*; import java.text.*; // needed for formatting public class CalculateTaxes { public static void main(String[] args) { double taxable, taxes; String s1; DecimalFormat df = new DecimalFormat(",###.00"); s1 =JOptionPane.showInputDialog("Please type in the taxable income:"); taxable = Double.parseDouble(s1); if (taxable <= 20000.0) taxes = 0.02 * taxable; else taxes = 0.025 * (taxable - 20000.0) + 400.0; JOptionPane.showMessageDialog(null, "Taxes are $" + df.format(taxes), "QuickTest Program 5.1", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

23 23 Compound Statement Example int tempType; double temp, fahren, celsius; String s1, prompt, message; // set up format variable DecimalFormat num = new DecimalFormat(",###.00"); s1 = JOptionPane.showInputDialog("Enter the temperature to be converted: "); temp = Double.parseDouble(s1); prompt = "Enter a 1 if the temperature is in Fahrenheit\n" +" or a 2 if the temperature is in Celsius: "; s1 =JOptionPane.showInputDialog(prompt); tempType = Integer.parseInt(s1); if (tempType = = 1) { celsius = (5.0 / 9.0) * (temp - 32.0); message = "The equivalent Celsius temperature is " + num.format(celsius); } else { fahren = (9.0 / 5.0) * temp + 32.0; message = "The equivalent Fahrenheit temperature is " + num.format(fahren); }

24 24 import javax.swing.*; public class CheckLimit { public static void main(String[] args) { final double LIMIT = 3000.0; String s1; double mileage; s1 = JOptionPane.showInputDialog("Please enter the mileage driven:"); mileage = Double.parseDouble(s1); if(mileage > LIMIT) JOptionPane.showMessageDialog(null, "For a mileage of " + mileage + "\nThis car is over the mileage limit", "QuickTest Program 5.3", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "End of Program", "QuickTest Program 5.3", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } One Way Selection

25 25 Nested Chain s1 = JOptionPane.showInputDialog("Enter the value of monthly sales:"); monthlySales = Double.parseDouble(s1); if (monthlySales >= 50000.00) income = 375.00 +.16 * monthlySales; else if (monthlySales >= 40000.00) income = 350.00 +.14 * monthlySales; else if (monthlySales >= 30000.00) income = 325.00 +.12 * monthlySales; else if (monthlySales >= 20000.00) income = 300.00 +.09 * monthlySales; else if (monthlySales >= 10000.00) income = 250.00 +.05 * monthlySales; else income = 200.00 +.03 * monthlySales; outMessage = "For monthly sales of $" + num.format(monthlySales) + "\nThe income is $" + num.format(income);

26 26 Lab Work Input and run QuickTest programs 5.1- 5.4 Begin lab 9 assignments


Download ppt "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."

Similar presentations


Ads by Google