Write a Java Program to implement array of objects.
Program Code:
import java.lang.*;
public class EmployeeTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("Mukesh Rajput", 7500);
staff[1] = new Employee("Ravi Kant", 8500);
staff[2] = new Employee("Rahul Sharma", 6500);
for (int i = 0; i < 3; i++)
staff[i].print();
}
}
class Employee
{
private String name;
private double salary;
public Employee(String n, double s)
{
name = n;
salary = s;
}
public void print()
{
System.out.println(name + " " + salary);
}
}
Output:
Mukesh Rajput 7500.0
Ravi Kant 8500.0
Rahul Sharma 6500.0
Post A Comment:
0 comments: