Create an Employee class that includes a first name (type String), a last name (
ID: 3641993 • Letter: C
Question
Create an Employee class that includes a first name (type String), a lastname (type String) and a monthly salary (double) instance variable. 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 application 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
public class Employee {
private String first;
private String last;
private double salary;
public Employee(String first, String last, double salary) {
this.first = first;
this.last = last;
if(salary > 0) {
this.salary = salary;
}
else {
salary = 0;
}
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary > 0) {
this.salary = salary;
}
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee>new Employee("John","Smith",5000);
Employee two = new Employee("Joe","Schmoe",1250);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
one.setSalary(one.getSalary() * 1.1);
two.setSalary(two.getSalary() * 1.1);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.