Applications folder
workspace folder.
File->New to create a new Java Project. Call your
new project SocialSecurityTaxCalculator, then click
Finish.
SocialSecurityTaxCalculator) in the
Package Explorer tab, and select New->Class.
Name: textbox, enter the name of your class as
TaxCalculator, and then click Finish. Your
new class should now appear in the Eclipse editor.
TaxCalculator class:Problems
tab (at the bottom).
Implement the following methods in the TaxCalculator class,
declaring fields as needed. Use the exact method names specified, with
the same return types and parameters. Include a brief javadoc comment
explaining the purpose of each method. You do not need to use @param
and @return tags in your javadoc comments.
When complete, this class will calculate the social security tax paid by a particular tax payer, depending on that person's salary and whether or not they are self employed.
int), and a boolean indicating
whether or not the tax payer is self employed (true means
self employed).
getSalary that returns the
tax payer's salary as an int
isSelfEmployed that returns
boolean - true if the tax payer is self employed,
and false otherwise
getTaxBill method that returns double - the
total social security tax owed by this tax payer. The tax is calculated
as follows:
main method:main method specifies what happens when a
Java program is actually run.
main method to your TaxCalculator
class. Recall that the signature of a main method is:
public static void main(String [] args) {
}
main method that creates a
TaxCalculator object and then uses
System.out.println to print the result of calling each of the
other methods. For example:
public static void main(String [] args) {
TaxCalculator calc = new TaxCalculator(90000, true);
System.out.println(calc.getSalary());
System.out.println(calc.isSelfEmployed());
System.out.println(calc.getTaxBill());
}
TaxCalculator
class in the Project Explorer tab, and then selecting
Run As->Java Application. The results of running your
main method should appear in the Console tab at the bottom
of the screen. For example, if you used the main method above, the
Console should display:
90000 true 12720.0
TaxCalculator.java
(in the Project Explorer tab), then select New->JUnit Test Case.
(If JUnit Test Case does not appear in the pop-up menu,
select Other, and then JUnit Test Case).
Make sure that the radio button for New JUnit 4 test is
selected, and then click the link that says "Click here to add JUnit 4 to
the build bath" at the bottom of the window. Click OK
to close the
Build Path window, and then Finish to close the JUnit Test
Case window.
The file TaxCalculatorTest.java will appear in the Eclipse
editor. Note that we are using JUnit 4, which has a slightly different
syntax from the JUnit 3 tests you used in COMP 131.
TaxCalculatorTest.java,
add the line:
import org.junit.*;(if it is not already there)
TaxCalculator
class. In JUnit 4, test methods are indicated with the @Test
annotation. For example, your test method might look like:
@Test
public void testConstructor() {
// test code goes here
}
TaxCalculator class, call the accessor methods, and check that
the values returned are as expected using the methods of class
org.junit.Assert as explained in the next bullet.
org.junit.Assert:
Assert.assertEquals(int x, int y) - passes if
x and y are the same number, and fails otherwise
Assert.assertTrue(boolean b) - passes if b
is true, and fails otherwise
Assert.assertFalse(boolean b) - passes if b
is false, and fails otherwise
Assert.assertEquals(double x, double y, double delta)
- passes if x and y are different by no more than
delta, and fails otherwise
For example, if the object calc is created as:
TaxCalculator calc = new TaxCalculator(90000, true);then all of the following should pass:
Assert.assertEquals(calc.getSalary(), 9000); Assert.assertTrue(calc.isSelfEmployed()); Assert.assertEquals(calc.getTaxBill(), 12720.0, 0.01);and all of the following should fail:
Assert.assertEquals(calc.getSalary(), 9001); Assert.assertFalse(calc.isSelfEmployed()); Assert.assertEquals(calc.getTaxBill(), 12719.0, 0.01);
TaxCalculatorTest file, and then run your test
by right-clicking on TaxCalculatorTest.java in the
Project Explorer tab and selecting Run As->JUnit Test.
A JUnit tab should appear in the same pane as the Package Explorer,
with a green bar if your test passed, and a red bar and error message
if your test failed.
getTaxBill method. Review the
testing rules if you do not remember
how to do this.
TaxCalculator.java and TaxCalculatorTest.java
files, which are located in your
workspace/SocialSecurityTaxCalculator folder.