← Back to Week 1 Hub

Payroll Calculator 2 (Refactor)

Same program, better structure — break main() into methods

Workbook 1c, p.119 — Project name: payroll-calculator-2

In Plain English

You already built a payroll calculator. Now reorganize it. The program does the exact same thing — but instead of having all the code inside main(), break it into separate methods. Each method should do ONE job: one gets input, one calculates pay, one displays results. The output doesn't change — only the structure of your code does.

What a Successful Run Looks Like
Important: The output is identical to your original payroll calculator. The difference is HOW your code is organized.

Run 1 — With Overtime

Enter your name: Alice Enter hours worked: 45 Enter pay rate: 20 Name: Alice Gross Pay: $950.00

40 regular hours at $20 = $800, plus 5 overtime hours at $30 (1.5x) = $150. Total: $950.00

Run 2 — No Overtime

Enter your name: Bob Enter hours worked: 35 Enter pay rate: 15 Name: Bob Gross Pay: $525.00

35 hours at $15 = $525.00. No overtime because hours did not exceed 40.

What This Exercise Practices

This exercise reinforces these concepts from Week 1:

Why this matters: Refactoring is a core skill. Breaking code into methods makes it easier to read, test, and reuse. When main() reads like a story, anyone can understand what the program does without reading every detail.
Flow Diagram
Before (Original)
main()
- ask for name
- ask for hours
- ask for pay rate
- check overtime
- calculate gross pay
- display results
After (Refactored)
main()
↓ calls
getEmployeeInfo(scanner)
↓ calls
calculateGrossPay(hours, rate)
↓ calls
displayEmployeeInfo(name, grossPay)
Key idea: main() should read like a story — each method call describes what happens next. The details live inside each method, not all crammed into one place.

Workbook 1c, p.119 — Exercises: Calculator with Methods (Exercise 2)

← Method Practice Rental Car Calculator →