Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The objective of this programming assignment is to experience the use of inherit

ID: 3542068 • Letter: T

Question

The objective of this programming assignment is to experience the use of inheritance in Java and to see how polymorphism works with inheritance in Java.

The assignment involves writing three classes, plus a test class. The base class is a TaxableWorker class which contains a couple of attributes and methods common to all workers. The first derived class is a StateTaxableWorker which adds state tax information to a TaxableWorker. The second derived class is a LocalTaxableWorker which adds local tax information to a StateTaxableWorker. The test program will be structured to include a method which accepts a base class reference and demonstrates polymorphic behavior.

NOTE: None of the first three classes below do any user input or console output! User input and console output are only done in the test program code!

NOTE: ALL the member variable of these classes MUST be PRIVATE!

The details of the three classes to be implemented are as follows:

1.      A TaxableWorker contains a name, an hourly pay rate (ie. 12.50/hr), and a federal tax rate (ie. 0.25). An explicit value constructor must be provided to set all three values. There must be mutator methods to change the values of the pay rate and the tax rate. There must be ONE workerInfo method that returns a string containing the name, hourly pay rate, and federal tax rate. There must be a grossPay method that takes the number of hours worked as a parameter and calculates the gross pay (hours * payRate) and returns that as a double. There must be a taxWithheld method that takes a gross pay amount as a parameter and calculates the tax withheld (grossPay * taxRate) and returns that as a double.

Private Member Variables:

             name, payRate, taxRate

Public Methods:

            one constructor

Explanation / Answer

//This is the base class


class TaxableWorker

{

private String name;

private double payRate;

private double taxRate;

  

public TaxableWorker(String n, double pRate, double tRate)

{

name = n;

payRate = pRate;

taxRate = tRate;

}

  

public void setPayRate(double rate)

{

payRate = rate;

}

  

public void setTaxRate(double rate)

{

taxRate = rate;

}

  

public String workerInfo()

{

return (name +" "+ payRate +" "+ taxRate);

}

  

public double grossPay(double hours)

{

return ((double)(hours * payRate));

}

  

public double taxWithheld(double grosspay)

{

return ((double)(grosspay * taxRate));

}

}





//now comes the second class


class StateTaxableWorker extends TaxableWorker

{

String stateName;

double stateTaxRate;

  

public StateTaxableWorker(String n, double pRate, double tRate, String sName, double stRate)

{

super(n,pRate,tRate);

stateName = sName;

stateTaxRate = stRate;

}

  

public void setStateTaxRate(double stRate)

{

stateTaxRate = stRate;

}

  

public String workerInfo()

{

return (super.workerInfo()+" "+stateName+" "+stateTaxRate);

}

  

public double taxWithheld(double grossPay)

{

return (super.taxWithheld(grossPay) + stateTaxRate * grossPay);

}

  

  

}



//now comes the third class


class CityTaxableWorker extends StateTaxableWorker

{

String cityName;

double cityTaxRate;

  

public CityTaxableWorker(String n, double pRate, double tRate, String sName, double stRate,String cName, double ctRate)

{

super(n, pRate, tRate, sName, stRate);

cityName = cName;

cityTaxRate = ctRate;

}

  

public void setCityTaxRate(double ctRate)

{

cityTaxRate = ctRate;

}

  

public String workerInfo()

{

return (super.workerInfo()+" "+cityName+" "+cityTaxRate);

}

  

public double taxWithheld(double grossPay)

{

return (super.taxWithheld(grossPay) + cityTaxRate * grossPay);

}

  

  

}



//then comes the test class


class test

{

public static void main(String args[])

{

StateTaxableWorker stateworker = new StateTaxableWorker("John",3.2,0.5,"state_x",0.1);

CityTaxableWorker cityworker = new CityTaxableWorker("Andrew",2.5,0.7,"state_y",0.2,"city_x",0.1);

display(stateworker,20);

display(cityworker, 30);

}

  

public static void display(TaxableWorker w, double hours)

{

String info = w.workerInfo();

System.out.println("The information of the worker is "+info);

System.out.println("The worker gets paid "+w.grossPay(hours));

System.out.println("The worker has a withheld tax of "+w.taxWithheld(w.grossPay(hours)));

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote