java i have a assignment of oop by using java can any body help me pz

: Design and implement these four classes Employee, MonthlySalEmployee, HourlySalEmployee, Company and implement the following functions:

⦁ Create an application class with N HourlySalaryEmployee objects and N MonthlySalEmployee objects.
⦁ Create constructors, accessor and mutator as per your need in the program
⦁ Should ask the user to input the data for the MonthlySalEmployee and parent class attribute, same for the HourlySalEmployee class
⦁ should calculate average salary of all Hourly Employee and Monthly Employee respectively
⦁ Displayinfo() should display all the information of the all the attributes of the class and parent class and salary.
⦁ calcSal() should calculate salary according to class criteria

Dani AI

Generated

Notes for (and acknowledging ’s point about learning): implement a small class hierarchy so behavior is clear and testable. Use an abstract Employee base to hold common fields and declare calcSal(); implement MonthlySalEmployee and HourlySalEmployee to override calcSal() and displayInfo(). Keep input/output in a separate Company/Application class that builds lists of objects and computes averages by calling each employee’s calcSal().

public abstract class Employee {
    protected String id, name;
    public Employee(String id, String name) { this.id = id; this.name = name; }
    public abstract double calcSal();
    public void displayInfo() {
        System.out.println("ID: " + id + ", Name: " + name + ", Salary: " + calcSal());
    }
}

public class MonthlySalEmployee extends Employee {
    private double monthlySalary;
    public MonthlySalEmployee(String id, String name, double monthlySalary) {
        super(id, name); this.monthlySalary = monthlySalary;
    }
    @Override public double calcSal() { return monthlySalary; }
}

public class HourlySalEmployee extends Employee {
    private double hourlyRate, hoursWorked;
    public HourlySalEmployee(String id, String name, double rate, double hours) {
        super(id, name); this.hourlyRate = rate; this.hoursWorked = hours;
    }
    @Override public double calcSal() { return hourlyRate * hoursWorked; }
}
public class CompanyApp {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<MonthlySalEmployee> monthly = new ArrayList<>();
        List<HourlySalEmployee> hourly = new ArrayList<>();
        // read N, loop to create objects and add to lists
        double avgMonthly = monthly.stream().mapToDouble(Employee::calcSal).average().orElse(0.0);
        double avgHourly   = hourly.stream().mapToDouble(Employee::calcSal).average().orElse(0.0);
        // display info and averages
    }
}

Practical tips: validate numeric input and guard against N == 0 to avoid divide-by-zero; use double for money and format output when printing; watch Scanner.nextLine() after nextInt() (consume the newline). Keep business logic (salary calc, display) inside model classes and keep Scanner/IO in the application class for easier testing and maintenance.

Recommended Answers

All 2 Replies

Hello. We can help you learn how to do this, but we won’t do it for you. What have you got so far?

commented: but i have only 2 hour +0

AMNA_5 commented: but i have only 2 hour

Then I'm sorry, but you have left it far too late. All you can do is to learn from this mistake and start your next project in good time.

commented: ok now u can help me now i have 2 days for this +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.