Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Work with Data and Decision Structure. Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 2."— Presentation transcript:

1 Work with Data and Decision Structure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16 Decision Structure

17 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 <= 10000 5% tax Taxable income > 1000015% tax Complex condition: – If an applicant’s GPA > 3.0 and SAT > 1200: admitted

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

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

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

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

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

23 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;

24 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! "); }

25 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.

26 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");

27 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); }

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

29 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);

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

31 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

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

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

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

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

36 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;

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

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

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

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

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

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

43 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?

44 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?

45 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?

46 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

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

48 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?

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

50 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

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

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

53 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


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

Similar presentations


Ads by Google