Presentation is loading. Please wait.

Presentation is loading. Please wait.

Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.

Similar presentations


Presentation on theme: "Work with Data and Decision Structure. Slide 2 Note: String and Date are classes."— Presentation transcript:

1 Work with Data and Decision Structure

2 Slide 2 Note: String and Date are classes.

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 Modulus Example Long division: – Quotient – Remainder Enter length in inches and determine the equivalent feet and inches: – 57 inches = 4 feet and 9 inches

12 Return Smallest Number of Coins Input: Changes in penny from 0 to 99 Output: Smallest number of coins System.out.print("enter changes between 0 to 99: "); Scanner sc = new Scanner(System.in) ; int Changes = sc.nextInt(); int qtr = Changes/25; int dime = (Changes - qtr*25)/10; int nickle = (Changes - qtr*25 - dime*10)/5; int penny = (Changes - qtr*25-dime*10-nickle*5); System.out.print("qtr= " + qtr + " dime=" + dime + " Nickle=" + nickle + " penny= " + penny); Examples: 26 cents: 1 Q, 1 P 57 cents: 2 Q, 1 N, 2 P 63 cents: 2 Q, 1 D, 3 P

13 String Concatenation with the concat method or “+” String firstName, lastName, fullName; System.out.println("Enter first name"); firstName=sc.next(); System.out.println("Enter last name"); lastName=sc.next(); fullName= firstName.concat(" ").concat(lastName); // fullName=firstName + " " + lastName; System.out.println("Full name is: " + fullName) ;

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 Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 16

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

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

19 Decision Structure

20 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

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 Murach’s Java SE 6, C2© 2007, Mike Murach & Associates, Inc. Slide 23

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

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

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

27 Even integer or odd integer Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); if (N % 2==0) System.out.println("Even"); else System.out.println("Odd");

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

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

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

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

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

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

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

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

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

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

38 Code Examle System.out.println("enter job code:"); double jobCode=sc.nextDouble(); Switch((int) jobCode) { case 1: bonus=300; break; case 2: bonus=500; break; case 3: bonus=700; break; case 4: bonus=1000; break; default: bonus=0; break; }

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

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

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

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

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

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

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

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

47 Complex Condition Examples: – A theater charges admission fee based on customer’s age: 12 <= Age <= 65:Fee = $5 Otherwise: Fee = $3 – X University admission rules: If GPA > 3.5 or SAT > 1500: Admitted – Y University admission rules: If GPA > 3.0 and SAT > 1200: Admitted

48 Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F

49 Examples Write a complex condition for: 12 <= Age <= 65 Use a complex condition to describe age not between 12 and 65. X <= 15 is equivalent to: X<15 AND X =15? (T/F) This complex condition is always false: – X 10 This complex condition is always true: – X >= 5 OR X <= 10

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

51 Examples of Logical Operators if (Age>=12 && Age <=65) Fee=20; else Fee=0; 1. If 12 <= Age <= 65, admission fee = 20, otherwise, free admission The rules to compute employee bonus are: If JobCode = 1 or Salary < 50000, then bonus = 500 Otherwise, bonus = 1000 if (jobCode==1 || Salary<50000) Bonus=500; else Bonus=1000;

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

53 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

54 More 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?

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

56 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

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

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

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

60 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

61 Java Examples (SAT > 900 OR Income >= 50,000) AND Not GPA < 2.5 – (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

62 Leap Year The complex condition to determine if a year is leap year is: If (the year is divisible by 4 and not divisible by 100) or (the year is divisible by 400), then it is a leap year. Write a equivalent Java complex condition.


Download ppt "Work with Data and Decision Structure. Slide 2 Note: String and Date are classes."

Similar presentations


Ads by Google