Create a class called Employee that includes three instance variables-a first na
ID: 3929282 • 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 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
import java.util.*;
import java.lang.*;
import java.io.*;
class EmployeeTest {
public static void main(String[] args) {
Employee[] emparr = new Employee[2];
emparr[0] = new Employee("Tom", "Cruise", 75000);
emparr[1] = new Employee("Lilly","Brown", 50000);
for (Employee e : emparr)
e.raiseSalary(10);
for (Employee e : emparr)
System.out.println("After Bonus, Name= " + e.getFirstName() + " "+ e.getLastName() + ",salary= " + e.getSalary() );
}
}
class Employee {
private String firstName;
private String lastName;
private double salary;
public Employee( String first, String last, double sal )
{
firstName = first;
lastName = last;
if(sal>=0) salary=sal;
System.out.println( "Initial Values: " +
firstName + " " + lastName + " Salary "+salary);
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getSalary()
{
return salary;
}
public void raiseSalary(double val) {
double raise = salary * val / 100;
salary += raise;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.