Write a program to create an employee class and set the variable through methods? Java Assignment Questions, Java Assignment, Java Questions.
Code:-
Java
/*
*Objective:- Write a program to create an employee class
and set the variable through methods?
*/
// Create Class
public class Employee {
//Variables
private String name;
private int id;
private double salary;
//Setter and Getter Methods
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getSalary() {
return salary;
}
// Main method
public static void main(String[] args) {
Employee emp1 = new Employee();
// Set the variable value using setter methods
emp1.setName("Shivam Kumar");
emp1.setId(2022098);
emp1.setSalary(60000.0);
// Print value
System.out.println("Employee Name: " + emp1.getName());
System.out.println("Employee ID: " + emp1.getId());
System.out.println("Employee Salary: ₹" + emp1.getSalary());
}
}
Output:
Employee Name: Shivam Kumar
Employee ID: 2022098
Employee Salary: ₹60000.0