Friday, November 23, 2007

Happy Thanksgiving!

Not that I celebrated it before, being a Filipino and all. But I guess it was a nice change, having a little bit of downtime as I had no access to my servers, no emails to be sent, and a few people in the office.

That included SoftDev's capable Head, Mahrnee, whose help I appreciated greatly. She taught me how to debug line by line, and she was as worried as I was when the compiler showed a NullPointerException again and again.

But in the end, serendipity saved the day as I realized that I was calling null values in my no-arg constructor.

7.14*** (The Tax class) Design a class named Tax to contain the following instance data fields:
int filingStatus: One of the four tax filing statuses: 0 — single filer, 1 — married filing jointly, 2 — married filing separately, and 3 — head of household. Use the public static constants SINGLE_FILER (0), MARRIED_JOINTLY (1), MARRIED_SEPARATELY (2), HEAD_OF_HOUSEHOLD (3) to represent the status.

int[][] brackets: Stores the tax brackets for each filing status (see Listing 6.11, ComputeTax.java).

double[] rates: Stores tax rates for each bracket (see Listing 6.11).

double taxableIncome: Stores the taxable income.

Provide the get and set methods for each data field and the getTax() method that returns the tax. Also provide a no-arg constructor and the constructor Tax(filingStatus, brackets, rates, taxableIncome).

Draw the UML diagram for the class. Implement the class. Write a test program that uses the Tax class to print the 2001 and 2002 tax tables for taxable income from $50,000 to $60,000 with intervals of $1,000 for all four statuses.
Tax.java
public class Tax {
private int filingStatus = 0;
private int[][] brackets = {
{6000, 27950, 67700, 141250, 307050},
{12000, 46700, 112850, 171950, 307050},
{6000, 23350, 56425, 85975, 153525},
{10000, 37450, 96700, 156600, 307050}};
private double[] rates = {0.10, 0.15, 0.27, 0.30, 0.35, 0.386};
private double taxableIncome = 27050;

public Tax() {
this.filingStatus = filingStatus;
this.brackets = brackets;
this.rates = rates;
this.taxableIncome = taxableIncome;
}

public Tax(int newFilingStatus, int[][] newBrackets, double[] newRates, double newTaxableIncome) {
filingStatus = newFilingStatus;
brackets = newBrackets;
rates = newRates;
taxableIncome = newTaxableIncome;
}

public int getFilingStatus() {
return filingStatus;
}

public int[][] getBrackets() {
return brackets;
}

public double[] getRates() {
return rates;
}

public double getTaxableIncome() {
return taxableIncome;
}

public void setFilingStatus(int newFilingStatus) {
filingStatus = newFilingStatus;
}

public void setBrackets(int[][] newBrackets) {
brackets = newBrackets;
}

public void setRates(double[] newRates) {
rates = newRates;
}

public void setTaxableIncome(double newTaxableIncome) {
taxableIncome = newTaxableIncome;
}

public double getTax(int newFilingStatus, double newTaxableIncome) {

double tax = 0; // Tax to be computed
// Compute tax in the first bracket

if (newTaxableIncome <= this.brackets[newFilingStatus][0])
return tax = newTaxableIncome * this.rates[0]; //Done
else
tax = this.brackets[newFilingStatus][0] * this.rates[0];

// Compute tax in the 2nd, 3rd, 4th, and 5th brackets, if needed
for (int i = 1; i < this.brackets[0].length; i++) {
if (newTaxableIncome > this.brackets[newFilingStatus][i])
tax += (this.brackets[newFilingStatus][i] - this.brackets[newFilingStatus][i - 1]) *
this.rates[i];
else {
tax += (newTaxableIncome - this.brackets[newFilingStatus][i - 1]) * this.rates[i];
}
}
return tax; // Done
}
}
TestTax.java
public class TestTax {
public static void main(String[] args) {
Tax test1 = new Tax();
Tax test2 = new Tax();
test1.setFilingStatus(2);
test1.setTaxableIncome(500000);
test2.setFilingStatus(3);
test2.setTaxableIncome(450000);
int var1 = test2.getFilingStatus();
double var2 = test2.getTaxableIncome();

System.out.println(test1.getFilingStatus() + ", " + test1.getTaxableIncome() );
System.out.println(test2.getFilingStatus() + ", " + test2.getTaxableIncome());
System.out.println(var1 + ", " + var2);
System.out.println(test2.getTax(var1, var2));
}
}

No comments: