Java Proram to create class Staff and derive classes FullTimeStaff and PartTimeStaff from it - IProgramX

Q.  Define an abstract class “Staff” with members name and address. Define two sub-classes of this class – “FullTimeStaff” (department, salary) and “PartTimeStaff” (number-of-hours, rate-perhour).  Define appropriate constructors. Create n objects which could be of either FullTimeStaff or PartTimeStaff class by asking the user’s choice. Display details of all “FullTimeStaff” objects and all “PartTimeStaff” objects.


Program:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
abstract class Staff{
String name,address;
}
class FullTimeStaff extends Staff{
String department;
double salary;
public void accept() throws IOException{
System.out.println("Enter the name, address, department and salary: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine();
address=br.readLine();
department=br.readLine();
salary=Double.parseDouble(br.readLine());
}
public void display(){
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("Department: "+department);
System.out.println("Salary: "+salary);
System.out.println("----------------------");
}
}
class PartTimeStaff extends Staff{
int hours, rate;
public void accept() throws IOException{
System.out.println("Enter the name, address, No of working hours and rate per hour: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name=br.readLine();
address=br.readLine();
hours=Integer.parseInt(br.readLine());
rate=Integer.parseInt(br.readLine());
}
public void display(){
System.out.println("Name: "+name);
System.out.println("Address: "+address);
System.out.println("No of Working Hours: "+hours);
System.out.println("Rate per hour: "+rate);
System.out.println("----------------------");
}
}


public class sb1 {
public static void main(String [] args) throws IOException{
int i;
System.out.println("Select Any One: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("1.Full Time Staff");
System.out.println("2.Part Time Satff");
int ch=Integer.parseInt(br.readLine());
switch(ch){
case 1: 
System.out.println("Enter the number of Full Time Staff: ");
int n=Integer.parseInt(br.readLine());
FullTimeStaff [] l=new FullTimeStaff[n];
for(i=0;i<n;i++){
l[i]=new FullTimeStaff();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
System.out.println("Enter the number of Part Time Staff: ");
int m=Integer.parseInt(br.readLine());
PartTimeStaff [] h=new PartTimeStaff[m];
for(i=0;i<m;i++){
h[i]=new PartTimeStaff();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;

}

}

Output:

Select Any One: 
1.Full Time Staff
2.Part Time Satff
1
Enter the number of Full Time Staff: 
2
Enter the name, address, department and salary: 
suresh
rahuri
hr
4000
Enter the name, address, department and salary: 
girish
loni
comp
5000
Name: suresh
Address: rahuri
Department: hr
Salary: 4000.0
----------------------
Name: girish
Address: loni
Department: comp
Salary: 5000.0

----------------------

Post a Comment

1 Comments

  1. Slip12_1: Write a program to create parent class College(cno, cname, caddr) and derived
    class Department(dno, dname) from College. Write a necessary methods to display College
    details.
    import java.util.*;
    class college
    {
    int no;
    String name;
    String addr;

    }
    class Dept extends college
    {
    int dno;
    String dname;
    Scanner sc = new Scanner(System.in);
    public void accept()
    {
    System.out.println("enter no");
    no=sc.nextInt();
    System.out.println("enter name");
    name=sc.next();
    System.out.println("enter college address");
    addr=sc.next();
    System.out.println("enter depatrment no");
    dno=sc.nextInt();
    System.out.println("enter department name");
    dname=sc.next();
    }
    public void display()
    {
    System.out.println("college no"+no);
    System.out.println("college name"+name);
    System.out.println("college address"+addr);
    System.out.println("department number"+dno);
    System.out.println("department number"+dname);
    }
    public static void main(String a[])
    {
    Dept ob=new Dept();
    ob.accept();
    ob.display();

    }
    }

    ReplyDelete