Calling super Methods

Lab Java Intermediate 20 points

Create Employee(name, salary) with double pay() = salary, and Manager that adds a bonus: super.pay() + bonus. Read employees and print each pay with 2 decimals and the total.

Input

n, then lines E name salary or M name salary bonus.

Output

n lines name pay, then total X.

Example

Input

3
E ann 3000
M bob 5000 1200.5
E cy 2500

Output

ann 3000.00
bob 6200.50
cy 2500.00
total 11700.50
Sample input available Sample output available
Editor
Java

Hints

super.pay() runs the parent's version of the method.

Solution explanation

Using super.pay() means a later change to Employee.pay() automatically reaches managers too.

Show solution
import java.util.Scanner;

class Employee {
    final String name;
    final double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    double pay() {
        return salary;
    }
}

class Manager extends Employee {
    final double bonus;

    Manager(String name, double salary, double bonus) {
        super(name, salary);
        this.bonus = bonus;
    }

    @Override
    double pay() {
        return super.pay() + bonus;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double total = 0;
        for (int i = 0; i < n; i++) {
            String kind = sc.next();
            String name = sc.next();
            double salary = sc.nextDouble();
            Employee e = kind.equals("M") ? new Manager(name, salary, sc.nextDouble()) : new Employee(name, salary);
            System.out.printf("%s %.2f%n", e.name, e.pay());
            total += e.pay();
        }
        System.out.printf("total %.2f%n", total);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD