I have created a class employee in Java, where each object of the class stands for a staff. The objects take 3 parameters – Name, Dept. and Salary. The program looks like this:
public class employee
{
String name;
int salary;
String dept;
employee staff1 = new employee("x","IT",100000);
employee staff2 = new employee("y", "HR", 200000);
public employee(String n, String d, int s)
{
this.name= n;
this.salary= s;
this.dept = d;
}
public static void main (String args [])
{
}
public void Display()
{
}
}
I want to make a method (the Display method in the code) which takes the object name as a parameter (the code does not have a parameter) and returns (or prints) its data values. Please also tell me what should come in the main method. Thanks in advance.
>Solution :
first of all try to study the programming basics, it’s an embarrassing question
public class Employee {
private String name;
private int salary;
private String dept;
public employee() {
}
public employee(String n, String d, int s) {
this.name= n;
this.salary= s;
this.dept = d;
}
public static void main (String args[]) {
Employee staff1 = new Employee("x", "IT", 100000);
staff1.display();
Employee staff2 = new Employee("y", "HR", 200000);
staff2.display();
}
public void display() {
System.out.println(this);
}
public String toString() {
return "Name: " + name + ", Salary: " + salary + ", Department: " + dept;
}
}