implement a class Employee. an employee has a name (a string) and a salary (a double). provide a constructor with two parameters.

public Employee (String employeeName, double currentSalary)

methods:
public String getName()
public double getSalary()
public void raiseSalary(double byPercent)

this is what i have so far:

public class Employee

{
	private String name;
	private double salary;

	public Employee (String employeeName, double currentSalary)
	{
		name = employeeName;
		salary = currentSalary;
	}
	public String getName()
	{
		return name;
	}
	public double getSalary()
	{
		return salary;
	}
	public void raiseSalary(double byPercent)
	{
	}
}

here is my tester:

public class EmployeeTester
{
	public static void main(String[]args)
	{
		Employee buck = new Employee("buckaroo", 50);
		buck.getName();
		System.out.println(buck);
	}
}

this prints: Employee@3ae48e1b..... it doesnt print buckaroo. how come?

You never tell the console to print it.

System.out.println(buck);

try instead,

System.out.println(buck.getName());
public class EmployeeTester
{
	public static void main(String[]args)
	{
		Employee buck = new Employee("buckaroo", 50);
		buck.getName();

		System.out.println(buck);
	}
}

Line 6 does nothing. You didn't harness the return value of getName() so it's lost. if you want to display the buck object, you would do this:

System.out.println(buck.toString());

If that doesn't work or you want to look different, you'll have to write your own toString() function.

to display "buckaroo", you'd do this:

System.out.println(buck.getName());

if you want to display the buck object, you would do this:

System.out.println(buck.toString());

Actually, it is unnecessary to call toString in a print statement. The print statements (print and println, not sure about printf, though, I should probably look into that) as those methods automatically use String.valueOf(argument), which, for Objects, turns arund and calls toString() on that Object, itself. ;-)

Edit: He is getting the output he is getting because he hasn't defined his own toString() method, and so Object's toString() method is being used, which produces the output you see. (P.S. this line was more for the OPs benefit, as I am fairly sure you know this already. ;-) )

Edited 5 Years Ago by masijade: n/a

>> P.S. this line was more for the OPs benefit, as I am fairly sure you know this already.

Well, I sort of knew this already. I knew that somewhere along the line, toString() often seemed to get called automatically, but I didn't know it ALWAYS did and I didn't know the underlying mechanics of println, so I stuck the toString() in there.

I know more now. :)

public class Employee

{

	private String name;

	private double salary;



	public Employee(String employeeName, double currentSalary)

	{

		name = employeeName;

		salary = currentSalary;

	}

	public String getName()

	{

		return name;

	}

	public double getSalary()

	{

		return salary;

	}

	public void raiseSalary(double byPercent)

	{

		salary = byPercent * .10;

	}

}

I want to be able to raise the employee's salary by a certain percent.. but im stuck again!!!!!!! ugh

Well 10% of anything is only 10%. If you want to raise something by 10% then you want the end product to 110% of the original. Now, using that hint, what do you want to modify and what should you multiply that by.

Edited 5 Years Ago by masijade: typo

well i have a mutator method for salary, so in my tester program i can set the initial salary to what ever i want.

say for instance i set the salary to 50.

Employee masijade = new Employee("masijade", 50);

now i need a mutator method that raises the salary by a certain percent.

for instance, 10%.

masijade.raiseSalary(10);

this doesnt work :(

So what is it you want to raise by 10%?

That is what you should be multiplying against (and assigning back to of course)
i.e. x = x* y or x *= y

Now, as to the number you should be multiplying with. As I said before, if you multiply with 10% (as 0.1 is) you will end up with only 10% of the salary. That's not what you want. You want to raise it by 10%, for that you still need the original 100%, so you need to multiply by 110%. Now, if 10% is 0.1 what is 110%?

Now, attempt to change the method and post that back here.

okay mate, I think I got it!

Class:

public class Employee

{

	private String name;

	private double salary;



	public Employee(String employeeName, double currentSalary)

	{

		name = employeeName;

		salary = currentSalary;

	}

	public String getName()

	{

		return name;

	}

	public double getSalary()

	{

		return salary;

	}

	public void raiseSalary(double byPercent)

	{

		salary = (salary/100) * byPercent + salary;

	}

}

Main:

public class EmployeeTester

{

	public static void main(String[]args)

	{

		Employee buck = new Employee("buckaroo", 50);

		System.out.println(" ");

		System.out.println(buck.getName());

		System.out.println(" ");

		System.out.println(buck.getSalary());

		System.out.println(" ");

		buck.raiseSalary(15);

		System.out.println(buck.getSalary());

	}

}

This is taking an employee named buck with a salary of 50. printing the name and original salary of 50, then raising the salary by 15% and printing the updated salary of 57.5

this prints:

buckaroo

50

57.5

Edited 5 Years Ago by z00t: typo

I'm sorry but :sigh: why not just salary *= (1.0 + byPercent) if "byPercent" is already in 0.1 format salary *= (1.0 + (byPercent/100.0)) if not.

Edited 5 Years Ago by masijade: n/a

The way i wrote it does the same thing doesn't it?!

This article has been dead for over six months. Start a new discussion instead.