Each question is independent of each other. I want you to do it without copying
ID: 3747499 • Letter: E
Question
Each question is independent of each other. I want you to do it without copying from other's work. Do not forget to add comments to explain how your codes are working! Comments must be brief, clear, and understandable. Do not write long sentences!!!
Create a class called Employee that includes three instance variables – a first name (String), a last name (String), and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each employee’s name, last name, and yearly salary. Then, give each Employee a 10% raise and display each employee’s name, last name, and yearly salary again. UPLOAD Employee.java and EmployeeTest.java
Make sure the work should be done by your own, no copied work.
Explanation / Answer
Solution:
##########Employee.java
public class Employee {
//Declaring instance variables
String firstName;
String lastName;
double salary;
//Parameterized constructor to set the fields values respectively
public Employee(String firstName, String lastName, double salary) {
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
//Getters and Setters
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
//Checking if the salary is a positive numbe ror not
if(salary > 0)
this.salary = salary;
else
System.out.println("Invalid Salary!");
}
//Method to raise the salary by some percentage
public void raiseSalary(double percent) {
this.salary *= (1+percent/100);
}
}
###########EmployeeTest.java
public class EmployeeTest {
public static void main(String[] args) {
//Creating 2 employees objects.
Employee employee1 = new Employee("Adam", "Li", 2000.50);
Employee employee2 = new Employee("John", "Cena", 3000.10);
employee1.raiseSalary(10); //Raising employee1 salary by 10%.
employee2.raiseSalary(10); //Raising employee2 salary by 10%.
//Displaying employee1 Salary
System.out.println("Employee1 salary is "+employee1.salary);
//Displaying employee2 salary
System.out.println("Employee2 salary is "+employee2.salary);
}
}
Sample Run:
Employee1 salary is 2200.55
Employee2 salary is 3300.11
Note: If you have any doubt please do comment below, if you liked the solution then do hit the thumbs up button.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.