Reporter : Trần Thanh Giảng
Nguyễn Đình Vinh
1
Copyright © 2007 SaiGon Tech
Contents
A. Overview of the Sequential Structure
B. Decision Structures
C. Decision Structure Examples
2
Copyright © 2007 SaiGon Tech
A. Overview of the Sequential Structure
Statement 1
Statement 2
3
Copyright © 2007 SaiGon Tech
B. Decision Structures
The if statement
The if – else statement
The switch statement
4
Copyright © 2007 SaiGon Tech
1. The if statement
Start
if (BooleanExpression){
statements;
} False
Boolean
Expression
True
Statements
Exit
5
Copyright © 2007 SaiGon Tech
2. The if – else statement
Start
if (BooleanExpression) {
true_statements; True Boolean False
} Expression
else {
false_statements; true_statements false_statements
Exit
6
Copyright © 2007 SaiGon Tech
3. The switch statement
switch (SwitchExpression) {
case CaseExpression_A: Start
statements for CaseExpression_A;
break;
False True
case CaseExpression_ B: SwitchExpression
= Expression
statements for CaseExpression_B; A
break;
Statements A
default: False SwitchExpression True
= Expression
default statements; B
}
Default Statements Statements B
7
Copyright © 2007 SaiGon Tech Exit
C. Decision Structure Problems
I. Test Result Problem
II. Loan Verifier Problem
III. Speed of Sound Problem
IV. Guess Word Game Problem
V. Pet Food Problem
VI. Sales Commission Problem
VII. Consultant charges Problem
8
Copyright © 2007 SaiGon Tech
I. Test Result Problem
The Test Result of a student includes 3 parts: Math,
Essay and Interview.
The average score of the Test Result is calculated by
following formula:
Average Score = (Math score + Essay score + Interview score ) / 3
Classify the Test Result into categories based on
following rules:
9
Copyright © 2007 SaiGon Tech
Score Average Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
10
Copyright © 2007 SaiGon Tech
Solution
1. Problem Description
2. Examples
3. Preliminary Class Design
4. Finding the Main Task
5. Finding the Name of Class that Hosts the Main Task
6. Improving the Design
7. Developing the Test
8. Developing the Main Class
9. Running the Test
11
Copyright © 2007 SaiGon Tech
1. Problem Description
Given the math, essay, and interview scores, calculate
the grade of a test result
12
Copyright © 2007 SaiGon Tech
2. Examples
Example 1
Input: Math = 90; essay = 90; interview = 90, Average = 90
Output: A
Example 2:
Input: Math = 80; essay = 80; interview = 80, Average=80
Output: B
Example 3:
Input: Math = 70; essay = 70; interview = 70, Average=80
Output: C
Example 4:
Input: Math = 60; essay = 60; interview = 60, Average=60
Output: D
Example 5:
Input: Math = 50; essay = 50; interview = 50, Average=60
Output: F
13
Copyright © 2007 SaiGon Tech
3. Preliminary Class Design
14
Copyright © 2007 SaiGon Tech
4. Finding the Main Task
Name of method: getGrade
Input parameter(s): Math, essay, and interview scores
Return type: char
15
Copyright © 2007 SaiGon Tech
5. Finding the Name of Class that Hosts the Main
Task
Class name: TestResult
16
Copyright © 2007 SaiGon Tech
6. Improving the Design
17
Copyright © 2007 SaiGon Tech
7. Developing the Test
7.1. Creating the Project
7.2. Creating the Test Class
7.3. Developing the Test Method
18
Copyright © 2007 SaiGon Tech
7.1. Creating the Project
File > New > Java Project
Project name : test
File > New > Package
Package name : testresult
19
Copyright © 2007 SaiGon Tech
7.2. Creating the Test Class
•File > New > JUnit Test Case
•Test Class Name : TestResultTest
20
Copyright © 2007 SaiGon Tech
21
Copyright © 2007 SaiGon Tech
7.3. Developing the Test Method
7.3.1. Determining the Object that Performing the Task
7.3.2. Calling the Method On the Object
7.3.3. Confirming the Result
22
Copyright © 2007 SaiGon Tech
7.3.1. Determining the Object that Performing the Task
23
Copyright © 2007 SaiGon Tech
7.3.2. Calling the Method On the Object
24
Copyright © 2007 SaiGon Tech
7.3.3. Confirming the Result
25
Copyright © 2007 SaiGon Tech
8. Developing Main Class
8.1. Creating the Main Class Based On the Test
8.2. Creating the Main Method Based On the Test
26
Copyright © 2007 SaiGon Tech
8.1. Creating the Main Class Based On the Test
8.1.1. Generating the Main Class
8.1.2. Generating the Constructor
8.1.3. Declaring the Properties and Completing the
Constructor
27
Copyright © 2007 SaiGon Tech
8.1.1. Generating the Main Class
Click the bubble on the left > Double-click Create Class
‘TestResult’ > Finish
28
Copyright © 2007 SaiGon Tech
8.1.2. Generating the Constructor
Click the bubble on the left > Double-click Create
Constructor
29
Copyright © 2007 SaiGon Tech
8.1.3. Declaring the Properties and Completing the
Constructor
30
Copyright © 2007 SaiGon Tech
8.2. Creating the Main Method Based On the Test
8.2.1. Generating the Main Method
8.2.2. Developing the Main Method
31
Copyright © 2007 SaiGon Tech
8.2.1. Generating the Main Method
Click the bubble on the left > Double-click Create
method ‘getGrade()
32
Copyright © 2007 SaiGon Tech
8.2.2. Developing the Main Method
33
Copyright © 2007 SaiGon Tech
9. Running the Test
Click the red circle run TestResultTest
Result
34
Copyright © 2007 SaiGon Tech
II. Loan Verifier Problem
A loan request consists of annual salary and number of
years on current job of the customer.
Develop a program to 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.
35
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Improving the Design
6. Developing the Test
7. Developing the Main Class
36
Copyright © 2007 SaiGon Tech
1. Examples
Annual Salary: |----------------------30,000---------------------|
Years on Job: |----------------------2--------------------------|
Thus, we should have 9 examples:
1. (Annual Salary < 30,000) && (Years on Job < 2)
2. (Annual Salary < 30,000) && (Years on Job == 2)
3. (Annual Salary < 30,000) && (Years on Job > 2)
4. (Annual Salary == 30,000) && (Years on Job < 2)
5. (Annual Salary == 30,000) && (Years on Job == 2)
6. (Annual Salary == 30,000) && (Years on Job > 2)
7. (Annual Salary > 30,000) && (Years on Job < 2)
8. (Annual Salary > 30,000) && (Years on Job == 2)
9. (Annual Salary > 30,000) && (Years on Job > 2)
37
Copyright © 2007 SaiGon Tech
Example 1
Input: Annual Salary = $25,000; Years on Job = 1
Output: False (Not Qualified for loan)
Example 2:
Input: Annual Salary = $25,000$; Years on Job = 2
Output: False (Not Qualified for loan)
Example 3:
Input: Annual Salary = $25,000$; Years on Job = 3
Output: False (Not Qualified for loan)
38
Copyright © 2007 SaiGon Tech
Example 4
Input: Annual Salary = $30,000; Years on Job = 1
Output: False (Not Qualified for loan)
Example 5:
Input: Annual Salary = $30,000$; Years on Job = 2
Output: True(Qualified for loan)
Example 6:
Input: Annual Salary = $30,000$; Years on Job = 3
Output: True(Qualified for loan)
39
Copyright © 2007 SaiGon Tech
Example 7
Input: Annual Salary = $40,000; Years on Job = 1
Output: False (Not Qualified for loan)
Example 8:
Input: Annual Salary = $40,000$; Years on Job = 2
Output: True(Qualified for loan)
Example 9:
Input: Annual Salary = $40,000$; Years on Job = 3
Output: True(Qualified for loan)
40
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
41
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method: isQualified
Input parameter(s): Annual Salary, Years on Job
Return type: boolean
42
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
43
Copyright © 2007 SaiGon Tech
5. Improving the Design
44
Copyright © 2007 SaiGon Tech
6. Developing the Test
Project name : loan
Package name : loanrequest
45
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
Using if-statement inside isQualified()
46
Copyright © 2007 SaiGon Tech
Another implementation of isQualified()
47
Copyright © 2007 SaiGon Tech
III. Speed Of Sound Problem
A sound meter holds information about speed of sound in air, water, and
steel (see the table)
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
Medium Approximate speed (feet/second)
Air 1100
Water 4900
Steel 16400
48
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Developing the Test
6. Developing the Main Class
49
Copyright © 2007 SaiGon Tech
1. Examples
Example 1
Input: Medium: Air, Distance: 1100 feet
Output: 1(s)
Example 2:
Input: Medium: Water, Distance: 4900 feet
Output: 1(s)
Example 3:
Input: Medium: Steel, Distance: 16400 feet
Output: 1(s)
50
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
51
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method: getTime
Input parameter(s): Medium, Distance
Return type: double
52
Copyright © 2007 SaiGon Tech
4. Finding the Name of Class that Hosts the Main
Task
53
Copyright © 2007 SaiGon Tech
5. Developing the Test
Project name : SpeedOfSound
Package name : soundmeter
54
Copyright © 2007 SaiGon Tech
6. Developing the Main Class
55
Copyright © 2007 SaiGon Tech
IV. Guess Word Game Problem
The game hides a secret word and then asks users to
guess it
Check whether the user’s guess is correct or not.
Comparison should be case-insensitive
56
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Improving the Design
6. Developing the Test
7. Developing the Main Class
57
Copyright © 2007 SaiGon Tech
1. Examples
Secret Word : SAIGONTECH
No Input Output
1 sAIgONteCH False
2 SAIGONTECH True
3 PROSPERO False
58
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
59
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method(s): matches
Input parameter(s): secretWord, guessWord
Return type: boolean
60
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
61
Copyright © 2007 SaiGon Tech
5. Improving the Design
62
Copyright © 2007 SaiGon Tech
6. Developing the Test
Project name : GuessWordGame
Package name : guessword
63
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
64
Copyright © 2007 SaiGon Tech
V. Pet Food Problem
A pet food store has a price table for grades as below:
Pet Food Grade Price per lb. (cents)
A 30.0
B 20.0
C 10.0
Given a grade, determine its price
65
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Developing the Test
6. Developing the Main Class
66
Copyright © 2007 SaiGon Tech
1. Examples
Example 1
Input: Food Grade: ‘A’
Output: 30
Example 2
Input: Food Grade : ‘B’
Output: 20
Example 3
Input: Food Grade : ‘C’
Output: 10
67
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
68
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method(s): getPrice
Input parameter(s): Food Grade
Return type: double
69
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
70
Copyright © 2007 SaiGon Tech
5. Developing the Test
Project name : PetFood
Package name : petfood
71
Copyright © 2007 SaiGon Tech
6. Developing the Main Class
72
Copyright © 2007 SaiGon Tech
VI. 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 Sales Commission Rate
less than $10,000 5%
$10,000–14,999 10%
$15,000–17,999 12%
$18,000–21,999 15%
$22,000 or more 16%
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).
73
Copyright © 2007 SaiGon Tech
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 mount of pay:
Pay = Commission – Advanced pay
74
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Improving the Design
6. Developing the Test
7. Developing the Main Class
75
Copyright © 2007 SaiGon Tech
1. Examples
SalesPerson Input Output
Sales Advanced Pay Commission Gross Pay
1 $ 16,000 $0 $1,920 $1,920
2 $ 20,000 $0 $3,000 $3,000
3 $ 21,400 $1,500 $3,210 $1,710
4 $12,600 $1,500 $1,260 -$240
76
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
77
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method : getPay
Input parameter(s): Sales, Advanced Pay
Return type: double
78
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
79
Copyright © 2007 SaiGon Tech
5. Improving the Design
80
Copyright © 2007 SaiGon Tech
6. Developing the Test
Project name : Sales Commission
Package name : salescommission
81
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
82
Copyright © 2007 SaiGon Tech
VII. Consultant Charges 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.
83
Copyright © 2007 SaiGon Tech
Solution
1. Examples
2. Preliminary Class Design
3. Finding the Main Task
4. Finding a Class that Hosts the Main Task
5. Improving the Design
6. Developing the Test
7. Developing the Main Class
84
Copyright © 2007 SaiGon Tech
1. Examples
Example 1
Input: Hours worked: 4
Output: 250
Example 2
Input: Hours worked: 5
Output: 250
Example 3
Input: Hours worked: 10
Output: 500
85
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
86
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
Name of method(s): getTotalCharge
Input parameter(s): Hours
Return type: double
87
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
88
Copyright © 2007 SaiGon Tech
5. Improving the Design
89
Copyright © 2007 SaiGon Tech
6. Developing the Test
Project name : Consultant Charges
Package name : charge
90
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
91
Copyright © 2007 SaiGon Tech
References
1. Tony Gaddis & Godfrey Muganda – Starting Out with
Java From Control Structures through Data Structures–
Pearson Addison Wesley -2007- Chapter 3(pg. 107).
92
Copyright © 2007 SaiGon Tech