Work with Data and Decision Structure. Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 2.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
If Function Determine action based on condition. Examples Simple condition: –If total sales exceeds $300 then applies 5% discount; otherwise, no discount.
Murach’s ASP.NET 4/C#, C6© 2011, Mike Murach & Associates, Inc.Slide 1.
Murach’s C# 2010, C6 © 2010, Mike Murach & Associates, Inc.Slide 1.
Murach’s C# 2010, C3 © 2010, Mike Murach & Associates, Inc.Slide 1.
Murach’s JavaScript, C3© 2009, Mike Murach & Associates, Inc.Slide 1.
Murach’s ASP.NET 4/C#, C3© 2011, Mike Murach & Associates, Inc.Slide 1.
Murach’s ASP.NET 2.0/C#, C3© 2006, Mike Murach & Associates, Inc.Slide 1.
Murach's C# 2012, C3© 2013, Mike Murach & Associates, Inc.Slide 1.
Murach’s ASP.NET 4/C#, C2© 2011, Mike Murach & Associates, Inc.Slide 1.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Murach’s C# 2010, C1© 2010, Mike Murach & Associates, Inc.Slide 1.
© 2013, Mike Murach & Associates, Inc.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: – Fees_and_Expenses.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Murach’s C# 2010, C4 © 2010, Mike Murach & Associates, Inc.Slide 1.
Murach’s C# 2010, C4 © 2010, Mike Murach & Associates, Inc.Slide 1.
Murach's C , C4 © 2009, Mike Murach & Associates, Inc. Slide 1 © 2009, Mike Murach & Associates, Inc.Slide 1.
Murach’s ASP.NET 3.5/C#, C4© 2008, Mike Murach & Associates, Inc.Slide 1.
Logic & program control part 3: Compound selection structures.
Murach’s Java SE 6, C6© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Nested IF and Complex Condition. Nested IF Example: –Rules to determine bonus: JobCode = 1, Bonus=500 JobCode = 2, Bonus = 700 JobCode = 3, Bonus = 1000.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: –
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to.
Expression and Decision Structure ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Decisions, Decisions, Decisions Conditional Statements In Java.
Decisions Action based on condition. Examples Simple condition: –If total sales exceeds $300 then applies 5% discount; otherwise, no discount. More than.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Functions BUS 782. What are functions? Functions are prewritten formulas. We use functions to perform calculations. Enclose arguments within parentheses.
IF Function Decision: Action based on condition. Examples SF State Tuition calculation: –
Decision Structure - 2 ISYS 350.
© 2016, Mike Murach & Associates, Inc.
Decision Structure ISYS 350.
Decision Structure - 1 ISYS 350.
© 2016, Mike Murach & Associates, Inc.
SELECTION STATEMENTS (1)
© 2010, Mike Murach & Associates, Inc.
Decision Structure - 2 ISYS 350.
Decision Structure - 1 ISYS 350.
© 2010, Mike Murach & Associates, Inc.
Control Statement Examples
Work with Data and Decision Structure
Decision Structure - 2 ISYS 350.
SELECTION STATEMENTS (2)
Decision Structures ISYS 350.
Decision Structure - 2 ISYS 350.
Decision Structure - 2 ISYS 350.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Decision Structure - 1 ISYS 350.
Decision: Action based on condition
Decision Structure - 2 ISYS 350.
Functions BUS 782.
Decision Structure - 1 ISYS 350.
© 2010, Mike Murach & Associates, Inc.
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

Work with Data and Decision Structure

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 2

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 3

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 4

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 5

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 6

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 7 Enter Floating and Long Integer Values

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 8

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 9

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 10

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 11

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 12

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 13

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 14

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 15

Decision Structure

Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount; otherwise, no discount. More than one condition: Taxable Income < =3000 no tax 3000 < taxable income <= % tax Taxable income > % tax Complex condition: – If an applicant’s GPA > 3.0 and SAT > 1200: admitted

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 18

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 19

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 20

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 21

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 22

Example: If total sales is larger than 1000, then give 5% discount Scanner sc = new Scanner(System.in); double totalSales; double discountRate=0; System.out.println("Enter a total sales: "); totalSales=sc.nextDouble(); if (totalSales > 1000) discountRate=0.05; double netPay=totalSales*(1-discountRate); System.out.println("Net pay is: " + netPay); double discountRate; System.out.println("Enter a total sales: "); totalSales=sc.nextDouble(); if (totalSales > 1000) discountRate=0.05; else discountRate=0;

Example: If with a block of statements if (totalSales > 1000) { discountRate=0.05; System.out.println("Thank you so much! "); } else { discountRate=0; System.out.println("Thank you! "); }

Variable Scope Block-level scope: declared within a block of code. Method (Procedural) level scope: declared in a procedure Class-level: declared in a class but outside any procedure.

Scanner sc = new Scanner(System.in); System.out.println("Enter a city name"); String City=sc.next(); if (City.equalsIgnoreCase("Paris")) { String Country="France"; System.out.println("It is in " + Country); } else System.out.println("It is not in France");

What output you will see? public class Main { public static String projLevelVar="Project level"; String testVar="Class Level Var"; public static void main(String[] args) { demoScope(); } public static void demoScope(){ String testVar="Method Level Var"; System.out.println(testVar); }

More than one condition Rules for bonus: JobCode = 1300 JobCode = 2500 JobCode = 3700 JobCode = 41000

Scanner sc = new Scanner(System.in); double jobCode, bonus; System.out.println("Enter job code: "); jobCode = sc.nextDouble(); if (jobCode==1) bonus=300; else if (jobCode==2) bonus=500; else if (jobCode==3) bonus=700; else bonus=1000; System.out.println("Bonus is: " + bonus);

Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 30

Example Electric Company charges customers based on KiloWatt-Hour used. The rules are: – First 100 KH,20 cents per KH – Each of the next 200 KH ( up to 300 KH), 15 cents per KH – All KH over 300, 10 cents per KH

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 32 Case Structure with the Switch Statement

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 33

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 34

Code Examle switch((int) jobCode) { case 1: bonus=300; case 2: bonus=500; case 3: bonus=700; case 4: bonus=1000; default: bonus=0; }

Explicit Casting Since the Switch structure requires an integer expression and the jobCode is declared as a double, we need to convert it to integer: From double to integer: int code = (int) jobCode From integer to double: int counter = 0; double dcounter = (double) counter;

Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 37

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 38

Nested if statements if (customerType.equals("R")) { // begin nested if if (subtotal >= 100) discountPercent =.2; else discountPercent =.1; } // end nested if else discountPercent =.4;

Type = “R” Or not Discount=.2Subtota>=100? Discount=.1 Discount=.4

Example State University calculates students tuition based on the following rules: – State residents: Total units taken <=12, tuition = 1200 Total units taken > 12, tuition = per additional unit. – Non residents: Total units taken <= 9, tuition = 3000 Total units taken > 9, tuition = per additional unit.

Decision Tree Resident or Not Units <= 12 or Not Units <= 9 or Not

Consider the following code snippet. if (aNumber >= 0) if (aNumber == 0) System.out.println("first string"); else System.out.println("second string"); System.out.println("third string"); Does the “else” pair with the first or the second if? What output do you think the code will produce if aNumber is 3?

Complex Condition University admission rules: Applicants will be admitted if meet one of the following rules: – 1. Income >= 100,000 – 2. GPA > 2.5 AND SAT > 900 An applicant’s Income is 150,000, GPA is 2.9 and SAT is 800. Admitted? – Income >= 100,000 OR GPA > 2.5 AND SAT >900 How to evaluate this complex condition?

Scholarship: Business students with GPA at least 3.2 and major in Accounting or CIS qualified to apply: – 1. GPA >= 3.2 – 2. Major in Accounting OR CIS Is a CIS student with GPA = 2.0 qualified? – GPA >= 3.2 AND Major = “Acct” OR Major = “CIS” Is this complex condition correct?

Examples SAT = 800, Income 60,000, GPA 3.0, admitted? – (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 A=2, B=3 – (A=3 OR NOT (B < A)) AND B=A+1

NOT Set 1: Young: Age < 30 Set 2: Rich: Income >= 100,000 YoungRich

Condition with Not University admission rules: Applicants will be admitted if meet all the rules: – 1. SAT > 900 OR Income >= 50,000 – 2. Not GPA < 2.5 Condition: – SAT > 900 OR Income >= 50,000 AND Not GPA < 2.5 – Correct?

Order of Evaluation 1. () 2. Not 3. AND 4. OR

Examples SAT = 800, Income 60,000, GPA 3.0, admitted? – (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 A=2, B=3 – (A=3 OR NOT (B < A)) AND B=A+1

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 51

Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 52

Examples SAT = 800, Income 60,000, GPA 3.0, admitted: – (SAT > 900 | Income >= 50,000) & !GPA < 2.5 GPA >= 3.2 AND (Major = “Acct” OR Major = “CIS”) – GPA>=3.2 & (Major.equals(“Acct”) | Major.equals(“CIS”)) (A=3 OR NOT (B < A)) AND B=A+1 – (A==3 | !(B < A)) & B=A+1