Professional Documents
Culture Documents
1
Copyright © 2007 SaiGon Tech
Contents
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
}
7
Copyright © 2007 SaiGon Tech Exit
C. Decision Structure Problems
8
Copyright © 2007 SaiGon Tech
I. Test Result Problem
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
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
15
Copyright © 2007 SaiGon Tech
5. Finding the Name of Class that Hosts the Main
Task
16
Copyright © 2007 SaiGon Tech
6. Improving the Design
17
Copyright © 2007 SaiGon Tech
7. Developing the Test
18
Copyright © 2007 SaiGon Tech
7.1. Creating the Project
19
Copyright © 2007 SaiGon Tech
7.2. Creating the Test Class
20
Copyright © 2007 SaiGon Tech
21
Copyright © 2007 SaiGon Tech
7.3. Developing the Test Method
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
26
Copyright © 2007 SaiGon Tech
8.1. Creating the Main Class Based On the Test
27
Copyright © 2007 SaiGon Tech
8.1.1. Generating the Main Class
28
Copyright © 2007 SaiGon Tech
8.1.2. Generating the 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
31
Copyright © 2007 SaiGon Tech
8.2.1. Generating the Main Method
32
Copyright © 2007 SaiGon Tech
8.2.2. Developing the Main Method
33
Copyright © 2007 SaiGon Tech
9. Running the Test
Result
34
Copyright © 2007 SaiGon Tech
II. Loan Verifier Problem
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)
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
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
45
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
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
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
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
54
Copyright © 2007 SaiGon Tech
6. Developing the Main Class
55
Copyright © 2007 SaiGon Tech
IV. Guess Word Game Problem
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
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
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
63
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
64
Copyright © 2007 SaiGon Tech
V. Pet Food Problem
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
69
Copyright © 2007 SaiGon Tech
4. Finding a Class that Hosts the Main Task
70
Copyright © 2007 SaiGon Tech
5. Developing the Test
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%
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
76
Copyright © 2007 SaiGon Tech
2. Preliminary Class Design
77
Copyright © 2007 SaiGon Tech
3. Finding the Main Task
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
81
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
82
Copyright © 2007 SaiGon Tech
VII. Consultant Charges Problem
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
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
90
Copyright © 2007 SaiGon Tech
7. Developing the Main Class
91
Copyright © 2007 SaiGon Tech
References
92
Copyright © 2007 SaiGon Tech