Class, Objects and Constructors

Program to create a class of employee with empno, name and salary as its parameters. Accept and display the details for 3 employees.

import java.io.*;

public class Employee
{
          int empno;
          String name;
          float sal;

          // Default Constructor
          Employee()
          {
                    empno = 0;
                    name = " ";
                    sal = 0.0f;
          }

          // Parameterised Constructor
          Employee(int eno, String ename, float esal)
          {
                    empno = eno;
                    name = ename;
                    sal = esal;
          }

          void display()
          {
                    System.out.println("\nEmpno: " + empno);
                    System.out.println("Name: " + name);
                    System.out.println("Salary: " + sal);
          }

          public static void main(String[] args) throws IOException
          {
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                    Employee m[] = new Employee[3];
                    int eno, i;
                    String ename;
                    float esal;

                    for (i = 0; i < 3; i++)
                    {
                              // Read Values
                              System.out.println("\nEnter Employee Details::");
                              System.out.print("Enter Employee Number:: ");
                              eno = Integer.parseInt(br.readLine());
                              System.out.print("Enter Employee Name:: ");
                              ename = br.readLine();
                              System.out.print("Enter Salary:: ");
                              esal = Float.parseFloat(br.readLine());

                              Employee e1 = new Employee(eno, ename, esal);
                              m[i] = e1;
                    }

                    // Display Information
                    System.out.println("\n\nEmployee Information::");
                    for (i = 0; i < 3; i++)
                    {
                              m[i].display();
                    }
          }
}

OUTPUT::

Enter Employee Details::
Enter Employee Number:: 1
Enter Employee Name:: abc
Enter Salary:: 10000

Enter Employee Details::
Enter Employee Number:: 2
Enter Employee Name:: xyz
Enter Salary:: 12000

Enter Employee Details::
Enter Employee Number:: 3
Enter Employee Name:: pqr
Enter Salary:: 8000


Employee Information::

Empno: 1
Name: abc
Salary: 10000.0

Empno: 2
Name: xyz
Salary: 12000.0

Empno: 3
Name: pqr
Salary: 8000.0



Similarly,
You can write some programs like::
  • Program to create a class of student with rollno, name and marks as its parameters. Accept and display the details for 3 students.
  • Program to create a class of product with id,name and price as its parameters. Accept and display the details for 3 products.
  • etc..

-----

Firoz Memon

Please view my other blogs:

          C++ Codes 4 Beginners

          Java Tips

          Java 4 Beginners

Previous Post Next Post