Write a program to create an employee class and set the variable using the method? Java Assignment Questions, Java Assignment, Java Questions.
Code:
Java
/*
*Objective:- Write a program to create an employee class
and set the variable using the Through method?
*/
// Create a class
class Employee {
private String name;
private int age;
private double salary;
//Getters and Setters
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
// Main method
public static void main(String[] args){
//Create new Object of Employee class
Employee e = new Employee();
// Setter method to set values
e.setName("Yogesh Kumar");
e.setAge(25);
e.setSalary(25000.00);
// Access the values set by Method
System.out.println("Name: "+ e.getName());
System.out.println("Age: " +e.getAge());
System.out.println("Salary: " +e.getSalary());
// Alternate Method
System.out.println("\nAlternate Way:-");
System.out.println("Name: "+ e.name);
System.out.println("Age: " +e.age);
System.out.println("Salary: " +e.salary);
}
}
Write a program to create an employee class and set the variable using the method?
Output:
Name: Yogesh Kumar
Age: 25
Salary: 25000.0
Alternate Way:-
Name: Yogesh Kumar
Age: 25
Salary: 25000.0