Java Create a class called Employee that includes three instance variables—a fir
ID: 3847431 • Letter: J
Question
Java
Create a class called Employee that includes three instance variables—a
first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor
that initializes the three instance variables. Provide a set and a get method for each instance
variable. If the monthly salary is not positive, do not set its value. Write a test app named EmployeeTest
that demonstrates class Employee’s capabilities. Create two Employee objects and display each
object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary
again.
Explanation / Answer
PROGRAM:-
import java.util.*;
import java.lang.*;
import java.io.*;
class Employee
{
private String firstName, lastName;
private double monthlySalary;
public String getFirstName(){
return this.firstName;
}
public void setFirstName(String name){
this.firstName = name;
}
public String getLastName(){
return this.lastName;
}
public void setLastName(String name){
this.lastName = name;
}
public Double getMonthlySalary(){
return this.monthlySalary;
}
public void setMonthlySalary(double salary){
if( salary >= 0)
this.monthlySalary = salary;
else
System.out.println("Salary not set, because you entered a negative value");
}
Employee(String fn, String ln, double salary){
firstName = fn;
lastName = ln;
monthlySalary = salary;
}
public static void main (String[] args) throws java.lang.Exception
{
/* EmployeeTest */
Employee e1 = new Employee("Jon","Duckett",59.23); //object 1
Employee e2 = new Employee("Rupert","Grunt",241.32); //object 2
/*Initially instance variable values are as specified in constructor */
System.out.println("Name of employee e1: "+e1.getFirstName()+e1.getLastName());
System.out.println("Salary of employee e1:"+e1.getMonthlySalary());
System.out.println("Name of employee e2: "+e2.getFirstName()+e2.getLastName());
System.out.println("Salary of employee e2 :"+e2.getMonthlySalary());
/* power(capabilities) of our class */
/* change e1 with any object whose property you want to change */
e1.setFirstName("Riley");
e1.setLastName("Raid");
e1.setMonthlySalary(524.32);
/*Yearly salary of each object = multiply monthly salary with 12 months */
System.out.println("Yearly salary of e1 :"+(e1.getMonthlySalary()*12));
System.out.println("Yearly salary of e2 :"+(e2.getMonthlySalary()*12));
/* give 10 % increment by calculating increment in variale increment */
double increment=0;
increment = e1.getMonthlySalary() + e1.getMonthlySalary()*10/100;
e1.setMonthlySalary(increment);
increment = e2.getMonthlySalary() + e2.getMonthlySalary()*10/100;
/*Yearly salary of each object = multiply monthly salary with 12 months */
System.out.println("Yearly salary after increment of e1 :"+(e1.getMonthlySalary()*12));
System.out.println("Yearly salary after increment of e2 :"+(e2.getMonthlySalary()*12));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.