Java Program to display the Person's details using Constructor and Methods | IT2305 - Java Programming Lab


AIM:
 To write a Java program to display the person's details using Constructor and Methods in IT2305 - Java Programming Laboratory.

SOURCE CODE:
import java.util.*;
class Person
{
 String name;
 String addr;
 int age;
public Person()
{
 name="null";
 addr="null";
 age=0;
 System.out.println("This is default ctor of person.");
}
public Person(String a,String b,int x)
{
 name=a;
 addr=b;
 age=x;
}
public void readP()
{
 System.out.println("ENTER THE PERSON DETAILS:");
 Scanner sin=new Scanner(System.in);
System.out.println("enter the name:");
 name=sin.next();
System.out.println("enter the address:");
 addr=sin.next();
System.out.println("enter the age:");
 age=sin.nextInt();
}
public void printP()
{
System.out.println("**********************************************************");
System.out.println("PERSON DETAILS ARE:");
System.out.println("name:"+name);
System.out.println("address:"+addr);
System.out.println("age:"+age);
System.out.println("***********************************************************");
}
}
class Faculty extends Person
{
  String quli;
  int sal;
 String col;
                                                                                                                                            
 String dept;
 public Faculty()
{
 super();
 quli="null";
 dept="null";
 col="null";
 sal=0;
 System.out.println("This is the default ctor of faculty.");
}
public Faculty(String a,String b,String c,int x,String na,String ad,int ag)
{

 super(na,ad,ag);
  quli=a;
 dept=b;
 col=c;
 sal=x;
 printF();
}
public void readF()
{
 super.readP();
 System.out.println("ENTER THE FACULTY DETAILS:");
 Scanner sin=new Scanner(System.in);
 System.out.println("enter the qualification:");
 quli=sin.next();
System.out.println("enter the emp dept:");
 dept=sin.next();
System.out.println("enter the col:");
 col=sin.next();
System.out.println("enter the salary:");
 sal=sin.nextInt();
}
public void printF()
{
 super.printP();
 System.out.println("FACULTY DETAILS ARE:");
 System.out.println("quali:"+quli);
 System.out.println("dept:"+dept);
 System.out.println("col:"+col);
 System.out.println("sal:"+sal);
 System.out.println("***********************************************************************\n");
}
}
class Stud extends Person
{
 int rno;
 int year;
 String dept;
 String sec;
public Stud()
{
 super();
 rno=0;
 year=0;
 dept="null";
 sec="null";
 System.out.println("This is the default ctor of Student.");

}
public Stud(int x,int y,String a,String b,String na,String ad,int ag)
{
 super(na,ad,ag);
 rno=x;
 year=y;
 dept=a;
 sec=b;
 printS();
}
public void readS()
{
 super.readP();
 System.out.println("ENTER THE STUDENT DETAILS:");
 Scanner sin=new Scanner(System.in);
System.out.println("enter the rno:");
 rno=sin.nextInt();
System.out.println("enter the year:");
 year=sin.nextInt();
System.out.println("enter the stud dept:");
 dept=sin.next();
System.out.println("enter the sec:");
 sec=sin.next();
}
public void printS()
{
 super.printP();
System.out.println("STUDENT DETAILS ARE:");
System.out.println("rno:"+rno);
System.out.println("dept:"+dept);
System.out.println("year:"+year);
System.out.println("sec:"+sec);
 System.out.println("*****************************************************************\n");
}
}
public class Detail
{
 public static void main(String arg[])
{
 int ch,age,sal,rno,year;
 String name,addr,quli,dept1,dept2,col,sec;
// Stud s=new Stud();
 System.out.println("*********************************************************************\n");
 Faculty f=new Faculty();
 System.out.println("***********************************************************************\n");
 Stud s=new Stud();
 System.out.println("*************************************************************************\n");
 do
{
 System.out.println("THE CHOICES ARE:\n 1.BY USING CONSTRUCTOR\n 2.BY USING METHOD\n");
 System.out.println("ENTER THE CHOICE:");
 Scanner sin=new Scanner(System.in);
 ch=sin.nextInt();
 switch(ch)
{
 case 1:
      System.out.println("parameterised ctor");
       System.out.println("ENTER THE PERSON DETAILS\n");
       System.out.println("enter the name:");
       name=sin.next();
       System.out.println("enter the address:");
       addr=sin.next();
       System.out.println("enter the age:");
       age=sin.nextInt();
       System.out.println("ENTER THE FACULTY DETAILS\n");
       System.out.println("enter the qulification:");
       quli=sin.next();
       System.out.println("enter the emp dept:");
       dept1=sin.next();
       System.out.println("enter the col:");
       col=sin.next();
 System.out.println("enter the salary:");
       sal=sin.nextInt();
       System.out.println("STUDENT DETAILS\n");
       System.out.println("enter the rno:");
       rno=sin.nextInt();
       System.out.println("enter the year:");
       year=sin.nextInt();
       System.out.println("enter the stu dept:");
       dept2=sin.next();
       System.out.println("enter the sec:");
       sec=sin.next();
       Faculty f1=new Faculty(quli,dept1,col,sal,name,addr,age);
       Stud s1= new Stud(rno,year, dept2,sec, name, addr,age);
 break;

case 2:
   f.readF();
   System.out.println("-----------------------------------------------------------------\n");
   f.printF();
   System.out.println("------------------------------------------------------------------\n");
   s.readS();
   System.out.println("------------------------------------------------------------------\n");
   s.printS();
   System.out.println("------------------------------------------------------------------\n");
  break;
}
}while(ch!=2);
}
}
Previous
Next Post »

Still not found what you are looking for? Try again here.