0% found this document useful (0 votes)
7 views

Java_CH_10_exercise_864a2b12-0200-4215-8bed-7c130c8ccd03

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java_CH_10_exercise_864a2b12-0200-4215-8bed-7c130c8ccd03

Uploaded by

wwangyibo17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 10

1. A company pays its employees on a weekly basis. The employees are of two types:
Salaried employees are paid a fixed weekly salary, hourly employees are paid by the hour and
receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in excess of
40 hours. Write an application that performs its payroll calculations polymorphically.
//Employee.java
public abstract class Employee {
public String firstName;
public String lastName;

public Employee(String f, String l) {


firstName = f;
lastName = l;
}
public String toString() {
return String.format("Name: %s %s", firstName,lastName);
}
public abstract double earning();
}
// SalariedEmployee.java
public class SalariedEmployee extends Employee {
public double salary;
public SalariedEmployee(String f, String l, double s) {
super(f, l);
salary = s;
}
public double earning() {
return salary;
}
public String toString() {
String n = super.toString();
String s = "Salary: " + salary;
String e = "Total Earn: " + earning();
String info = String.format(" %s \n %s \n %s \n",n,s,e);
return info;
}
}
// HourlyEmployee.java
public class HourlyEmployee extends Employee {
public double wage;
public double hours;
public HourlyEmployee(String f, String l, double w, double h) {
super(f, l);
wage = w;
hours = h;
}
public double earning() {
if ( hours <= 40 )
return wage * hours;
else
return 40 * wage + ( hours - 40 ) * wage * 1.5;
}
public String toString() {
String n = super.toString();
String w = "Wage: " + wage;
String h = "Hours: " + hours;
String e = "Total Earn: " + earning();
String info = String.format(" %s \n %s \n %s \n %s \n",n,w,h,e);
return info;
}
}
//PayRollSystem.java
public class PayRollSystem {
public static void main(String[] args) {
Employee e1 = new SalariedEmployee("John","Smith",1538);
Employee e2 = new HourlyEmployee("Alex","Vance",25,42);
System.out.println(e1.toString());
System.out.println(e2.toString());
}
}
Output:
Name: John Smith
Salary: 1538.0
Total Earn: 1538.0

Name: Alex Vance


Wage: 25.0
Hours: 42.0
Total Earn: 1075.0

2. Write a program that demonstrate the use of interfaces for different animals. Create three
interfaces: Walkable, Swimmable and Flyable. Each interface should declare a method that
describe the animal’s ability. Create two animal classes that implement any or all of these
interfaces and display the result.

public interface Walkable {


void walk();
}
public interface Swimmable {
void swim();
}
public interface Flyable {
void fly();
}
public class Duck implements Walkable, Swimmable, Flyable {
public void walk() {
System.out.println("Duck can walk by using its legs.");
}
public void swim() {
System.out.println("Duck can swim by using its legs.");
}
public void fly() {
System.out.println("Duck can fly by using its wings.");
}
}
public class Fish implements Swimmable {
public void swim() {
System.out.println("Fish can swim by using its fins.");
}
}
public class AnimalTest {

public static void main(String[] args) {

System.out.println("Animals That Can Walk:");


Walkable duck1 = new Duck();
duck1.walk();
System.out.println();
System.out.println("Animals That Can Swim:");
Swimable duck2 = new Duck();
Swimable fish = new Fish();
duck2.swim();
fish.swim();

}
Output:
Animals That Can Walk:
Duck can walk by using its legs.

Animals That Can Swim:


Duck can swim by using its legs.
Fish can swim by using its fings.

You might also like