0
\$\begingroup\$

WAP to create a class called Employee having empid and salary as data members and salary as data members. Create objects of this class and count no. of employees working in an organization using static keyword.

This is my solution for the above question. Is it correct way of doing this? I am a beginner so I would appreciate your help. Let me know of my mistakes.

    import java.util.*;
    class Employee{
private int empid;
private int salary;
private static int count;

private void setValue(int empid,int salary){
this.empid=empid;
this.salary=salary;
count++;
}

private void getValue(){
System.out.println("EmpId = "+empid);
System.out.println("Salary ="+salary);
System.out.println("Number of employees: = "+count);
}

public static void main(String[] args)
{
    Scanner kb=new Scanner(System.in);
    Employee e=new Employee();
    int id,sal;
    System.out.println("Enter Employee Id: ");
    id=kb.nextInt();
    System.out.println("Enter Salary: ");
    sal=kb.nextInt();
    System.out.println("The Values entered are :");
    e.setValue(id,sal);
    e.getValue(); 
    Employee e1=new Employee();
    System.out.println("Enter Employee Id: ");
    id=kb.nextInt();
    System.out.println("Enter Salary: ");
    sal=kb.nextInt();
    System.out.println("The Values entered are :");
    e1.setValue(id,sal);
    e1.getValue();
}}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Downvoted because you really should be able to understand how classes work (e.i. how to use a constructor, how to use the most basic methods, ...). Still answered because I do want you to actually learn how to do it correctly. \$\endgroup\$
    – Imus
    Commented Mar 7, 2018 at 14:42
  • \$\begingroup\$ I have made changes according to your answer(in the question itself). Please review it once. I hope now its fine. \$\endgroup\$ Commented Mar 7, 2018 at 15:47
  • 3
    \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Heslacher
    Commented Mar 7, 2018 at 15:52
  • 3
    \$\begingroup\$ @Imus Seems a bit harsh to downvote because someone's a beginner, and doesn't know everything about programming yet. It took me a couple of years till I could use classes. \$\endgroup\$
    – Peilonrayz
    Commented Mar 7, 2018 at 15:56
  • \$\begingroup\$ @Heslacher Thanks for your suggestion. I will remove the answer from my question and I am new to StackExchange so I didn't know that. \$\endgroup\$ Commented Mar 7, 2018 at 16:19

2 Answers 2

0
\$\begingroup\$

No.

You use a setter to modify an existing employee. But it makes no sense to change the id and salary together. In fact, you should never change the id of an employee.

This should be the constructor of the class instead. In your main I would expect to see the folowing flow:

Scanner kb=new Scanner(System.in);
int id,sal;
System.out.println("Enter Employee Id: ");
id=kb.nextInt();
System.out.println("Enter Salary: ");
sal=kb.nextInt();
System.out.println("The Values entered are :");
Employee e=new Employee(id, sal);

A second thing to note is that a method with prefix "get" is expected to return some value of the object. Either rename that getValue() method to something like printInfo() or provide actual getters:

public String getId() {
    return id;
}

public static int getNbOfEmployees(){
    return count;
}

These can then be used somewhere else to print out the info, for example in your main:

System.out.println("EmpId = " + e.getId());
System.out.println("Salary =" + e.alary());
System.out.println("Number of employees: = " + Employee.getNbOfEmployees());
\$\endgroup\$
0
0
\$\begingroup\$

I have made the following changes according to @Imus. For multiple employees, I think using an array of object references would be great.

    class Employee{
private int empid;
private int salary;
private static int count;

Employee(int empid,int salary){
this.empid=empid;
this.salary=salary;
count++;
}

public int getId(){
return empid;
}

public int getSalary(){
return salary;
}

public static int getNumberOfEmployees(){
return count;
}

public static void main(String[] args)
{
    Scanner kb=new Scanner(System.in);
    int id,sal;
    System.out.println("Enter Employee Id: ");
    id=kb.nextInt();
    System.out.println("Enter Salary: ");
    sal=kb.nextInt();
    Employee e=new Employee(id,sal);
    System.out.println("The values are :");
    System.out.println("EmpId = "+e.getId());
    System.out.println("Salary = "+e.getSalary());
    System.out.println("No. of employees = "+Employee.getNumberOfEmployees());

}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.