Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 SaiGon Tech 1. 2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples.

Similar presentations


Presentation on theme: "Copyright © 2007 SaiGon Tech 1. 2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples."— Presentation transcript:

1 Copyright © 2007 SaiGon Tech 1

2 2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples

3 Copyright © 2007 SaiGon Tech 3 A. Overview of the Sequential Structure Statement 1 Statement 2

4 Copyright © 2007 SaiGon Tech 4 B. Decision Structures 1.The if statement 2.The if – else statement 3.The switch statement

5 Copyright © 2007 SaiGon Tech 5 1. The if statement if (BooleanExpression){ statements; } Boolean Expression Statements True False Start Exit

6 Copyright © 2007 SaiGon Tech 6 2. The if – else statement if (BooleanExpression) { true_statements; } else { false_statements; } Boolean Expression False false_statements true_statements True Start Exit

7 Copyright © 2007 SaiGon Tech 7 3. The switch statement switch (SwitchExpression) { case CaseExpression_A: statements for CaseExpression_A; break; case CaseExpression_ B: statements for CaseExpression_B; break; default: default statements; } SwitchExpression = Expression A SwitchExpression = Expression B Exit Statements A TrueFalse True Statements B Default Statements False Start

8 Copyright © 2007 SaiGon Tech 8 C. Decision Structure Problems I.The Grade Problem II.The Loan Verifying Problem III.The Speed of Sound Problem IV.The Guess Word Game Problem V.The Pet Food Problem VI.The Sales Commission Problem VII. The Consulting Charge Problem

9 Copyright © 2007 SaiGon Tech 9  Test result of a student includes 3 parts: math, essay and interview  The average score of a test result is calculated by following formula:  Calculate the grade of a test result based on following rules: Average Score = (Math score + Essay score + Interview score ) / 3 I. The Grade Problem

10 Copyright © 2007 SaiGon Tech 10 Score AverageLetter Grade 90-100A 80-89B 70-79C 60-69D Below 60F

11 Copyright © 2007 SaiGon Tech 11 Solution 1.Problem Description (WHAT) 2.Preliminary Class Design (WHAT) 3.Examples (WHAT) 4.Developing the Test Class (WHAT) 5.Developing the Main Class (HOW) 6.Testing

12 Copyright © 2007 SaiGon Tech 12 1. Problem Description  Calculate the grade of a test result  Input: math, essay, and interview scores  Output: Grade

13 Copyright © 2007 SaiGon Tech 13 2. Preliminary Class Design 2.1. Defining behavior 2.2. Defining properties and remove parameters if necessary 2.3. Defining class name

14 Copyright © 2007 SaiGon Tech 14 2.1. Defining behavior

15 Copyright © 2007 SaiGon Tech 15 2.2. Defining properties and remove parameters if necessary

16 Copyright © 2007 SaiGon Tech 16 2.3. Defining class name

17 Copyright © 2007 SaiGon Tech 17 3. Examples  Average score < 60  ‘F’  Average score == 60  ‘D’  Average score < 70  ‘D’  Average score == 70  ‘C’  Average score < 80  ‘C’  Average score == 80  ‘B’  Average score < 90  ‘B’  Average score == 90  ‘A’  Average score > 90  ‘A’

18 Copyright © 2007 SaiGon Tech 18 4. Developing the Test Class

19 Copyright © 2007 SaiGon Tech 19 5. Developing the Main Class

20 Copyright © 2007 SaiGon Tech 20 6. Testing 

21 Copyright © 2007 SaiGon Tech 21 II. Loan Verifying Problem  A loan request consists of annual salary and number of years on current job of the customer  Determine whether a loan request is qualified or not  To qualify, a customer must earn at least $30,000 per year, and must have been on his or her current job for at least two years

22 Copyright © 2007 SaiGon Tech 22 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

23 Copyright © 2007 SaiGon Tech 23 1. Problem Description  Determine whether a loan request is qualified or not  Input: annual salary, number of years on current job  Output: true/false

24 Copyright © 2007 SaiGon Tech 24 2. Preliminary Class Design 2.1. Defining behavior 2.2. Defining properties and remove parameters if necessary 2.3. Defining class name

25 Copyright © 2007 SaiGon Tech 25 2.1. Defining behavior

26 Copyright © 2007 SaiGon Tech 26 2.2. Defining properties and remove parameters if necessary

27 Copyright © 2007 SaiGon Tech 27 2.3. Defining class name

28 Copyright © 2007 SaiGon Tech 28  Annual Salary: |----------------------30,000---------------------|  Years on Job: |----------------------2--------------------------|  Thus, we should have 9 examples: 1. annual salary < 30,000 && nYears on current job < 2  false 2. annual salary < 30,000 && nYears on current job == 2  false 3. annual salary 2  false 4. annual salary == 30,000 && nYears on current job < 2  false 5. annual salary == 30,000 && nYears on current job == 2  true 6. annual salary == 30,000 && nYears on current job > 2  true 7. annual salary > 30,000 && nYears on current job < 2  false 8. annual salary > 30,000 && nYears on current job == 2  true 9. annual salary > 30,000 && nYears on current job > 2  true 3. Examples

29 Copyright © 2007 SaiGon Tech 29 4. Developing the Test Class

30 Copyright © 2007 SaiGon Tech 30 5. Developing the Main Class

31 Copyright © 2007 SaiGon Tech 31 III. Speed Of Sound Problem  A sound meter holds information about speed of sound in air, water, and steel (see the table below)  Given the distance and the medium that a sound wave will travel, calculate the amount of time it takes that sound to travel in that medium with the following formula Time = Distance / Speed MediumApproximate speed (feet/second) Air 1,100 Water 4,900 Steel 16,400

32 Copyright © 2007 SaiGon Tech 32 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

33 Copyright © 2007 SaiGon Tech 33 1. Problem Description  Calculate the amount of time that takes a sound to travel in a given medium  Input: distance (feet), medium (air, water, or steel)  Output: time (seconds)

34 Copyright © 2007 SaiGon Tech 34 2. Preliminary Class Design

35 Copyright © 2007 SaiGon Tech 35 3. Examples  1100.0, “air”  1.0  4900.0, “water”  1.0  16400.0, “steel”  1.0

36 Copyright © 2007 SaiGon Tech 36 4. Developing the Test Class

37 Copyright © 2007 SaiGon Tech 37 5. Developing the Main Class 5.1. Template 5.2. Implementation 5.3. Improving the Code

38 Copyright © 2007 SaiGon Tech 38 5.1. Template

39 Copyright © 2007 SaiGon Tech 39 5.2. Implementation

40 Copyright © 2007 SaiGon Tech 40 5.3. Improving the Code 5.3.1. Caring about the case of Strings 5.3.2. Removing the Magic Numbers 5.3.3. Using static Methods

41 Copyright © 2007 SaiGon Tech 41 5.3.1. Caring about the case of Strings

42 Copyright © 2007 SaiGon Tech 42 5.3.2. Removing the Magic Numbers

43 Copyright © 2007 SaiGon Tech 43 5.3.3. Using static Methods

44 Copyright © 2007 SaiGon Tech 44

45 Copyright © 2007 SaiGon Tech 45 IV. The Guess Word Game Problem  The game hides a secret word. Develop a program to check whether the user’s guess is correct or not  Comparison should be case-insensitive

46 Copyright © 2007 SaiGon Tech 46 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

47 Copyright © 2007 SaiGon Tech 47 1. Problem Description  Check whether a guess about a secret word is correct or not  Input: a guess word  Output: true/false

48 Copyright © 2007 SaiGon Tech 48 2. Preliminary Class Design

49 Copyright © 2007 SaiGon Tech 49 3. Examples Secret Word : SAIGONTECH NoInputOutput 1sAIgONteCHFalse 2SAIGONTECHTrue 3PROSPEROFalse

50 Copyright © 2007 SaiGon Tech 50 4. Developing the Test Class

51 Copyright © 2007 SaiGon Tech 51 5. Developing the Main Class

52 Copyright © 2007 SaiGon Tech 52 V. The Pet Food Problem  A pet food store has a price table for different food grades as is follows  Given a grade, determine its price Pet Food GradePrice per lb. (cents) A30.0 B20.0 C10.0

53 Copyright © 2007 SaiGon Tech 53 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class 6.Removing Bad Smell: Magic Numbers

54 Copyright © 2007 SaiGon Tech 54 1. Problem Description  Determine the food price  Input: grade  Output: price

55 Copyright © 2007 SaiGon Tech 55 2. Preliminary Class Design

56 Copyright © 2007 SaiGon Tech 56 3. Examples  ‘A’  30.0  ‘B’  20.0  ‘C’  10.0

57 Copyright © 2007 SaiGon Tech 57 4. Developing the Test Class

58 Copyright © 2007 SaiGon Tech 58 5. Developing the Main Class

59 Copyright © 2007 SaiGon Tech 59 6. Removing Bad Smell: Magic Numbers

60 Copyright © 2007 SaiGon Tech 60 VI. The Sales Commission Problem  Hal’s Home Computer Emporium is a retail seller of home computers. Hal’s sales staff works strictly on commission. At the end of the month, each salesperson’s commission is calculated according to table below: Monthly SalesCommission Rate less than $10,0005% $10,000–14,99910% $15,000–17,99912% $18,000–21,99915% $22,000 or more16%  For example, a salesperson with $16,000 in monthly sales will earn a 12% commission ($1,920). Another salesperson with $20,000 in monthly sales will earn a 15% commission ($3,000).

61 Copyright © 2007 SaiGon Tech 61  Because the staff gets paid once per month, Hal allows each employee to take up to $1,500 per month in advance. When sales commissions are calculated, the amount of each employee’s advanced pay is subtracted from the commission. If any salesperson’s commissions are less than the amount of their advance, they must reimburse Hal for the difference.  Here are two examples: Beverly and John have $21,400 and $12,600 in sales, respectively. Beverly’s commission is $3,210 and John’s commission is $1,260. Both Beverly and John took $1,500 in advance pay. At the end of the month, Beverly gets a check for $1,710, but John must pay $240 back to Hal.  Calculate the amount of pay by using the formula Pay = Commission – Advanced pay

62 Copyright © 2007 SaiGon Tech 62 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

63 Copyright © 2007 SaiGon Tech 63 1. Problem Description  Calculate the payment of an employee  Input: monthly sales, advanced pay  Output: payment

64 Copyright © 2007 SaiGon Tech 64 2. Preliminary Class Design

65 Copyright © 2007 SaiGon Tech 65 3. Examples SalesPersonInputOutput SalesAdvanced PayCommissionPayment 1$ 16,000$0$1,920 2$ 20,000$0$3,000 3$ 21,400$1,500$3,210$1,710 4$12,600$1,500$1,260-$240

66 Copyright © 2007 SaiGon Tech 66 4. Developing the Test Class

67 Copyright © 2007 SaiGon Tech 67 5. Developing the Main Class

68 Copyright © 2007 SaiGon Tech 68 VII. The Consulting Charge Problem  A consulting session records the number of hours on discussion with the customer. The rule is that if the number of hours is less than 5, it is recorded as 5.0  Calculate the total charge of a consulting session, provided that an hour costs $50

69 Copyright © 2007 SaiGon Tech 69 Solution 1.Problem Description 2.Preliminary Class Design 3.Examples 4.Developing the Test Class 5.Developing the Main Class

70 Copyright © 2007 SaiGon Tech 70 1. Problem Description  Calculate the total charge of a consulting session  Input: number of consulting hours  Output: total charge

71 Copyright © 2007 SaiGon Tech 71 2. Preliminary Class Design

72 Copyright © 2007 SaiGon Tech 72 3. Examples  4.0  250.0  5.0  250.0  10.0  500.0

73 Copyright © 2007 SaiGon Tech 73 4. Developing the Test Class

74 Copyright © 2007 SaiGon Tech 74 5. Developing the Main Class

75 Copyright © 2007 SaiGon Tech 75 References 1.Tony Gaddis – Starting Out with Java. From Control Structures through Data Structures – Pearson Addison Wesley, 2010, Chapter 3


Download ppt "Copyright © 2007 SaiGon Tech 1. 2 Contents A.Overview of the Sequential Structure B.Decision Structures C.Decision Structure Examples."

Similar presentations


Ads by Google