Monday, 1 July 2013

Using Inheritance and Polymorphism in Program

 Using Inheritance and Polymorphism in Program


Using Inheritance and Polymorphism in Java Program, Calculating worker monthly Salary, Gross rate, rate tax and Making records.

Java Code:




public class TaxableWorker {

       String name;
       double payRate=12.50;
       double taxRate=0.25;

       TaxableWorker(String n, double Pr, double Tr){
              name=n;
              payRate=Pr;
              taxRate=Tr;
       }
      


       public void setPayRate(double Pr){
              payRate=Pr;
       }
       public void setTaxRate(double Tr){
              taxRate=Tr;
       }
       public void setName(String n){
              name=n;
       }
       public String getName(){
              return name;
       }
       public double getPayRate(){
              return payRate;
       }
       public double getTaxRate(){
              return taxRate;
       }
       public String ONEworkerInfo(){
              return String.format("%s: %s \n%s: %s \n%s: %s",
                           "NAME", getName(),
                           "hourly pay rate",getPayRate(),
                           "federal tax rate", getTaxRate() );
       }
       public double grossPay(int HW){
              double a=(HW*payRate);
              return a;
       }
       public double taxWithheld(double GP){
              double b=(GP * taxRate);
              return b;
       }
      
      
}


public class StateTaxableWorker extends TaxableWorker{
      
       String StateName;
       double StateTaxRate=0.05;
      

       StateTaxableWorker(String n, double Pr, double Tr, String Sn, double St) {
              super(n, Pr, Tr);
              StateName=Sn;
              StateTaxRate=St;
              // TODO Auto-generated constructor stub
       }
      

       public void setStateTaxRate(double St){
              StateTaxRate=St;
       }
       public double getStateTaxRate(){
              return StateTaxRate;
       }
       public void setStateName(String Sn){
              StateName=Sn;
       }
       public String getStateName(){
              return StateName;
       }
       @Override
       public String ONEworkerInfo(){
              return String.format("%s: %s \n%s: %s \n%s: %s \n%s: %s \n%s: %s ",
                           "NAME", getName(),
                           "hourly pay rate", getPayRate(),
                           "federal tax rate ", getTaxRate(),
                           "StateName",getStateName(),
                           "StateTaxRate",getStateTaxRate() );
       }
       @Override
       public double taxWithheld(double GP){
              double b=((GP * taxRate) + (GP * StateTaxRate));
              return b;
       }
      

      

}


public class LocalTaxableWorker extends StateTaxableWorker {
        String cityName;
        double cityTax=0.025;

       LocalTaxableWorker(String n, double Pr, double Tr, String Sn, double St, String CN, double CT) {
              super(n, Pr, Tr, Sn, St);
              cityName=CN;
              cityTax=CT;
              // TODO Auto-generated constructor stub
       }
       public void setCityName(String CT){
              cityName=CT;
       }
       public String getCityName(){
              return cityName;
       }
       public void setCityTax(double CN){
              cityTax=CN;
       }
       public double getCityTax(){
              return cityTax;
       }
       @Override
       public String ONEworkerInfo(){
              return String.format("%s: %s \n%s: %s \n%s: %s \n%s: %s \n%s: %s \n%s: %s \n%s: %s ",
                           "NAME", getName(),
                           "hourly pay rate",getPayRate(),
                           "federal tax rate ", getTaxRate(),
                           "StateName",getStateName(),
                           "StateTaxRate",getStateTaxRate(),
                           "City Name",getCityName(),
                           "City Tax ", getCityTax() );
       }
       @Override
       public double taxWithheld(double GP){
              double b=((GP * taxRate) + (GP * StateTaxRate) + (GP * cityTax));
              return b;
       }
      
      

}



public class test {

       /**
        * @param args
        */
       StateTaxableWorker STW=new StateTaxableWorker("saadk", 120, 60, "Sname", 90);
       LocalTaxableWorker LTW=new LocalTaxableWorker("hello", 40, 50, "Sname", 550, "Cname", 0);
       TaxableWorker TW=new TaxableWorker("saad", 10, 40);
       public void display(){
              System.out.println("information about the worker:");
              System.out.println(TW.ONEworkerInfo());
              System.out.print("gross Pay:");
              System.out.println(TW.grossPay(4));
              System.out.print("total amount of tax withheld for the worker:");
              System.out.println(TW.taxWithheld(100));
             
              TaxableWorker[] Tw=new TaxableWorker[2];
              Tw[0]=STW;
              Tw[1]=LTW;
             
              for (TaxableWorker ab : Tw){
                     System.out.println(ab);
             
              if(ab instanceof StateTaxableWorker){
                     StateTaxableWorker T =(StateTaxableWorker) ab;
                     System.out.println(T.ONEworkerInfo());
                     System.out.print("tax on state :");
                     System.out.println(T.taxWithheld(500));
              }
              else
              if(ab instanceof LocalTaxableWorker){
                     LocalTaxableWorker B =(LocalTaxableWorker) ab;
                     System.out.println(B.ONEworkerInfo());
                     System.out.print("tax on city :");
                     System.out.println(B.taxWithheld(500));
                    
              }
              }
             
                          
       }
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              test run=new test();
              run.display();

      

       }

}

Screenshot:

 


No comments:

Post a Comment