Hi Everyone,

I just need a little bit of help figuring out what i'm doing wrong in my code that the salary keeps output as zero. I'm writing a code that is suppose to determine whether the employee is a staff or faculty and then calculate and output the salary.
Below is my code:

Driver Class:

//driver class
public class DriverEmployee
{

	public static void main(String[] args)
	{
		Faculty faculty1 = new Faculty("James", 1999);
		Faculty faculty2 = new Faculty("Adam", 1997);
		Faculty faculty3 = new Faculty("Judy", 2001);

		Staff staff1 = new Staff("Tom", 1975);
		Staff staff2 = new Staff("Frank", 1980);

	System.out.println(faculty1.toString() + "\n" );
	System.out.println(faculty2.toString() + "\n" );
	System.out.println(faculty3.toString() + "\n" );
	System.out.println(staff1.toString() + "\n");
	System.out.println(staff2.toString() + "\n");

	}
}

Super Class:

//super class
public class Employee
{

	public String name;

	//the year that the employee starts to work at mason
	public int year;

	//the boolean variable to determine if the person is a faculty
	public boolean faculty;
	public double rate;
	public double salary;

	public Employee(){}

	public Employee(String empName, int yr)
	{
		this.name = empName;
		this.year = yr;
	}

	public int getYear()
	{
		return this.year;
	}

	public String getName()
	{
		return this.name;
	}

	public double getSalary()
	{
		return this.salary;
	}

	public void setYear(int yr)
	{
		if(year > 1957)
			year = yr;
	}

	public String toString()
	{
		return "Name: " + name + "\n Salary: $ " + salary;
	}

	public double getRate()
	{
		return salary;
	}

}

Subclass:

//subclass
class Staff extends Employee
{
	public Staff(){}

	public Staff(String empName, int yr)
	{
		this.name = empName;
		this.year = yr;
	}

	public double getRate()
	{
		salary = 5000*(Math.pow(1.03, (super.getYear()-1957)));
		return salary;

	}


}

Subclass:

//subclass
class Faculty extends Employee
{
	public Faculty(){}

	public Faculty(String empName, int yr)
	{
		this.name = empName;
		this.year = yr;
	}

	public double getRate()
	{
		salary = 6000*(Math.pow(1.05, (super.getYear()-1957)));
		return salary;

	}

}

Where do you output the value of salary? If it's printed on the console, please copy and paste the program's output here to show us what is output.

A debugging technique is to print out the value of a variable every time it is changed. Print with an id String to show where it was printed.

attached is how my output looks

A debugging technique is to print out the value of a variable every time it is changed. Print with an id String to show where it was printed.

I'm a little confused what you mean by this.

To print out the value of varible: var on the console, use: System.out.println("var=" + var);

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Name: James
Salary= 0.0

Name: Adam
Salary= 0.0

Name: Judy
Salary= 0.0

Name: Tom
Salary= 0.0

Name: Frank
Salary= 0.0

Press any key to continue . . .

You left out the debugging output that shows the values of salary as they are changed in the program.
If you did add those printlns and nothing was printed, that means that your code doesn't ever change the value from zero. So its 0.0 when it prints.
Then look at your logic to see why you are not calling the methods that change the value of salary.

Do you mean I need to add

System.out.println("Salary = " + salary);

in various methods of the program to see if the value for salary ever changes?

I did try to do that before I made this post, and I believe the values aren't getting passed to be calculated, thats what i'm not seeing. Am a missing a method? or returning the wrong values?

Where is the value of salary changed?
Do you call that method anywhere?
If you don't call the method to change the value of salary, it will stay at 0.

as of now the value of salary isn't changing throughout.

the values, name & year are passed from the driver to the super class
in the super class i call the getRate() method from the subclasses where the salary should be calculated and returned to the superclass to output using the toString() method.

the value of salary isn't changing throughout.

If you want it to change, you'll have to execute the code that changes it.

to begin with salary is declared to be 0, right?

in the method getRate() is where i execute the code to assign a new value to it, where it should change right?

where it should change

I don't know about what should be changed. But in the code you posted, it looks like in the getRate() method is where it is changed.

The name: getRate() does not sound like a method that calculates salary.

Member Avatar for ztini

The issue lies with your constructors, you never use getRate to set the salary. Also, its a good idea to match getters/setters. That is, you should have a get that matches a set and vice verse.

In your case, both subclasses are essentially the same except for their salary. So, you can therefore abstract all up to the super class, like this:

import java.text.NumberFormat;


public abstract class Employee {
	
	private String name;
	private double salary;
	private int year;

	public Employee(String name, int year) {
		setName(name);
		setYear(year);
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public double getSalary() {
		return salary;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getYear() {
		return year;
	}
	
	@Override
	public String toString() {
		return "Name: " + getName() + "\nSalary: $ " + 
			NumberFormat.getCurrencyInstance().format(getSalary());
	}
}
public class Staff extends Employee {

	public Staff(String name, int year) {
		super(name, year);
		setSalary(5000 * Math.pow(1.03, getYear() - 1957));
	}
	
}
public class Faculty extends Employee {

	public Faculty(String name, int year) {
		super(name, year);
		setSalary(6000 * Math.pow(1.05, getYear() - 1957));
	}
	
}

Oh, thank you. Sorry I didn't see your post earlier but I did manage to get the problem solved. Here is what my code looks like now:

Driver Class:

public class DriverEmployee
{
	public static void main(String[] args)
	{


		Faculty faculty1 = new Faculty("James", 1999);
		Faculty faculty2 = new Faculty("Adam", 1997);
		Faculty faculty3 = new Faculty("Judy", 2001);

		Staff staff1 = new Staff("Tom", 1975);
		Staff staff2 = new Staff("Frank", 1980);

		System.out.println(faculty1.toString());
		System.out.println(faculty2.toString());
		System.out.println(faculty3.toString());
		System.out.println(staff1.toString());
		System.out.println(staff2.toString());


	}
}

SuperClass:

public class Employee
{
	public String name;
	public int year;
	public double salary;
	public double max;
	public int i;

	public Employee(String empName, int yr)
	{
		this.name = empName;
		this.year = yr;

	}


	public int getYear()
	{
		return this.year;
	}

	public String getName()
	{
		return this.name;
	}

	public void setYear(int yr)
	{
		if(year > 1957)
			year = yr;
	}

	public void setName(String empName)
	{
		name = empName;
	}

	public double getSalary()
	{
		return this.salary;
	}


	public String toString()
	{
		return "Name: " + name + "\nSalary: $ " + getSalary() + "\n";
	}

}

Subclass:

public class Faculty extends Employee
{
	public Faculty(String empName, int yr)
	{
		super(empName, yr);
	}

	public double getSalary()
	{
		salary = 60000*(Math.pow(1.05, (super.getYear()-1957)));
		return salary;
	}
}

Subclass:

public class Staff extends Employee
{
	public Staff(String empName, int yr)
	{
		super(empName, yr);
	}

	public double getSalary()
	{
		salary = 50000*(Math.pow(1.03, (super.getYear()-1957)));
		return salary;
	}
}

is it necessary to have a abstract class or thats just an alternative?
now that I have got the program to output the correct salary calculations now I'm instructed to:

*create an array of object to hold the faculty and staff members(max number of array is 5)
*output the salary and name of the person who has the HIGHEST salary
*output the salary and name of the person who has the LOWEST salary

I'm a little stumped here, any suggestions, hints?

Which part of the assignment is the problem?
Define an array of faculty members
Search that array for the HIGHEST and LOWEST

There are two parts to setting up an array of objects,
first define the array itself
then fill the array with objects

For the search, start by writing a small simple program with an int array holding some numbers.
Write a loop to go thru the array and find the index of the highest and lowest number in the array.
When this works you can use the logic in your bigger program.

Sorry, I dont think i'm understanding the array of objects :(

I had actually created methods getMinSalary and getMaxSalary in the superclass and call in the driver, but to do that I was having to create two arrays in the superclass: salaries[] and employees[]. It got really messy, that I dropped that idea and then got confused about how the array of objects is fitting in. Am I going to have methods in the driver class? like addEmployee to add the employee objects to the array?

*Sorry for all these confusing questions, new to java last week of the semester - just struggling to get things working, appreciate all your guys help! :)*

I suggested that you start with something simple, get that to work and then merge those ideas into the larger program. Are you going to try that or do you want to do it another way?

I dont think i'm understanding the array of objects

Do you know how to create an array of String? String is an object.

String[] employees = {"James", "Adam", "Judy", "Tom", "Frank"};

that would be a String array, right?
but what good does that do for this program?
Would you mind giving me a short, simple example code of creating an array of objects that might be relevant to my situation here?

The syntax for creating an array:

<CLASSNAME>[] VARIABLENAME = new <CLASSNAME>[<NBR OF ELEMENTS>];

YourObject[] yourObject = new YourObject[5]; // create an array of 5 YourObject

DriverEmployee[] employees = new DriverEmployee[5];

that's the array, right?

now I have to figure out a way to fill that with the objects created in that class.
and once i have that settled, then I can use compare the salaries to find the min and max ones?

Is the DriverEmployee class the base class for all the employees? Remember polymorphism?
You want to be able to put all the classes that derive from the base class in the array.

Before you try to write a search with the classes, I recommend:
For the search, start by writing a small simple program with an int array holding some numbers.
Write a loop to go thru the array and find the index of the highest and lowest number in the array.
When this works you can use the logic in your bigger program.

Sorry, but I don't get what you mean by this array thing. Is this all suppose to happen in the driver class or super class?

Polymorphism is just overloading, isn't it? Where is that suppose to happen? In the super class?

I'm a visual learner, could it be possible to show some random code that may help me understand this? It's been about 3 days now working on this assignment, and its due in less than 3hrs now. I really want to understand and get it to work. Appreciate all your help, trust me I'm really trying to understand this my final exam is in less than 4days so I have no choice but to understand this. Any help is helpful.

For the array, what classes are you supposed to put in it? The faculty and staff objects? So the type of the array must be one that is common to the objects that you put in it.

Correct, the array should be of the objects created in the driver class.

This is the array that would store the name of each staff/employee along with his/her salary amount.

ARRAY
[0] = faculty1
[1] = faculty2
[2] = faculty3
[3] = staff1
[4] = staff2

Common would be the super class 'Employee'?

Yes that looks reasonable.
Next you need to work on the search. I still recommend a small simple loop as suggested earlier to get the technique. You'll be saving indexes to the array vs the array's contents.

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.