Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Sunday, 31 July 2011

Program to Implement abstract class


import java.io.*;
import java.util.*;
abstract class Employee
{
            private String name;
            private int ssn;
            Employee(String s,int a)
            {
                        setname(s);
                        setssn(a);
                        getname();
                        getssn();

            }
            void setname(String s1)
            {
                        name=s1;
            }
            void setssn(int a1)
            {
                        ssn=a1;
            }
            void getname()
            {
                        System.out.println("NAME:"+name);
            }
            void getssn()
            {
                        System.out.println("SSN:"+ssn);
            }
            abstract void earnings();
}
class Commissionemp extends Employee
{
            int sal,comis,qty;
            Commissionemp(String x,int y,int a,int b,int c)
            {
                        super(x,y);
                        setsal(a);setcomis(b);setqty(c);getsal();getcomis();getqty();
            }
            void setsal(int x)
            {
                        sal=x;
            }
            void setcomis(int y)
            {
                        comis=y;
            }
            void setqty(int z)
            {
                        qty=z;
            }
            void getsal()
            {
                        System.out.println("SALARY="+sal);
            }
            void getcomis()
            {
                        System.out.println("COMMISSION="+comis);
            }
            void getqty()
            {
                        System.out.println("QUANTITY="+qty);
            }
            void earnings()
            {System.out.println("Earnings of the commission Employee:"+((sal+comis)*qty));}
}
class Piecework           extends Employee      
{
            int wages,qty;
            Piecework(String x,int y,int a,int b)
            {
                        super(x,y);
                        setwages(a);
                        setqty(b);
                        getwages();
                        getqty();
            }
            void setwages(int a1)
            {
                        wages=a1;
            }
            void setqty(int b1)
            {
                        qty=b1;
            }
            void getwages()
            {          System.out.println("WAGES="+wages);       }
            void getqty()
            {          System.out.println("QUANTITY="+qty);     }
            void earnings()
            {System.out.println("Earnings of the piecework Employee:"+(wages*qty));}         
}
class Hourlyworks extends Employee
{
            int wages,hrs;
            Hourlyworks(String x,int y,int m,int n)
            {
                        super(x,y);
                        setwages(m);
                        sethrs(n);
                        getwages();
                        gethrs();
            }

            void setwages(int m1)
            {
                        wages=m1;
            }
            void sethrs(int n1)
            {
                        hrs=n1;
            }
            void getwages()
            {          System.out.println("WAGES="+wages);       }
            void gethrs()
            {          System.out.println("HOURS="+hrs); }
            void earnings()
            {System.out.println("Earnings of the hourly work Employee:"+(wages*hrs));}
}
class Mainemp
{
            public static void main(String args[])
            {
                        Commissionemp c=new Commissionemp("emp1",1,1500,10,100);
                        c.earnings();
                        Piecework p=new Piecework("emp2",2,200,100);
                        p.earnings();
                        Hourlyworks h=new Hourlyworks("emp3",3,50,24);
                        h.earnings();
            }
}                                             

Output

NAME:emp1
SSN:1
SALARY=1500
COMMISSION=10
QUANTITY=100
Earnings of the commission Employee:151000
NAME:emp2
SSN:2
WAGES=200
QUANTITY=100
Earnings of the piecework Employee:20000
NAME:emp3
SSN:3
WAGES=50
HOURS=24
Earnings of the hourly work Employee:1200

No comments:

Post a Comment

Followers