Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Contents
A. Payroll Problem
B. Solutions
1 Functional Programming Solution
2 Object-Oriented Programming Solution
2
Copyright © 2007 SaiGon Tech
A. Payroll Problem
Given a number of working hours and a
payment per hour, calculate the payment
total.
Payment total = working hours x payment
per hour
3
Copyright © 2007 SaiGon Tech
B. Solutions
1 Functional Programming Solution
2 Object-Oriented Programming
Solution
4
1 Functional Programming
Solution
1.1. Problem Description
1.2. Function Header Definition
1.3. Function Test Cases
1.4. Function Template
1.5. Function Implementation
1.6. Function Testing
5
Copyright © 2007 SaiGon Tech
1.1. Problem Description
Given: a number of working hours and a
payment per hour
Determine: the payment total
6
Copyright © 2007 SaiGon Tech
1.2. Function Header
Definition
Determine function name
Determine function parameters
7
Copyright © 2007 SaiGon Tech
1.3. Function Test Cases
Working hours = 4, payment per hours =
$40
Working hours = 2, payment per hours =
$45
Working hours = 1, payment per hours =
$20
8
Copyright © 2007 SaiGon Tech
1.4. Function Template
Payment = working hours x payment per
hours
(* … …)
(… working-hours payment-per-hour)
9
Copyright © 2007 SaiGon Tech
1.5. Function Implementation
(* … …)
(… working-hours payment-per-hour)
(* working-hours payment-per-hour)
10
Copyright © 2007 SaiGon Tech
1.6. Function Testing
11
Copyright © 2007 SaiGon Tech
2 Object – Oriented
Programming Solution
2.1. Creating a New Java Project
2.2. Creating a New Java Package
2.3. Creating a Test Class
2.4. Developing a Test Method
2.5. Developing the Main Class
2.6. Running Test
12
Copyright © 2007 SaiGon Tech
2.1. Creating a New Java Project
Open Eclipse IDE
File > New > Java Project
Enter Project Name > Finish
13
Copyright © 2007 SaiGon Tech
2.2. Creating a New Java
Package
File > New > Package
Enter Source Folder Name > Package
Name > Finish
14
Copyright © 2007 SaiGon Tech
2.3. Creating a Test Class
File > New > JUnit Test Case
Enter Test Class Name > Finish
15
Copyright © 2007 SaiGon Tech
2.4. Developing a Test
Method
2.4.1 Determine Which Object That Has
Responsibility To Do Calculation
2.4.2 Determine Which Object Behavior
Exactly Does Calculate Payment Total
2.4.3 Assert Expected Value With Result
Returns From Calling Object Method
16
Copyright © 2007 SaiGon Tech
2.4.1 Determine Which Object
That Has Responsibility To Do
Calculation
Identify object name
Payment
Calculator
…new PaymentCalculator()…
17
Copyright © 2007 SaiGon Tech
2.4.2 Determine Which Object
Behavior Exactly Does Calculate
Payment Total
Identify method name
Identify method parameters
calculatePayment(working
Payment Hours, paymentPerHour)
Calculator
… new PaymentCalculator().calculatePayment(…, … ) …
18
Copyright © 2007 SaiGon Tech
2.4.3 Assert Expected Value With
Result Returns From Calling Object
Method
assertEquals(40, new
PaymentCalculator().calculatePayment(40, 1))
19
Copyright © 2007 SaiGon Tech
2.5. Developing the Main
Class
2.5.1 Generate PaymentCalculator Class
2.5.2 Generate calculatePayment Method
2.5.3 Implement calculatePayment Method
20
Copyright © 2007 SaiGon Tech
2.5.1 Generate
PaymentCalculator Class
Click on x sign – Pick Create class
‘PaymentCalculator’ > Finish
21
Copyright © 2007 SaiGon Tech
2.5.2 Generate
calculatePayment Method
Click on x sign – Pick Create method
‘calculatePayment(int, int)’
22
Copyright © 2007 SaiGon Tech
2.5.4 Implement
calculatePayment Method
return dHours *
dPaymentPerHour;…;
23
Copyright © 2007 SaiGon Tech
2.6. Run Test
Run the CalculatePaymentTest class
24
Copyright © 2007 SaiGon Tech
Reference
1. Procedure programming and object
oriented programming:
Chapter 1 – Introduction - Starting Out
with Java From Control Structures through
Data Structures 1e
2. Class, Packet, Method, Variable, Object,
Integer type, java syntax:
Chapter 2 – Java Fundamentals - Starting
Out with Java From Control Structures
through Data Structures 1e
25
Copyright © 2007 SaiGon Tech
History
1. 21/03/2008 Version 1.0 by Trần Thanh
Giảng
(Please change history when edit)
26
Copyright © 2007 SaiGon Tech