I need help fixing my code. I\'m having trouble understanding override methods s
ID: 3686233 • Letter: I
Question
I need help fixing my code. I'm having trouble understanding override methods so an explanation on what I'm doing wrong would be much appreciated. I believe most of my error is in the "Salesman" code
Employee.java
public class Employee {
private int monthlySalary;
private String name;
/**
* Declare name and monthlySalary data
* fields to be used in methods and constructors
*/
public Employee(String name){
this.name = name;
}
public Employee(int monthlySalary){
this.monthlySalary = monthlySalary;
}
private int getAnnualSalary() {
int annualSalary = monthlySalary * 12;
return annualSalary;
}
/* toString() method to return name, monthlySalary, and annualSalary
*
*/
public String toString(){
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary:" + getAnnualSalary();
}
}
Salesman.java
public class Salesman extends Employee
{
//Declare new data fields
private double commission;
private int annualSales;
//Constructor of Salesman which includes name and fields monthly salary,an annual sales to be initialized
public Salesman (int annualSales){
this.annualSales = annualSales;
}
public Salesman(double commission){
this.commission = commission *(annualSales*.03);
if (commission > 25000){
commission = 25000;
}
}
/**
* Overridden method of class Employee returns the annual salary for a salesman
* consisting of the getAnnualSalary() method, casted to an integer, of class
* Employee plus the the commission.
* Commission earned can not be greater than $25,0000
*/
//@Override
private int getAnnualSalary() {
int annualSalary = annualSalary + commission;
return annualSalary;
}
/**
* The toString() method overrides the superclass Employee toString() method.
* Returns a string representation containing the name, monthlySalary, annualSalary
* and the annual sales of the salesman employee.
*/
//@Override
public String toString() {
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary (including commission):" + getAnnualSalary() + " Annual Sales: " + annualSales;
}
}
Explanation / Answer
Hi, have seen couple of issues in you code. Provided the working code. Not sure what your requirement is but made few achanges so that it could get compiiled.
Coming to the explanations part:
a)You cant or shouldn't access variables directly be it from super class or sub class. You need to use accessors for accessing them.So, I have provided get methods for variables in super class
b)When you override default constructor, compiler won't call the default one. When you instantiate a sub class object, it first and foremost checks whether it can call the sub class constructor or did you make a call. In your case, you have overriden it and so coompiler checks for explicit super class constructor call. As it is not availble, it throws an error.
Modfied the code a bit for that.
Do let me know if you have any questions
class Employee {
private int monthlySalary;
private String name;
public Employee(String name){
this.name = name;
}
public Employee(int monthlySalary){
this.monthlySalary = monthlySalary;
}
private int getAnnualSalary() {
int annualSalary = monthlySalary * 12;
return annualSalary;
}
public String getName()
{
return this.name;
}
public int getMonthlySalary()
{
return this.monthlySalary;
}
public String toString(){
return " Name: " + name + " Monthly Salary: " + monthlySalary + " Annual Salary:" + getAnnualSalary();
}
}
class Salesman extends Employee
{
private double commission;
private int annualSales;
double annualSalary;
/*
not sur what you are trying to achieve here
When you call a sub-class constructor, you should always make sure super class constructor is called.
You ahve to explictly call the super call constructor if default constructor is overiiden
public Salesman (int annualSales){
super("DSFdf");
this.annualSales = annualSales;
}
public Salesman(double commission){
super("dfdF");
this.commission = commission *(annualSales*.03);
if (commission > 25000){
commission = 25000;
}
}
*/
public Salesman(String name,int annualSales,double commision)
{
super(name);
this.annualSales = annualSales;
this.commission = commission *(annualSales*.03);
if (this.commission > 25000){
this.commission = 25000;
}
}
private double getAnnualSalary() {
annualSalary = annualSalary + commission;
return annualSalary;
}
public String toString() {
return " Name: " + this.getName() + " Monthly Salary: " + this.getMonthlySalary() + " Annual Salary (including commission):" + getAnnualSalary() + " Annual Sales: " + annualSales;
//return "Sfsdf";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.