When I started Java instruction


Here’s a program I wrote years ago to calculate payroll taxes by binary search rather than Gausian elimination or something else sophisticated and mathematical. The binary search works, and is simple for me to read and understand to make sure it works. Inefficient at runtime, but quick to develop. A real trade off in the real world.  It even has a comment history!

I wrote it in C, using the C++ compiler, and then ported it to Java, some years ago.

Code for Calc.Java:

// #include <iostream.h>
// #include <stdlib.h>

import java.io.*;

/** calc.java
@Apr 13, 2009  wabbott SDI 0.8, UI 1.5%
@Mar 22, 2006  wabbott Port to Java
@Jun 22, 2001  wabbott Take FUTA out of deductions, UI % to 2.6, adjust SDI to 0.9%
@Feb 13, 1998  wabbott Created
*/

public class calc {

public static double asCash( int someValue ) {
return( (double) (someValue/100.00) );
}

public static void main (String argv[] ) {

double dFedW = 0.15;       // Federal withholding 15%
double dStateW = 0.06;     // State (CA) withholding 6%
double dSDIW = 0.009;      // State Disability withholding 0.9%
double dSSIW = 0.062;      // Social Security withholding 6.2%
double dMedW = 0.0145;     // Medicare withholding 1.45%

double dUIW = 0.015;       // Unemployment Insurance, paid by us, 1.5%
double dETTW = 0.001;      // Education tax, paid by us, 0.1%
double dFutaW = 0.008;     // FUTA, whatever that is, paid by us, .8%

int i_DAILY_EXCLUDE = 1020;
int i_WEEKLY_EXCLUDE = i_DAILY_EXCLUDE * 5;
int i_BI_WEEKLY_EXCLUDE= i_WEEKLY_EXCLUDE * 2;
int i_MONTHLY_EXCLUDE = i_DAILY_EXCLUDE * 22;
int i_BI_MONTHLY_EXCLUDE = i_MONTHLY_EXCLUDE * 2;

int iCalcFuta = 0;
int iCalcOurSSIContrib, uIC, eTTC;

int iCalcGross = 0;    // init
int iCalcTakeHomeToTry = 0;    // ditto
int iCalcSSI= 0;
int iCalcFedIncTax = 0;
int iCalcStateIncTax = 0;
int iCalcSDI = 0;
int iCalcMedicare = 0;
int iUserTakeHome = 0;
int iFedExclude= i_BI_WEEKLY_EXCLUDE;
int iAllDeductions =0;

double dBinaryStepToTry = 0.0;    // Set real value when we have input argument

System.out.println (” argv.length ” + argv.length );

if ( argv.length < 1 ) {    // No args, Say what to do.
System.out.println( “calc Take home pay, b/d/w, [ni] “);
return;
} else {                    // There are args,the first should be dollar value.
iUserTakeHome = Integer.parseInt( argv[0] ) * 100;
}

/* Nice idea but this has to look at [ 0 ] only if [ 0 ] won’t parse as a number…
* initialize to some magic value and then check for it as a parse failure? Later….
*            case ‘?’:
*                      case ‘h’:
*                      case ‘H’:
*                         System.out.println( “calc Take home pay, b/d/w, [ni] “);
*/

System.out.print( “Calculate salary for take home pay of ” + asCash(iUserTakeHome) + ” “);

if ( argv.length > 1 ) {
switch (argv[1].charAt(0)){
case ‘b’:
switch (argv[1].charAt(2)){
case ‘w’: iFedExclude= i_BI_WEEKLY_EXCLUDE;
System.out.println( “\tbiweekly”);
break;
case ‘m’: iFedExclude= i_BI_MONTHLY_EXCLUDE;
System.out.println( “\tbimonthly”);
break;
default: iFedExclude= i_BI_WEEKLY_EXCLUDE;
System.out.println( “\t DEFAULT biweekly”);
break;
} // end switch
break;
case ‘d’: iFedExclude= i_DAILY_EXCLUDE;
System.out.println( “\tdaily” );
break;
case ‘m’: iFedExclude= i_MONTHLY_EXCLUDE;
System.out.println( “\tmonthly” );
break;
case ‘q’: iFedExclude= i_MONTHLY_EXCLUDE;
System.out.println( “\tquarterly not implemented” );
break;
case ‘w’: iFedExclude= i_WEEKLY_EXCLUDE;
System.out.println( “\tweekly” );
break;

default: iFedExclude= i_BI_WEEKLY_EXCLUDE;
System.out.println( “\t DEFAULT biweekly”);
break;
} // end switch
} else {
System.out.println( “\tbiweekly” );
}

if ( argv.length > 2 ) { /* count fixed */
if ((( argv[2].charAt(0) == ‘n’ ) | (argv[2].charAt(0) == ‘N’ ))
& (( argv[2].charAt(1) == ‘i’ ) | (argv[2].charAt(1) == ‘I’ ))) {
dFedW = 0.0;
dStateW = 0.0;
iFedExclude= 0; // probably overkill
}
}

dBinaryStepToTry = 2 * iUserTakeHome;
while ( iCalcTakeHomeToTry != iUserTakeHome ) { // since they’re ints…

//while (( dBinaryStepToTry > 0.0005 )) {

iCalcGross = (int) ( ((double) iCalcGross) + dBinaryStepToTry );  // add it in
//System.out.println(  “dBinaryStepToTry = ” + dBinaryStepToTry
//    +”  iCalcGross = ” + iCalcGross );

iCalcFedIncTax = (int) (dFedW * (iCalcGross – iFedExclude));
if ( iCalcFedIncTax < 0 ) { iCalcFedIncTax = 0; }

iCalcStateIncTax = (int) (dStateW * iCalcGross);
iCalcSDI =  (int) (dSDIW * iCalcGross);
iCalcSSI =  (int) (dSSIW * iCalcGross);
iCalcMedicare =  (int) (dMedW * iCalcGross);

iCalcTakeHomeToTry = iCalcGross;        // then take deductions
iAllDeductions = iCalcFedIncTax + iCalcStateIncTax + iCalcSDI + iCalcSSI + iCalcMedicare;
iCalcTakeHomeToTry = iCalcGross – iAllDeductions;
//System.out.println(  “iCalcTakeHomeToTry = iCalcGross – iAllDeductions ” + iCalcTakeHomeToTry + ” “+  iCalcGross +” “+ iAllDeductions );

if (iCalcTakeHomeToTry > iUserTakeHome ) {    // too big
iCalcGross = (int) ( ((double) iCalcGross) – dBinaryStepToTry );  // take if back out
}
if (iCalcTakeHomeToTry < iUserTakeHome ) {    // too small
iCalcGross = ( iCalcGross + (iUserTakeHome – iCalcTakeHomeToTry ) );  // add difference back in
}

dBinaryStepToTry /= 2;                // divide by two
}

//     System.out.println(  “Success!” );
System.out.println(  “Gross $” + asCash(iCalcGross) + ” gives ”
+ “net $” + asCash(iCalcTakeHomeToTry) );

System.out.println(  “Details:” );
System.out.println(  “Gross\t”
+ “SSI\t”
+ “Fed\t”
+ “state\t”
+ “SDI\t”
+ “Medi\t”
+ “net\t”
);

System.out.println( asCash(iCalcGross) + “\t”
+ asCash(iCalcSSI )+ “\t”
+ asCash(iCalcFedIncTax) + “\t”
+ asCash(iCalcStateIncTax ) +” \t”
+ asCash(iCalcSDI )+ “\t”
+ asCash(iCalcMedicare )+ “\t”
+ asCash(iCalcTakeHomeToTry ) );

System.out.println( “Total Witholding $” + asCash(iCalcGross – iUserTakeHome) );

uIC =  (int) (dUIW * iCalcGross);
eTTC =  (int) (dETTW * iCalcGross);
iCalcFuta =  (int) (dFutaW * iCalcGross);

System.out.println(  “And we contribute\n”
+ “UI\t”
+ “ETT\t”
+ “SSI\t”
+ “FUTA\t” );

System.out.println(
asCash(uIC) + “\t”
+  asCash(eTTC) + “\t”
+  asCash(iCalcSSI) + “\t”
+  asCash(iCalcFuta) + “\t” );

int weOweFeds, weOweState;
weOweFeds = weOweState = 0;
weOweFeds += iCalcFedIncTax;
weOweFeds += iCalcSSI;
weOweFeds += iCalcMedicare;
weOweFeds += iCalcSSI; // our part

weOweState += iCalcStateIncTax;
weOweState += iCalcSDI;
weOweState += iCalcFuta;
weOweState += uIC;
weOweState += eTTC;

System.out.println(  “We owe Feds ” + asCash(weOweFeds) +
“\t\tWe owe State ” + asCash(weOweState) );
}
}

Advertisements

2 responses to “When I started Java instruction

  1. Hi,
    Not sure that this is true:), but thanks for a post.

    Have a nice day
    Saurooon

  2. Hi Saurooon,
    Well, I’m not sure of what you’re not sure of… I filed our taxes, including babysitter payroll taxes, State and Federal, for years, using the C/C++ version, and used this version for the last 2 or 3 years…
    I admit I’m not sure there is an elegant set of equations that can be solved for this, until I actually find them, at which point I should probably use them!
    Bill

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s