Thursday, January 24, 2013

Calculating Tax in Java

It's so very simple java code, you can copy paste to your java editor like netbeans or Ecplise.

For first time you need to create three classes such as Tax.java (red: Tax dot java), TestTax.java (red: TestTax dot java) and the last is NJTax.java (red: NJtax dot java). For NJTax.java it's not must it, you freely chose name which you want to be named it like UStax.java, MyTax.java. etc.

Okey here we go, let's started to create fun java program :

Tax.java
public class Tax {
 double grossIncome;
 String state;
 int dependents;
 
 public double calcTax(){
  return 284.90;
  }

}
TestTax.java (Default version)
class TestTax {
  public static void main (String [] args){
   //Tax t=new tax(); // creating an instance
   NJTax t=new NJTax(); // creating an instance
   
   t.grossIncome=500000; //assigning the values
   t.dependents=2;
   t.state="NJ";
   
   double yourTax = t.calcTax(); //calculating tax
   
   //Print The Result;
   System.out.println("Your tax is " + yourTax);
 }

}

TestTax.java (NJTax version)
class TestTax {
 public static void main (String [] args){
 //Tax t=new tax(); // creating an instance
 NJTax t=new NJTax(); // creating an instance
 
 t.grossIncome=500000; //assigning the values
 t.dependents=2;
 t.state="NJ";
   
 double yourTax = t.calcTax(); //calculating tax
   
 //Print The Result;
 System.out.println("Your tax is " + yourTax);
 }

}


NJTax.java
public class NJTax extends Tax {
       public double calcTax() {
    double stateTax=0;
    if (grossIncome<500000){
     stateTax=grossIncome*0.05;
    }
    else{
     stateTax=grossIncome*0.06;
    }
    return (stateTax-500);
 }

}


Oke, that's finished. Please remember java is the case sensitive coding so be carefully when type the program. But put in your brain I must find my mistake when writing code in any program language. After where your mistaken when you writing code and find the solution for your code.