-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerSalary.java
More file actions
56 lines (48 loc) · 1.63 KB
/
Copy pathWorkerSalary.java
File metadata and controls
56 lines (48 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Superclass: Worker
class Worker {
String name;
double salaryRate;
// Constructor
Worker(String name, double salaryRate) {
this.name = name;
this.salaryRate = salaryRate;
}
// Method to compute pay (to be overridden in subclasses)
double computePay(int hours) {
return 0; // Placeholder, overridden in subclasses
}
}
// Subclass: DailyWorker
class DailyWorker extends Worker {
DailyWorker(String name, double salaryRate) {
super(name, salaryRate);
}
// Computes pay based on the number of days worked (8 hours per day)
@Override
double computePay(int hours) {
int daysWorked = hours / 8; // Convert hours to days
return salaryRate * daysWorked;
}
}
// Subclass: SalariedWorker
class SalariedWorker extends Worker {
SalariedWorker(String name, double salaryRate) {
super(name, salaryRate);
}
// Fixed weekly wage for 40 hours per week
@Override
double computePay(int hours) {
return salaryRate * 40;
}
}
// Main class to test the program
public class WorkerSalary {
public static void main(String[] args) {
// Creating instances of DailyWorker and SalariedWorker
Worker dailyWorker = new DailyWorker("John", 500);
Worker salariedWorker = new SalariedWorker("Alice", 1000);
// Calculating and displaying weekly pay
System.out.println("Daily Worker's Weekly Pay (40 hours): " + dailyWorker.computePay(40));
System.out.println("Salaried Worker's Weekly Pay (50 hours): " + salariedWorker.computePay(50));
}
}