write a valid Java statement to create an object for a class “employee”.
Answers
Answer:
import java.util.Scanner;
public class Employee {
int empid;
String name;
float salary;
public void getInput() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the empid :: ");
empid = in.nextInt();
System.out.print("Enter the name :: ");
name = in.next();
System.out.print("Enter the salary :: ");
salary = in.nextFloat();
}
public void display() {
System.out.println("Employee id = " + empid);
System.out.println("Employee name = " + name);
System.out.println("Employee salary = " + salary);
}
public static void main(String[] args) {
Employee e[] = new Employee[5];
for(int i=0; i<5; i++) {
e[i] = new Employee();
e[i].getInput();
}
System.out.println("**** Data Entered as below ****");
for(int i=0; i<5; i++) {
e[i].display();
}
}
}
OUTPUT
C:\>javac Employee.java
C:\>java Employee
Enter the empid :: 101
Enter the name :: Smith
Enter the salary :: 30250.0
Enter the empid :: 103
Enter the name :: Harsh
Enter the salary :: 23560.0
Enter the empid :: 105
Enter the name :: Subhash
Enter the salary :: 19823.0
Enter the empid :: 203
Enter the name :: Rohan
Enter the salary :: 25230.0
Enter the empid :: 201
Enter the name :: Girish
Enter the salary :: 32000.0
**** Data Entered as below ****
Employee id = 101
Employee name = Smith
Employee salary = 30250.0
Employee id = 103
Employee name = Harsh
Employee salary = 23560.0
Employee id = 105
Employee name = Subhash
Employee salary = 19823.0
Employee id = 203
Employee name = Rohan
Employee salary = 25230.0
Employee id = 201
Employee name = Girish
Employee salary = 32000.0