Reputation: 13
Here is the problem:
Write the class Marketer to accompany the other law firm classes described in this chapter. Marketers make $50,000 ($10,000 more than general employees) and have an additional method called advertise that prints "Act now, while supplies last!" Make sure to interact with the Employee superclass as appropriate.
The code so far:
public class Marketer extends Employee {
public Marketer() {
setBaseSalary(super.getSalary() + 10000.0);
}
public void advertise() {
System.out.println("Act now, while supplies last!") ;
}
}
This is what the output should be:
50000.0
75535.0
Act now, while supplies last!
There is a file extension called Employee.java on the following site that the code above follows: http://practiceit.cs.washington.edu/problem.jsp?id=1324
So what I have so far, it printed out every expected outcome EXCEPT for 75535.0! Where did I go wrong?? When I ran the code, it said this (for the error I had received on the 75535.0 part):
(change base salary to $65535.00)
because my output was 65535.0, which is 10000 less than what the answer is supposed to be.
I can't seem to find the error since I just started to do these inheritance kind of things in Java, so I am still unfamiliar with it. Thank you if you can help me understand where I messed up.
Upvotes: 1
Views: 1966
Reputation: 1847
Solution to your problem is simple. Just define Marketer class as below
public class Marketer extends Employee {
public void advertise() {
System.out.println("Act now, while supplies last!") ;
}
@Override
public double getSalary() {
return super.getSalary() + 10000.0; // $40,000.00
}
}
Here we override getSalary method of Employee class and whenever we have to return salary we add 10000 to the employee salary.
Hope this helps
Upvotes: 0
Reputation: 2168
Here is my solution example:
public class Marketer extends Employee {
public void advertise() {
System.out.println("Act now, while supplies last!");
}
public double getSalary() {
return super.getSalary() + 10000;
}
}
It should help you understand problem better.
Upvotes: 0
Reputation: 688
Here's what's happening:
Create Marketer, which calls Employee's constructor and instance initialization. That sets baseSalary
to 40,000. Your code in the Markerter constructor then runs and sets baseSalary
to 50,000. Their test driver calls setBaseSalary
with a value of 65535.0, overwriting your initial set.
Your code isn't properly interacting with the Employee class. The comment in Employee.java
says your code isn't supposed to use, call, or modify the values below. One of those is the baseSalary
variable, and you are modifying it in the Marketer
constructor. Once the test driver changes the value, your class has no way to compute the changed salary for the Marketer
.
You shouldn't call setBaseSalary
in your constructor. Instead, override getSalary
in Marketer, and use the getSalary
method from Employee in your own class.
Upvotes: 2