Click here to Skip to main content
15,031,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So, I need some help with an assignment I have. I will include the question and then my classes.

The first programming project involves writing a program that computes the salaries for a collection of employees of different types. This program consists of four classes.

1. The first class is the Employee class, which contains the employee's name and monthly salary, which is specified in whole dollars. It should have three methods:
a. A constructor that allows the name and monthly salary to be initialized.
b. A method named annualSalary that returns the salary for a whole year.
c. A toString method that returns a string containing the name and monthly salary, appropriately labeled.

2. The Employee class has two subclasses. The first is Salesman. It has an additional instance variable that contains the annual sales in whole dollars for that salesman. It should have the same three methods:

a. A constructor that allows the name, monthly salary and annual sales to be
initialized.

b. An overridden method annualSalary that returns the salary for a whole year. The
salary for a salesman consists of the base salary computed from the monthly salary
plus a commission. The commission is computed as 2% of that salesman's annual
sales. The maximum commission a salesman can earn is $20,000.

c. An overridden toString method that returns a string containing the name,
monthly salary and annual sales, appropriately labeled.

3. The second subclass is Executive. It has an additional instance variable that
reflects the current stock price. It should have the same three methods:

a. A constructor that allows the name, monthly salary and stock price to be
initialized.

b. An overridden method annualSalary that returns the salary for a whole year. The
salary for an executive consists of the base salary computed from the monthly
salary plus a bonus. The bonus is $30,000 if the current stock price is greater
than $50 and nothing otherwise.

c. An overridden toString method that returns a string containing the name,
monthly salary and stock price, appropriately labeled.

4. Finally there should be a fourth class that contains the main method. It should
read in employee information from a text file. Each line of the text file will
represent the information for one employee for one year. An example of how the
text file will look is shown below:

2014 Employee Smith,John 2000
2015 Salesman Jones,Bill 3000 100000
2014 Executive Bush,George 5000 55

The year is the first data element on the line. The file will contain employee information for only two years: 2014 and 2015. Next is the type of the employee followed by the employee name and the monthly salary. For salesmen, the final value is their annual sales and for executives the stock price. As the employees are read in, Employee objects of the appropriate type should be created and they should be stored in one of two arrays depending upon the year. You may assume that the file will contain no more than ten employee records for each year and that the data in the file will be formatted correctly.
Once all the employee data is read in, a report should be displayed on the console for each of the two years. Each line of the report should contain all original data supplied for each employee together with
2
that employee's annual salary for the year. For each of the two years, an average of all salaries for all employees for that year should be computed and displayed.

My code so far:

Java
public class Employee {
    private String name;
    private double monthly;
    
    public double annualSalary(double monthly) {
        return monthly * 12;
    }
            
    public String toString() {
        return "Name: " + name + "  Monthly Salary: $" + monthly;
    }
        
}

/* File name: Salesman.java */

package project.pkg1;

public class Salesman extends Employee {
    private double annual;
    
    public Salesman(String name, double monthly, double annual) {
        super(name, monthly);
        annual = monthly * 12;
    }

    public String toString() {
        return "Name: " + name + "  Monthly Salary: $" + monthly + "  Annual Salary: $" + salary;
    }
            
}

/* File name: Executive.java */

package project.pkg1;

public class Executive extends Employee {
    private double stock;
    
    public Executive(string name, double monthly, double stock) {
        super(name, monthly) 
    }
}


What I have tried:

My compiler is showing an error on the "super" lines. The error is as follows:

constructor Employee in class Employee cannot be applied to given types:

required: no arguments
found: String, double
reason: actual and formal arguments differ in length
Posted
Updated 30-Jun-21 18:38pm
v4
Comments
[no name] 4-Jun-17 16:23pm
   
>"My compiler is showing an error on the "super" lines"
Maybe you could also tell us what the error message is?
Member 13240253 4-Jun-17 16:26pm
   
constructor Employee in class Employee cannot be applied to the given types:

required: no arguments
found: String, double
reason: actual and formal argument differ in length
[no name] 4-Jun-17 16:29pm
   
Please use Editing a Question[^] and add this Information to your question.
Patrice T 4-Jun-17 16:33pm
   
Use Improve question to update your question.

It seems you wouldn't pay much attention at classes... It is all about inheritance and its rules...
You have a class called Employee with a default constructor, that has no parameters whatsoever... (it is default because you did not created any).
Now you have an other class called Salesman that inherits (extends in Java) Employee with a public constructor that takes 3 parameters... That constructor calls the constructor on the base class (Employee) by super and passes to it the parameters it got... But Employee has no constructor that can accept parameters (see above)...
That what it means when the compiler wrote you: actual and formal arguments differ in length...
actual - the parameters that you pass now (string, double)
formal - what have to be expected according the definition (void)

We may go further, but I think that's the point when it becomes YOUR homework...
   
v2
   
Implement simple inheritance where Employee (employee ID, First Name, Last

Name, Current salary) is super class, consider Manager (number of stock options) and SalesPerson (number of sales, commission rate) as subclasses.
   
Comments
Dave Kreskowiak 1-Jul-21 0:43am
   
I seriously doubt the OP is still looking for an answer to their homework question four years later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900