JAVA Language* Problem 1: Create an Employee class Create a class to represent E
ID: 3749655 • Letter: J
Question
JAVA Language*
Problem 1: Create an Employee class
Create a class to represent Employee information called Employee. This class includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (type double). Your class should have 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, set it to 0.0. Write a driver class named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display the yearly salary for each Employee. Then give each Employee a 10% raise and display each Employee’s yearly salary again.
Explanation / Answer
//Employee.java
public class Employee
{
//Declaring Instance Variables
private String first_Name;
private String last_Name;
private double sal_ary;
//Parameterized Constructor
public Employee(String firstName, StringlastName, double salary)
{
setFirstName(firstName);
setLastName(lastName);
setSalary(salary);
}
//Getter & Setter
public String getFirstName()
{
return first_Name;
}
public void setFirstName(String firstName)
{
first_Name =firstName;
}
public String getLastName()
{
return last_Name;
}
public void setLastName(String lastName)
{
last_Name =lastName;
}
public double getSalary()
{
return sal_ary;
}
public void setSalary(double salary)
{
if(salary < 0.0)
{
salary = 0.0;
}
sal_ary = salary;
}
}
//EmployeeTest.java
public class EmployeeTest
{
public static void main(String [] args)
{
//Creating two employee objects
Employee employee1 = newEmployee("Joe", "Blow", 10000.00);
Employee employee2 = newEmployee("Jane", "Doe", 15000.00);
System.out.println("Before 10% raise:");
System.out.println(employee1.getFirstName() + "'s salary is $" +employee1.getSalary() +".");
System.out.println(employee2.getFirstName() + "'s salary is $" +employee2.getSalary() +".");
employee1.setSalary(employee1.getSalary() * 1.1);
employee2.setSalary(employee2.getSalary() * 1.1);
System.out.println("After 10% raise:");
System.out.println(employee1.getFirstName() + "'s salary is $" +employee1.getSalary() +".");
System.out.println(employee2.getFirstName() + "'s salary is $" +employee2.getSalary() +".");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.