THINK FIRST·CODE LATER

← All labs

Payable interface and polymorphism

Problem

Write an interface Payable with one method double pay().

Write two classes implementing it:

  • HourlyWorker(String name, double hours, double rate) — pay() returns hours * rate, and hours above 40 are paid 1.5 times;
  • SalariedWorker(String name, double monthly) — pay() returns monthly.

PayrollApp reads: the hourly worker's name, hours and rate, then the salaried worker's name and monthly salary. It puts both in a Payable[] array, prints each pay with two decimals as name: 1000.00, then the total as Total: 3000.00.

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-1-data-1.txt

Ousmane 45 20 Amina 3000
What your program must print for l15-1-data-1.txt
Ousmane: 950.00

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-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content l15-1-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.