Program on Array of objects
Mr. John has joined the CGI Company as Database
Administrator and his major role is to maintain the Employee
database which includes Employee Id, Employee Name, Age
and Employee Salary. The criteria for maintaining is that he
needs to store all the records in one single table. How Mr. John
is going to access all the records which he has stored?
Solution:
import java.util.Scanner;
class Employee
{
int Id;
String Name;
int Age;
long Salary;
void GetData() // Defining GetData()
{
Scanner sc = new Scanner(System.in);
System.out.print("\n\tEnter Employee Id : ");
Id = sc.nextInt();
System.out.print("\n\tEnter Employee Name : ");
Name = sc.next();
System.out.print("\n\tEnter Employee Age : ");
Age = sc.nextInt();
System.out.print("\n\tEnter Employee Salary : ");
Salary = sc.nextLong();
void PutData() // Defining PutData()
{
System.out.print("\n\t" + Id + "\t" +Name + "\t" +Age + "\t"
+Salary);
}
public static void main(String args[])
{
Employee[] Emp = new Employee[3];
int i;
for(i=0;i<3;i++)
Emp[i] = new Employee(); // Allocating memory to each
object
for(i=0;i<3;i++)
{
System.out.print("\nEnter details of "+ (i+1) +" Employee\n");
Emp[i].GetData();
}
System.out.print("\nDetails of Employees\n");
for(i=0;i<3;i++)
Emp[i].PutData();
}}