fork download
  1. class Employee {
  2. // Private fields (encapsulation)
  3. private String name;
  4. private double salary;
  5.  
  6. // Setter method (mutator)
  7. public void setName(String n) {
  8. name = n;
  9. }
  10.  
  11. // Getter method (accessor)
  12. public String getName() {
  13. return name;
  14. }
  15.  
  16. // Setter method for salary
  17. public void setSalary(double s) {
  18. if (s > 0) { // Validation to ensure positive salary
  19. salary = s;
  20. }
  21. }
  22.  
  23. // Getter method for salary
  24. public double getSalary() {
  25. return salary;
  26. }
  27.  
  28. public static void main(String[] args) {
  29. Employee emp = new Employee();
  30. emp.setName("John");
  31. emp.setSalary(50000);
  32.  
  33. System.out.println("Name: " + emp.getName());
  34. System.out.println("Salary: " + emp.getSalary());
  35. }
  36. }
  37.  
Success #stdin #stdout 0.17s 55740KB
stdin
Standard input is empty
stdout
Name: John
Salary: 50000.0