Answer to Question #253531 in Java | JSP | JSF for himanshu

Question #253531

Write a java program for the following-

Class person

Derive two classes-student and employee [identify inheritance]

For student

Input: Name, roll no, marks in 4 subjects (use arrays of object]

Output: student information, total marks [create necessary function, Use of constructor

For Employee

Input: empno, empname, basic

Process:

a. DA=50%of basic

N

b. HRA=25%of basic

c. CCA=Rs240/-

d. PF=10%of basic


1
Expert's answer
2021-10-19T16:18:41-0400


SOLUTION CODE FOR THE QUESTION ABOVE


package com.company;

import java.util.*;

// Base Class Person
class Person{

    // Private Fields
    private String name;

    public void person_setData(String name){
        this.name = name;
    }

    // public method to print details
    public void printDetails() {
        System.out.println("\t\tName: " + name);
    }

}
// Derived Class student from person class to make use of of array object
class student extends Person{
    // Private field
    private String roll_number;
    private int subject_1_mark;
    private int subject_2_mark;
    private int subject_3_mark;
    private int subject_4_mark;

    public void setData( String n,String d, int sub1, int sub2, int sub3, int sub4){
        //calling the base class set data method
        person_setData(n);
        this.roll_number= d;
        this.subject_1_mark = sub1;
        this.subject_2_mark = sub2;
        this.subject_3_mark = sub3;
        this.subject_4_mark = sub4;
    }

    public void print_student_details() {
        //calling print method of the from parent class
        printDetails();
        System.out.println("\t\tStudent roll number: " + roll_number);
        System.out.println("\t\tmarks for subject 1: " + subject_1_mark);
        System.out.println("\t\tmarks for subject 2: " + subject_2_mark);
        System.out.println("\t\tmarks for subject 3: " + subject_3_mark);
        System.out.println("\t\tmarks for subject 4: " + subject_4_mark);
        //calculate the total marks
        int total_marks = subject_1_mark + subject_2_mark + subject_3_mark+subject_4_mark;
        System.out.println("\t\tTotal marks :"+total_marks);
    }

}

//derived class for Employee
class Employee extends Person{
    // Private field
    private String employee_number;
    private int basic;

    public void setData( String n,String e_n, int b){
        //calling the base class set data method
        person_setData(n);
        this.employee_number= e_n;
        this.basic = b;
    }

    public void print_employee_details() {
        //calling print method of the from parent class
        printDetails();
        System.out.println("\t\tEmployee  number: " +employee_number );
        System.out.println("\t\tEmployee basic : " + basic);

        //Now lets calculate the gross salary and net salary
        float f = basic;
        float DA,HRA,PF,gr_sal,net_sal;
        float CCA=240f,PT=100f;
        DA=(0.5f)*f;
        HRA=(0.25f)*f;
        PF=(0.1f)*f;
        gr_sal=(f + DA + HRA);
        net_sal=(gr_sal - CCA - PT - PF);
        System.out.println("\t\tEmployee Gross Salary : " + gr_sal);
        System.out.println("\t\tEmployee Net Salary : " + net_sal);

    }

}

class Main {

    public static void main(String   []args)
    {
        Scanner  sc= new Scanner(System.in);
        //Now working with students class
        System.out.println("\nNow dealing with Student\n");
        System.out.print("Enter student roll Number: ");
        String roll_number =sc.next();
        System.out.print("Enter student name: ");
        String student_name =sc.next();
        System.out.print("Enter marks for subject 1: ");
        int subject_1 =sc.nextInt();
        System.out.print("Enter marks for subject 2: ");
        int subject_2 =sc.nextInt();
        System.out.print("Enter marks for subject 3: ");
        int subject_3 =sc.nextInt();
        System.out.print("Enter marks for subject 4: ");
        int subject_4 =sc.nextInt();

        //create array of student object
        student[] student_object = new student[6] ;
        //create actual student object
        student_object[0] = new student();
        //assign data to student object
        student_object[0].setData(student_name,roll_number, subject_1, subject_2, subject_3,subject_4);
        //display the student object data
        System.out.println("Student Object details:");
        student_object[0].print_student_details();


        //Now dealing with Employee class
        System.out.println("\nNow dealing with Employee\n");
        System.out.print("Enter employee Number: ");
        String employee_number =sc.next();
        System.out.print("Enter your basic: ");
        int basic =sc.nextInt();
        System.out.print("Enter employee name: ");
        String employee_name =sc.next();
        //create array of Employee object
        Employee[] Employee_object = new Employee[3] ;
        //create actual Employee object
        Employee_object[0] = new Employee();
        //assign data to Employee object
        Employee_object[0].setData(employee_name,employee_number,basic);
        //display the Employee object data
        System.out.println("\nEmployee Object details:");
        Employee_object[0].print_employee_details();
        
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS