Results 1 to 7 of 7
  1. #1
    Benji is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Problem with Inheritance assignment (Employee, Salaried, Company classes)

    I'm new to programming. I've been working on an assignment for my java class. It involves inheritance with several classes. I've posted what I have done so far. I'm sure most programmers will see mistakes throughout, but I'm really having issue with the Company class. Please see my instructions below. Any help will be much appreciated.

    Develop a Java program that uses:
    1) Inheritance constructs
    a) overriding superclass methods
    b) invoking super class methods
    c) accessing superclass instance variables
    d) abstact classes and methods
    2) Polymorphism
    3) Container operations, access all elements

    1) We are going to develop a program that implements inheritance to process employees in an organization. The program will consist of the following classes: Employee, HourlyEmployee, SalariedEmployee, CommissionedEmployee and Company and CompanyTester.

    6) The Company Class
    a) Holds and manages all the employee objects.
    b) Instance fields
    i) Name of company
    ii) A container (ArrayList or Array) to hold the abstract classes employee objects. ArrayList are not covered until Chapter 14 but they are fairly simple to use if you’d like to use them in this project.
    c) Methods
    i) Constructor - sets the instance field from parameter and creates the container.
    ii) Default Constructor - sets the instance field to a default value and creates the container
    iii) Add an employee to the container
    iv) Get an employee by employee Id
    v) Get the number of employees in the company
    vi) Get the total payroll for the company assuming all employees work a 40 hour work week.
    vii) toString method that returns a neatly formatted string the company name and each employees information (employees name, id number and weekly pay (assume 40 hour work week).


    Java Code:
    public abstract class [B]Employee[/B]{
    
    	private String employeeName;
    	private int IDnum;
    	private static int nextIDnum = 1000;
    	
    	public Employee(String n, int ID){
    		setEmployeeName(n);
    		setIDnum(ID);
    	}
    	
    	public Employee(){
    		employeeName = "";
    		IDnum = 0;
    		nextIDnum = 1000;
    	}
    	
    	public void setIDnum(int ID) {
    		IDnum = nextIDnum + 1;
    	}
    	
    	public int getIDnum(){
    		return IDnum;
    	}
    	
    	public void setEmployeeName(String n) {
    	}
    	
    	public String getEmployeeName(){
    		return employeeName;
    	}
    	
    	abstract double computePay();
    	
    	public String toString(){
    		String employeeInfo = getEmployeeName() + "\t ID number: " + getIDnum();
    		return (employeeInfo);
    	}
    	
    }
    
    
    public class [B]HourlyEmployee extends Employee [/B]{
    
    	private double hourlyRate;
    	private int weeklyHours;
    	private double hourlyPay;
    	
    	public HourlyEmployee(double hr, int wh, double wp){
    		setHourlyRate(hr);
    		setWeeklyHours(wh);
    		setHourlyPay(wp);
    	}
    	
    	public void setWeeklyHours(int wh) {
    		
    		
    	}
    
    	public void setHourlyRate(double hr) {
    		
    		
    	}
    	
    	public void setHourlyPay(double wp) {
    		
    		
    	}
    
    	public HourlyEmployee(){
    		hourlyRate = 0.0;
    		weeklyHours = 0;
    		hourlyPay = 0.0;
    	}
    	
    	public double getHourlyRate(){
    		return hourlyRate;
    	}
    	
    	public int getWeeklyHours(){
    		return weeklyHours;
    	}
    	
    	public double getHourlyPay(){
    		return hourlyPay;
    	}
    
    	@Override
    	double computePay() {
    		if(weeklyHours <= 40)
            {
                this.hourlyPay = this.hourlyRate * weeklyHours;
            }
            else
            {
               this.hourlyPay = this.hourlyRate * 40;
               int extra = weeklyHours - 40;
               double overTime = (extra * 40) * 1.5;
               this.hourlyPay += overTime;
            }
    		return this.hourlyPay;
        }
    	
    	public String toString(){
    		String employeeInfo = getEmployeeName() + "\t ID number: " + getIDnum() + "\t Hourly Pay Rate: " + hourlyPay;
    		return (employeeInfo);
    	}
    }
    
    public class [B]SalariedEmployee extends Employee [/B]{
    
    	private double weeklySalary;
    	
    	public SalariedEmployee(double ws){
    		setWeeklySalary(ws);
    	}
    	
    	public void setWeeklySalary(double ws) {
    		
    		
    	}
    
    	public SalariedEmployee(){
    		weeklySalary = 0.0;
    	}
    	
    	public double getWeeklySalary(){
    		return weeklySalary;
    	}
    	
    	@Override
    	double computePay() {
    		
    		return this.weeklySalary;
        }
    	
    	public String toString(){
    		String employeeInfo = getEmployeeName() + "\t ID number: " + getIDnum() + "\t Pay Rate: " + weeklySalary;
    		return (employeeInfo);
    	}
    }
    
    public class [B]CommissionedEmployee extends SalariedEmployee[/B]{
    
    	private int weeklySales;
    	private double commissionPer;
    	private double commission;
    	
    	public CommissionedEmployee(int wks, double cp){
    		setWeeklySales(wks);
    		setCommissionPer(cp);
    	}
    	
    	public void setWeeklySales(int wks) {
    		
    		
    	}
    
    	public void setCommissionPer(double cp) {
    		
    		
    	}
    	
    	public CommissionedEmployee(){
    		weeklySales = 0;
    		commissionPer = 0.0;
    	}
    	
    	public int getWeeklySales(){
    		return weeklySales;
    	}
    	
    	public double getCommissionPer(){
    		return commissionPer;
    	}
    	
    	@Override
    	double computePay() {
    		
    		this.commission = this.weeklySales * this.commissionPer;
    		return this.commission + this.getWeeklySalary();
        }
    	
    	public String toString(){
    		String employeeInfo = getEmployeeName() + "\t ID number: " + getIDnum() + "\t Pay Rate: " + this.getWeeklySalary() + "\t plus: " + this.commission + "Commission Earned";
    		return (employeeInfo);
    	}
    }
    
    import java.util.Arrays;
    
    public class [B]Company [/B]{
    	
    	private String companyName; 
    
    	public String toString() {
            return Arrays.asList(companyName).toString();
        }
        Employee[] org = new Employee[] {
        };

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    You have posted the assignment and the code but have not asked a specific question. Where are you having problems?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Benji is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    I guess with the Company Class and the array list. With the methods listed below I guess they are all tied into the Array list?

    c) Methods
    i) Constructor - sets the instance field from parameter and creates the container.
    ii) Default Constructor - sets the instance field to a default value and creates the container
    iii) Add an employee to the container
    iv) Get an employee by employee Id
    v) Get the number of employees in the company
    vi) Get the total payroll for the company assuming all employees work a 40 hour work week.
    vii) toString method that returns a neatly formatted string the company name and each employees information (employees name, id number and weekly pay (assume 40 hour work week).

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    Quote Originally Posted by Benji View Post
    I guess with the Company Class and the array list. With the methods listed below I guess they are all tied into the Array list?
    That still doesn't tell me anything. Are you getting compilation errors? When you run the program, what doesn't it do that it is supposed to do? Are any exceptions being thrown? Post all error messages, input, actual output, and expected output.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Benji is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    Ok, so please see below I made a driver class. I kind of new the output would be wrong and I'm still confused about the company class and how to use the Array. Does this help?

    output:
    Employees processed at Big Business Company:

    ID number: 0 Hourly Pay Rate: 0.0
    earned: $0.00

    ID number: 0 Pay Rate: 0.0
    earned: $0.00

    ID number: 0 Pay Rate: 0.0 plus: 0.0 Commission Earned
    earned: $0.00



    Java Code:
    public class CompanyTester {
    
    	public static void main(String[] args) {
    		
    		HourlyEmployee hourlyEmployee = new HourlyEmployee("Johnson", "4321", 16.75);
    		SalariedEmployee salariedEmployee = new SalariedEmployee("Smith", "1234", 800.00);
    		CommissionedEmployee commissionedEmployee = new CommissionedEmployee("Jones", "5678", 800.00, .06);
    		
    		System.out.println("Employees processed at Big Business Company:\n");
    		System.out.printf("%s\n%s: $%,.2f\n\n", hourlyEmployee, "earned", hourlyEmployee.getHourlyPay());
    		System.out.printf("%s\n%s: $%,.2f\n\n", salariedEmployee, "earned", salariedEmployee.getWeeklySalary());
    		System.out.printf("%s\n%s: $%,.2f\n\n", commissionedEmployee, "earned", commissionedEmployee.getCommissionPer());
    		
    
    
    	}
    
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    Well the reason for 0 output is probably because your set methods are not actually setting anything.

    For arrays, you need to allocate their length (Lists are better than arrays but I am presuming you are required to use arrays).

    Java Code:
    Employee [] employees = new Employee[5]; // holds five employees indexed from 0 to 4
    
    employees[0] = new Employee( args here);
    employees[1] = new Employee( args here);
    employees[2] = new Employee( args here);
    employees[3] = new Employee( args here);
    employees[4] = new Employee( args here);
    You can also initialize them like this:

    Java Code:
    Employee[] employees = new Employee[] { // size is dependent on number of initializers
    new Employee(args here),
    new Employee(args here),
    new Employee(args here),
    new Employee(args here),
    };
    Check out the tutorials in my signature for more information on Java
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Benji is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default Re: Problem with Inheritance assignment (Employee, Salaried, Company classes)

    Ok, i'll implement this and look at the tutorials. Thanks a lot Jim!

Similar Threads

  1. Replies: 3
    Last Post: 07-02-2015, 05:30 PM
  2. Troubling compiling Classes and Objects assignment!
    By Gaebril in forum New To Java
    Replies: 6
    Last Post: 03-19-2011, 12:05 AM
  3. Class employee,company,test: Read data from text file.
    By jeskoston in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 01:50 AM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM
  5. Help with classes and inheritance task.
    By checho in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •