In: Computer Science
CompSci 251: Assignment 3
Due 2/20, 2017 10:00am
Topics Covered: Instance variables and methods; using a driver class
1 Introduction
This assignment will have you implement your first real Java class using instance variables(state) and meth- ods(behaviour). It will also probably be the first time that you write a program with a driver class.
2 Income tax computation
Most Americans (not just Americans, really!) complain about filing their income taxes. Some people dont think they should pay at all. Others dont like some of things that taxes are used for. Many people find the forms and the concepts confusing (they are). But the basic computation for income tax is not really that complicated. Its really the many special rules that make it hard to understand.
For this assignment, we’ll write a class that does the basic income tax computation for a “typical” household. The steps (for our simplified situation) are:
Compute Adjusted Gross Income (AGI) where AGI = wages + interestIncome.
Compute total deductions, where deductions = stateTax + propertyTax.
Compute taxable income, where taxableIncome = AGI - deductions.
Compute tax using this table. You have to know whether the household is single or married to choose the correct column.
Tax rate |
Single |
Married filing jointl |
10% |
$0 - $9,275 |
$0 to $18,550 |
15% |
$9,276 - $37,650 |
$18,551-$75,300 |
25% |
$37,651 - $91,150 |
$75,301-$151,900 |
28% |
$91,151 - $190,150 |
$151,901-$231,450 |
33% |
$190,151 - $413,350 |
$231,451-$413,350 |
35% |
$413,351 - $415,050 |
$413,351-$466,950 |
39.6% |
≥ $415, 051 |
≥ $466, 951 |
Your class will also compute the overall tax rate, which is just total tax divided by AGI. For most people, this number is much lower than the rate on their bracket.
1
2.1 The IncomeTax and IncomeTaxDriver class
Here is the UML class diagram for the IncomeTax class.
IncomeTax |
- name: String // name of person whose tax is computed - married
: boolean // true if married, false if single - stateTax: integer |
+ getName() : String + getAGI() : integer |
The instance variables were explained above. Each one has a mutator/setter and an accessor/getter. The mutators for the integer variables must check the input value to ensure that it is non-negative. If the value is negative, they should leave the variable unchanged. You may not add additional instance variables. Any values that you need can be computed from the instance variables. The remaining methods compute values used in the tax computation. getIncomeTax() and getOverallRate() are the only ones that return double values and they need to because of the nature of the computation. The income tax computation looks complicated, but it really is not.
First, you figure out which top tax bracket the person falls into. For that tax bracket, the person will pay 10/15/25/28/33/35/39.6 percent of their income above the minimum value for the bracket, plus the total tax they owe from all the lower brackets. The second part of this formula is actually a fixed value for each bracket (once you know whether the person is married or single).
You can do this with a big if-else statement or there is a slick way to put the relevant values in arrays and use a loop to do the computation. The IncomeTaxDriver class is only required to have a main() method, though you can make other methods if you wish. It creates two IncomeTax objects, prompting the user to enter the five values needed to initialize the objects and setting them using the mutators/setters. It prints a summary of the income tax computation for each object and states which person is paying the most tax(Total Income Tax).
2.2 Requirements
• All instance variables must be private
2
In the IncomeTax class, no methods do any input or output.
You must use the class and method names that we specify above.
Your classes must follow the description in the previous section.
You should have two other helper methods in the driver class. One to read in the values for the IncomeTax object (private static IncomeTax initIncomeTax(Scanner stdIn)) and another to print the data about an IncomeTax object (private static void printIncomeTax(IncomeTax it)).
Only the total incomeTax and the overall tax rate values need to be printed with fractional parts. Its a good idea to use printf() to control the number of digits to the right of the decimal place.
2.3 Example how to use the Table
Joe is single and has a Taxable Income of $57,250. However, his income tax is not 25% of $57250, which would be $14312.50.
Instead, there are three steps:
He pays 10% tax on the ”first $9,275” of his income: ( .10 * 9275 )
= $927.50
He pays 15% tax on the additional income, up to $37,650: ( .15 *
(37650 -9275) ) = $4256.25 Finally, he also pays 25% tax on his
income over $37,650: ( .25 * (57650 - 37650) ) = $4900.00 His total
tax is the sum of these three numbers:$927.50 + $4256.25 + $4900.00
= $10083.75
2.4 Sample Output
Welcome to the Income Tax Program! Please enter values for person #1 income tax profile:
Enter person’s name: Alex Is Alex married (true or false):true Enter wages of Alex: 22000 Enter interest income of Alex: 0 Enter state taxes paid by Alex: 1000 Enter property taxes paid by Alex: 300
Please enter values for person #2 income tax profile:
Enter person’s name: Victor Is Victor married (true or false):false Enter wages of Victor: 50000 Enter interest income of Victor: 0 Enter state taxes paid by Victor: 5000 Enter property taxes paid by Victor: 1000
Income Tax Profile for Alex
Marital Status: true Wages: $22000 Interest Income: $0 Adjusted Gross Income: $22000 State Tax Paid: $1000 Property Tax Paid: $300
Total Deductions: $1300 Taxable Income: $20700 Total Income Tax: $2177.50
3
Overall Tax Rate: 9.90%
Income Tax Profile for Victor
Marital Status: false Wages: $50000 Interest Income: $0 Adjusted Gross Income: $50000 State Tax Paid: $5000 Property Tax Paid: $1000 Total Deductions: $6000 Taxable Income: $44000
Total Income Tax: $6771.25 Overall Tax Rate: 13.54%
Victor will pay more income tax than Alex Goodbye!
public class IncomeTax {
private String name;
private boolean isMarried;
private int wages;
private int interest;
private int stateTax;
private int propertyTax;
public IncomeTax(String name, boolean isMarried,
int wages,
int stateTax,
int propertyTax) {
this.name = name;
this.isMarried = isMarried;
this.wages = wages;
this.stateTax = stateTax;
this.propertyTax =
propertyTax;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMarried() {
return isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public int getWages() {
return wages;
}
public void setWages(int wages) {
this.wages = wages;
}
public int getInterest() {
return interest;
}
public void setInterest(int interest) {
this.interest = interest;
}
public int getStateTax() {
return stateTax;
}
public void setStateTax(int stateTax) {
this.stateTax = stateTax;
}
public int getPropertyTax() {
return propertyTax;
}
public void setPropertyTax(int propertyTax) {
this.propertyTax =
propertyTax;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result +
interest;
result = prime * result +
(isMarried ? 1231 : 1237);
result = prime * result + ((name ==
null) ? 0 : name.hashCode());
result = prime * result +
propertyTax;
result = prime * result +
stateTax;
result = prime * result +
wages;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
IncomeTax other = (IncomeTax)
obj;
if (interest !=
other.interest)
return
false;
if (isMarried !=
other.isMarried)
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equals(other.name))
return
false;
if (propertyTax !=
other.propertyTax)
return
false;
if (stateTax !=
other.stateTax)
return
false;
if (wages != other.wages)
return
false;
return true;
}
public int getAGI() {
double interestIncome =
getWages() * (getOverallRate() / 100);
return (int) (wages +
interestIncome);
}
public int getDeductions() {
return getStateTax() +
getPropertyTax();
}
public int getTaxableIncome() {
return getAGI() -
getDeductions();
}
public double getIncomeTax() {
double overallrateInterestrate =
getOverallRate() / 100;
double getDeductionAmout = wages -
getDeductions();
double
overallrateInterestrateincome =
(overallrateInterestrate*getDeductionAmout);
double
totalincome=getDeductionAmout-overallrateInterestrateincome;
return totalincome;
}
public double getOverallRate() {
boolean status =
isMarried();
if (status == true) {
if (wages > 0
|| wages < 9275) {
setInterest(10);
return getInterest();
}
if (wages >
9276 || wages < 37650) {
setInterest(15);
return getInterest();
}
if (wages >
37650 || wages < 91150) {
setInterest(25);
return getInterest();
}
if (wages >
91151 || wages < 190150) {
setInterest(28);
return getInterest();
}
if (wages >
190151 || wages < 413350) {
setInterest(33);
return getInterest();
}
if (wages >
413351 || wages < 415050) {
setInterest(35);
return getInterest();
}
if (wages >=
415051) {
setInterest(10);
return getInterest();
}
} else {
if (wages > 0
|| wages < 18850) {
setInterest(10);
return getInterest();
}
if (wages >
18851 || wages < 75300) {
setInterest(15);
return getInterest();
}
if (wages >
75301 || wages < 151900) {
setInterest(25);
return getInterest();
}
if (wages >
151901 || wages < 231450) {
setInterest(28);
return getInterest();
}
if (wages >
231451 || wages < 413350) {
setInterest(33);
return getInterest();
}
if (wages >
413351 || wages < 466950) {
setInterest(35);
return getInterest();
}
if (wages >=
466950) {
setInterest(10);
return getInterest();
}
}
return 0;
}
public String toString() {
return "IncomeTax [name=" + name +
", isMarried=" + isMarried
+ ", wages=" + wages + ", interest=" + interest
+ ", stateTax="
+ stateTax + ", propertyTax=" + propertyTax +
"]";
}
}
import java.util.Scanner;
public class IncomeTaxDriverClass {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.println("enter the
name");
String name = scanner.next();
System.out.println("enter the
marital status");
boolean married =
scanner.nextBoolean();
System.out.println("enter the wages
");
int wages =
scanner.nextInt();
System.out.println("enter the state
tax paid");
int stateTax =
scanner.nextInt();
System.out.println("enter the
property tax paid");
int propertyTax =
scanner.nextInt();
IncomeTax tax = new
IncomeTax(name, married, wages, stateTax,
propertyTax);
System.out.println("enter the
name");
String name1 =
scanner.next();
System.out.println("enter the
marital status");
boolean married1 =
scanner.nextBoolean();
System.out.println("enter the wages
");
int wages1 =
scanner.nextInt();
System.out.println("enter the state
tax paid");
int stateTax1 =
scanner.nextInt();
System.out.println("enter the
property tax paid");
int propertyTax1 =
scanner.nextInt();
IncomeTax tax1 = new
IncomeTax(name1, married1, wages1,
stateTax1, propertyTax1);
System.out.println(tax.toString());
double agi=tax.getAGI();
System.out.println("the agi is
"+agi);
double
deductions=tax.getDeductions();
System.out.println("deducitons"+deductions);
double
taxincome=tax.getIncomeTax();
System.out.println("after tax
,income "+taxincome);
double
aftertax=tax.getWages()-taxincome;
System.out.println(" tax income
$"+aftertax);
System.out.println(tax1.toString());
double agi1=tax1.getAGI();
System.out.println("the agi is
"+agi1);
double
deductions1=tax1.getDeductions();
System.out.println("deducitons"+deductions1);
double
taxincome1=tax1.getIncomeTax();
System.out.println("after tax,
income "+taxincome1);
double
aftertax1=tax1.getWages()-taxincome1;
System.out.println(" tax income
$"+aftertax1);
}
}
output
enter the name
mark
enter the marital status
true
enter the wages
25000
enter the state tax paid
150
enter the property tax paid
3000
enter the name
smith
enter the marital status
false
enter the wages
36000
enter the state tax paid
250
enter the property tax paid
1500
IncomeTax [name=mark, isMarried=true, wages=25000, interest=0,
stateTax=150, propertyTax=3000]
the agi is 27500.0
deducitons3150.0
after tax ,income 19665.0
tax income $5335.0
IncomeTax [name=smith, isMarried=false, wages=36000, interest=0,
stateTax=250, propertyTax=1500]
the agi is 39600.0
deducitons1750.0
after tax, income 30825.0
tax income $5175.0