Create a class called Employee that includes three instance variables-a first na
ID: 3681007 • Letter: C
Question
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 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. Redo the exercise 3.14 such that you validate the Employee input (firstname, lastname, salary) Implement a Stack class data structure with methods : push(int i), pop(), top(), isempty(), displayinfo(), push(int i) - pushes an item on top of the stack pop()- pops/removes an item from the top of the stack top() - returns the value of the item on top of the stack isempty()- is true if stack is empty, is false otherwise displayinfo() - displays messages if Stack is full, or Stack is empty, or the value on top of the StackExplanation / Answer
(a)
Employee Test class:
// Create an Employee class
class Employee {
// that includes a first name (type String), a last name (type String)
// and a monthly salary (double) instance variable.
private String lastName, firstName;
private doublemonthlySalary;
// Provide a constructor that initializes the three instance variables.
public Employee( String lastName, String firstName, doublemonthlySalary ) {
this.lastName = lastName;
this.firstName = firstName;
// note, it doesn't say anything about the non positive rule yet
// but you appear to believe it's in effect
// this.monthlySalary = monthlySalary;
setMonthlySalary(monthlySalary);
}
//Provide a set and a get method for each instance variable.
public String getFirstName() { returnfirstName; }
public String getLastName() { returnlastName; }
public doublegetMonthlySalary() {return monthlySalary; }
public voidsetFirstName(String s) { firstName = s; }
public voidsetLastName(String s) { lastName = s; }
// If the monthly salary is not positive, do not set its value.
public voidsetMonthlySalary(double v) { if(v>0) { monthlySalary = v; } }
public void display() {
System.out.printf("Last Name: %s ", getLastName() );
System.out.printf("First Name: %s ", getFirstName() );
System.out.printf("Monthly Salary: %.2f ", getMonthlySalary() );
System.out.printf("Yearly Salary: %.2f ",12.0 * getMonthlySalary() );
System.out.println();
}
}
Test Application:
//Write a test application named EmployeeTest that demonstrates class Employee's capabilities.
public class EmployeeTest {
public static voidmain( String[] args ) {
// Create two Employee objects
Employee [] emps =new Employee[] {
new Employee("Nguyen", "Jean", 1111.11),
new Employee("Kaylor", "Odile", -1313.13 )
};
// display each object's yearly salary.
for(Employee emp : emps) { emp.display(); }
// Then give each Employee a 10% raise
for(Employee emp : emps) { emp.setMonthlySalary(emp.getMonthlySalary() * 1.1); }
// and display each Employee's yearly salary again.
for(Employee emp : emps) { emp.display(); }
}
}
// Create an Employee class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.