THINK FIRST·CODE LATER

← All labs

Payroll with an abstract class

Problem

Write an abstract class Employee (name, abstract double monthlyPay(), abstract String kind(), and a concrete toString() formatted %-10s %-10s %9.2f) and three concrete subclasses:

  • SalariedEmployee(name, annualSalary) — annual / 12;
  • HourlyEmployee(name, rate, hours) — hours above 160 in the month are paid 1.5 × the rate;
  • CommissionEmployee(name, base, sales, percent) — base + sales × percent / 100.

Payroll.main reads S name annual, H name rate hours or C name base sales percent until the end of input into an ArrayList<Employee>, prints each employee with one loop, then Total payroll: … and Highest paid: Omar (Hourly).

Why can you not write new Employee("x")?

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l15-2-data-1.txt

S Lina 96000
H Omar 60 180
C Mei 3000 50000 8
What your program must print for l15-2-data-1.txt
Salaried   Lina         8000.00
Hourly     Omar        11400.00
Commission Mei          7000.00
Total payroll: 26400.00
Highest paid: Omar (Hourly)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l15-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l15-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.