COMP 274 Week 3 Inheritance and Polymorphism The objective of this programming a
ID: 3534102 • Letter: C
Question
COMP 274 Week 3 Inheritance and Polymorphism 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! 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. 2. The StateTaxableWorker class inherits from the TaxableWorker class. This class adds attributes for the state name and the state tax rate (ie. 0.05). An explicit value constructor must be provided to set the three values of the base class plus the values for the state name and state tax rate. A mutator method must be provided to change the value of the state tax rate. The base class workerInfo method must be overridden to include all the base class worker information plus the state name and state tax rate information. The base class taxWithheld method must be overridden to include the base class tax withheld plus the state tax withheld (gross pay * state tax rate).Explanation / Answer
This is exact what u wanted. ------------------------------------------------------------------- TaxableWorker Class -------------------------------------------- public class TaxableWorker { String name; double rate; double tax; public TaxableWorker(String name, double rate, double tax) { super(); this.name = name; this.rate = rate; this.tax = tax; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } public double getTax() { return tax; } public void setTax(double tax) { this.tax = tax; } public String workerInfo () { String s=new String("name:"+this.name+",rate :"+this.rate+", tax :"+this.rate); return s; } public double grossPay(int hours) { return hours*this.rate; } public double taxWithheld(double grossPay) { return grossPay*this.tax/100; } public static void main(String[] args) { TaxableWorker t=new TaxableWorker("amit",100,5); String info=t.workerInfo(); System.out.println(info); double gross=t.grossPay(10); System.out.println(gross); double tax=t.taxWithheld(gross); System.out.println(tax); } } -------------------------------------------------- State TaxableWorker Class ----------------------------------------------- public class StateTaxableWorker extends TaxableWorker{ String stateName; double stateTax; public StateTaxableWorker(String name, double rate, double tax,String stateName,double statetax) { super(name, rate, tax); // TODO Auto-generated constructor stub this.stateName=stateName; this.stateTax=statetax; } public double getStateTax() { return stateTax; } public void setStateTax(double stateTax) { this.stateTax = stateTax; } public String workerInfo () { String s=new String(super.workerInfo()+"State name:"+this.stateName+",State tax :"+this.stateTax); return s; } public double taxWithheld(double grossPay) { return grossPay*this.stateTax/100 +super.taxWithheld(grossPay); } public static void main(String[] args) { StateTaxableWorker t=new StateTaxableWorker("amit",100,5,"usa",3); String info=t.workerInfo(); System.out.println(info); double gross=t.grossPay(10); System.out.println(gross); double tax=t.taxWithheld(gross); System.out.println(tax); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.