Skip to content

Compute a tax

Now we’re ready to actually compute a tax amount. Here’s a minimal example which calculates federal tax.

  1. Enter Code

Enter the following code in a file named ‘Compute.cs’:

using System;
using TaxControls;
class Compute{
public static void Main(){
try {
CTaxControl tc = new CTaxControl();
tc.DataFilename = "demo.tax";
tc.SelectedTax = "federal income tax";
tc.Earnings = 5000.00;
Console.WriteLine(tc.TaxAmount());
} catch (TaxControlException e) {
Console.WriteLine(e.Message);
}
}
}

Enter the following code in a file named ‘Compute.java’:

import com.boondocks.taxcontrols.TaxControl;
import com.boondocks.taxcontrols.TaxControlException;
public class Compute{
public static void main(String[] args){
try{
TaxControl tc = new TaxControl();
tc.setDataFilename("demo.tax");
tc.setSelectedTax("federal income tax");
tc.setEarnings(5000.0);
System.out.println(tc.getTaxAmount());
} catch (TaxControlException e){
e.printStackTrace();
}
}
}

This minimal code

  • chooses the tax table data file,
  • selects the tax to compute,
  • sets the earnings per pay period to $5000, and
  • computes the tax.

Default values are taken, which for most properties is zero, except PayPeriodsPerYear which defaults to 12 (monthly).

  1. Compile

Compile the code at the command prompt by executing the following:

Terminal window
\...\csc.exe /reference:TaxControls.dll Compute.cs
Terminal window
javac -classpath .;bigloo_u.zip;TaxControls.jar Compute.java
  1. Run

Run the code by typing:

Terminal window
Compute
Terminal window
java -classpath .;bigloo_u.zip;TaxControls.jar Compute

The program’s output should show the computed federal tax.