JAVA INHERITANCE AND OVERRIDING

AIM:
          To write a java program to get the details of students and staff using inheritance and method overriding.
PROCEDURE:
1)Start the program.
2)Define the class personal and declare the necessary variables.
3)Define function, getdata() to get the details.
4)Define function, display() to print the details.
5)Declare the class marks and staff and get the details of marks, staff.
6)Overload the methods getdata() and display().
7)Declare the main class.
8)To enter the choice in main class using switch condition.
9)To get the input and display the details.
10)Save and execute the program.
11)Exit the program.










SOURCE CODE:
/*Inheritance and Overriding*/
import java.io.*;
class personal
{
            String name,regno,branch,address,gender;
             void getdata() throws IOException
             {
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter the name:");
                        name=br.readLine();
                        System.out.println("Enter branch:");
                        branch=br.readLine();
                        System.out.println("Enter gender M/F:");
                        gender=br.readLine();
                        System.out.println("Enter the address:");
                        address=br.readLine();
            }
            void display()throws IOException
            {
                        System.out.println("Name:"+name);
                        System.out.println("Branch:"+branch);
                        System.out.println("Gender:"+gender);
                        System.out.println("Address:"+address);
            }
}


class marks extends personal
{
            int total,m1,m2,m3,m4,m5;
            float avg;
            void getdata() throws IOException
            {
                        super.getdata();
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter The Mark1:");
                        m1=Integer.parseInt(br.readLine());
                        System.out.println("Enter The Mark2:");
                        m2=Integer.parseInt(br.readLine());
                        System.out.println("Enter The Mark3:");
                        m3=Integer.parseInt(br.readLine());
                        System.out.println("Enter The Mark4:");
                        m4=Integer.parseInt(br.readLine());
                        System.out.println("Enter The Mark5:");
                        m5=Integer.parseInt(br.readLine());
                        total=m1+m2+m3+m4+m5;               
                        avg=(float)total/5;
            }
            void display()throws IOException
            {
                        System.out.println("\n**********************");
                        System.out.println("     PERSONAL DETAILS   ");
                        System.out.println("**********************");
                        super.display();
                        System.out.println("\n**********************");
                        System.out.println("     ACADEMIC DETAILS   ");
                        System.out.println("**********************");
                        System.out.println("Mark1:"+m1);
                        System.out.println("Mark2:"+m2);
                        System.out.println("Mark3:"+m3);
                        System.out.println("Mark4:"+m4);
                        System.out.println("Mark5:"+m5);                
                        System.out.println("Total:"+total);
                        System.out.println("Average:"+avg);
            }
}
class staff extends personal
{
            String staff_id;
            int bp,pf,lic,da,medical,hra,grosspay,netpay;
            void getdata()throws IOException
            {
                        super.getdata();
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter the staff id:");
                        staff_id=br.readLine();
                        System.out.println("Enter the basicpay:");
                        bp=Integer.parseInt(br.readLine());
                        System.out.println("Enter the pf:");
                        pf=Integer.parseInt(br.readLine());
                        System.out.println("Enter the medical allowance:");
                        medical=Integer.parseInt(br.readLine());
                        System.out.println("Enter the LIC:");
                        lic=Integer.parseInt(br.readLine());
                        System.out.println("Enter the da:");
                        da=Integer.parseInt(br.readLine());
                        System.out.println("Enter the hra:");
                        hra=Integer.parseInt(br.readLine());
                        grosspay=bp+pf+lic+da+medical+hra;
            }
            void display()throws IOException
            {
            System.out.println("\n**********************");
            System.out.println("      PERSONAL DETAILS    ");
            System.out.println("**********************");
            super.display();
            System.out.println("\n**********************");
            System.out.println("    STAFF DETAILS     ");
            System.out.println("**********************");
            System.out.println("Staff id:"+staff_id);
            System.out.println("Basic pay:"+bp);
            System.out.println("LIC:"+lic);
            System.out.println("Daily allowance:"+da);
            System.out.println("Medical allowance:"+medical);
            System.out.println("HRA:"+hra);
            System.out.println("Grosspay:"+grosspay);
            System.out.println("Netpay:"+(grosspay-pf-lic-medical));
            }
}


class main
{
            public static void main(String args[])throws IOException
            {
                        int ch;
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("1.Acadamic details\n2.Staff details\n");
                        System.out.println("Enter the choice:");
                        ch=Integer.parseInt(br.readLine());
                        switch(ch)
                        {
                         case 1:
                                    marks m=new marks();
                                    m.getdata();
                                    m.display();
                                    break;
                         case 2:
                                    staff s=new staff();
                                    s.getdata();
                                    s.display();
                                    break;
                        default:
                                    System.out.println("Enter correct choice!");
                        }
            }
}


OUTPUT:
1.Acadamic details
2.Staff details

Enter the choice:1
Enter the name:4Cnotes
Enter branch:mca
Enter gender M/F:m
Enter the address:trichy
Enter The Mark1:90
Enter The Mark2:99
Enter The Mark3:98
Enter The Mark4:89
Enter The Mark5:88

**********************
   PERSONAL DETAILS
**********************
Name:4Cnotes
Branch:mca
Gender:m
Address:trichy







**********************
     ACADEMIC DETAILS
**********************
Mark1:90
Mark2:99
Mark3:98
Mark4:89
Mark5:88
Total:464
Average:92.8

0 comments: