Question

This is super class of all,Employee class.

import java.util.Scanner;
import java.util.Calendar;
import java.util.*;

class Employee {
    Scanner in = new Scanner(System.in);
    Calendar cal = Calendar.getInstance();
    String name;
    String number;
    int month;
    int week;
    double pay;

    void load() {
        System.out.println("Enter name of employee");
        name = in.nextLine();
        System.out.println("Enter social security number");
        number = in.nextLine();
        System.out.println("Enter employee's birthday month(1-12)");
        month = in.nextInt();
        System.out.println("Enter employee's birthday week(1-4)");
        week = in.nextInt();

    }

    public String toString() {
        return "employee : " + name + "  social security number : " + number
                + "  paycheck : $" + pay;
    }

    void getBonus() {
        int mont = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (month == mont + 1 && week == (day / 7) + 1)
            pay = pay + 100;
    }

}

This is subclass of employee .

import java.util.Scanner;

class Hourly extends Employee {
    Scanner in = new Scanner(System.in);
    double pay;
    int hpay;
    int hours;

    void load() {
        System.out.println("Enter hourly pay");
        hpay = in.nextInt();
        System.out.println("Enter no. of hours worked last week");
        hours = in.nextInt();
    }

    double getEarnings() {
        if (hours > 40)
            pay = 1.5 * (hours - 40) * hpay + hpay * 40;
        else
            pay = hpay * hours;
        return pay;
    }
}

There are 2 more subclasses like these and finally i have test file.

import java.util.Scanner;

class driver {
    public static void main(String args[]) {
        int i;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter no. of employees");
        int a = in.nextInt();
        for (i = 1; i <= a; i++) {
            System.out
                    .println("Enter type : Hourly(1),Salaried(2),Salaried plus commision(3)");
            int b = in.nextInt();
            if (b == 1) {
                Hourly h = new Hourly();
                h.super.load();// error cannot find symbol h
                h.load();
                h.getEarnings();
            }
            if (b == 2) {
                Salaried s = new Salaried();

                s.load();
                s.getEarnings();
            }
            if (b == 3) {
                Salariedpluscommision sp = new Salariedpluscommision();
                sp.super();// error that super should be in first line but then
                            // where can i define sp
                sp.super.load();// cannot find symbol sp
                sp.load();
                sp.getEarnings();
            }

        }

    }
}

I have got 3 errors in these codes and as i am beginner i don't know how can i solve these errors. My program takes the employee's details from user and calculate paycheck of that employee. Also,I am confused in how can i print all employee's paychecks at last after user have completed giving input of all employee's details.Can i do these with an array ? But first,i have to remove these errors and also suggest my which topics are weak which i should focus more.

Thank you in advance

Was it helpful?

Solution

You appear to be a bit confused about the keyword super.

In the code

Salariedpluscommision sp=new Salariedpluscommision();
sp.super();//error that super should be in first line but then where can i define sp
sp.super.load();//cannot find symbol sp
sp.load();
sp.getEarnings();

the compiler is telling you that super cannot be used where you're using it.

Most likely you just don't need it at all in the driver code and the code

Salariedpluscommision sp=new Salariedpluscommision();
sp.load();
sp.getEarnings();

will do what you thought you needed these calls for.

Similarly, in the earlier code, you can likely just delete the line

h.super.load();// error cannot find symbol h

As it's coded however, you might need to call some superclass methods from your subclasses, which is what the keyword is for.

In Hourly and likely the other subclasses, you probably want to call the Employee load method within the subclass load method:

void load(){
    super.load();
    System.out.println("Enter hourly pay");
    hpay = in.nextInt();
    System.out.println("Enter no. of hours worked last week");
    hours = in.nextInt();
}

which appears to be what you were trying for with some of the non-compiling code in driver.

OTHER TIPS

Several suggestions:

  1. Make Employee an abstract class and add public abstract getEarnings(); method
  2. Simplify your Driver routine to use switch / case rather than multiple if statements
  3. Create an ArrayList to collect a list of all employees
  4. Use the "type" of employee to create the right kind ... use the same variable name though
  5. After creating the "right kind" of employee, load it and get the earnings .... (print out the earnings?
  6. Just create objects .. don't use "super

Consider the following for Driver:

import java.util.ArrayList;
import java.util.Scanner;

class driver {
public static void main(String args[]) {
    ArrayList<Employee> employees = new ArrayList<Employee>();
    Employee emp;
    double earnings;

    int i;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter no. of employees");
    int a = in.nextInt();
    for (i = 1; i <= a; i++) {
        System.out.println("Enter type : Hourly(1),Salaried(2),Salaried plus commision(3)");
        int b = in.nextInt();
        emp = null;
        switch (b) {
        case 1:
            emp = new Hourly();
            break;
        case 2:
            emp = new Salaried();
            break;
        case 3:
            emp = new Salariedpluscommision();
            break;
        default:
            System.out.println("You entered an invalid employee type.");
            break;
        }
        if (emp != null) {
            emp.load();
            earnings = emp.getEarnings();
            employees.add(emp);
            System.out.println("Earnings are: " + earnings + "for " + emp.getName());
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top