Employee Class Create a class called Employee that includes
(Employee Class) 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.
Solution
To run this application save both the files with the same class.
javaemployetest.java
public class Employee
{
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String name, String name2, double salary)
{
firstName = name;
lastName = name2;
monthlySalary = salary;
}
public void setFirstName(String name)
{
firstName = name;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String name)
{
lastName = name;
}
public String getLastName() {
return lastName;
}
public void setmonthlySalary(double salary)
{
monthlySalary = salary;
}
public double getmonthlySalary()
{
return monthlySalary;
}
public double yearlySalary()
{
double yearlySalary;
yearlySalary = (monthlySalary * 12);
return yearlySalary;
}
public double yearlySalaryIncrease()
{
double yearlySalaryIncrease;
yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary()));
return yearlySalaryIncrease;
}
public void displayYearlySalary()
{
System.out.printf(\"%s %s\'s Yearly Salary is $%.2f\ \", firstName, lastName,
yearlySalary());
}
public void displayYearlySalaryIncrease()
{
System.out.printf(\"%s %s = $%.2f\ \", firstName, lastName, yearlySalaryIncrease());
}
}
here is class employee test.java to test class employe.java
import java.util.Scanner;
public class EmployeeTest
{
public static void main(String[] args)
{
Employee employee1 = new Employee(\"first\", \"last\", 0.0);
Employee employee2 = new Employee(\"first\", \"last\", 0.0);
Scanner input = new Scanner(System.in);
String firstName;
String lastName;
double monthlySalary;
System.out.println(\"Enter details of employee1:\ \");
System.out.print(\"Enter First Name: \");
firstName = input.next();
employee1.setFirstName(firstName);
employee1.getFirstName();
System.out.print(\"Enter Last Name: \");
lastName = input.next();
employee1.setLastName(lastName);
employee1.getLastName();
System.out.print(\"Enter Monthly Salary: \");
monthlySalary = input.nextDouble();
if (monthlySalary > 0) //if monthly salary is not positive do not set its value
employee1.setmonthlySalary(monthlySalary);
employee1.getmonthlySalary();
System.out.println();
System.out.println(\"Enter details of employee2:\ \");
System.out.print(\"Enter First Name: \");
firstName = input.next();
employee2.setFirstName(firstName);
employee2.getFirstName();
System.out.print(\"Enter Last Name: \");
lastName = input.next();
employee2.setLastName(lastName);
employee2.getLastName();
System.out.print(\"Enter Monthly Salary: \");
monthlySalary = input.nextDouble();
if (monthlySalary > 0) employee2.setmonthlySalary(monthlySalary);
employee2.getmonthlySalary();
System.out.println();
employee1.yearlySalary();
employee2.yearlySalary();
employee1.displayYearlySalary();
System.out.println();
employee2.displayYearlySalary();
System.out.println();
employee1.yearlySalaryIncrease();
employee2.yearlySalaryIncrease();
System.out.printf(\"Congratulations to %s %s and %s %s. You just earned\"+ \" for yourselves a 10%c increase in your yearly salaries. \"+ \"\ Your new yearly salaries are:\ \ \", employee1.getFirstName(),
employee1.getLastName(), employee2.getFirstName(), employee2.getLastName(), \'%\');
employee1.displayYearlySalaryIncrease();
System.out.println();
employee2.displayYearlySalaryIncrease();
}
}
| To run this application save both the files with the same class. javaemployetest.java public class Employee { private String firstName; private String lastName; private double monthlySalary; public Employee(String name, String name2, double salary) { firstName = name; lastName = name2; monthlySalary = salary; } public void setFirstName(String name) { firstName = name; } public String getFirstName() { return firstName; } public void setLastName(String name) { lastName = name; } public String getLastName() { return lastName; } public void setmonthlySalary(double salary) { monthlySalary = salary; } public double getmonthlySalary() { return monthlySalary; } public double yearlySalary() { double yearlySalary; yearlySalary = (monthlySalary * 12); return yearlySalary; } public double yearlySalaryIncrease() { double yearlySalaryIncrease; yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary())); return yearlySalaryIncrease; } public void displayYearlySalary() { System.out.printf(\"%s %s\'s Yearly Salary is $%.2f\ \", firstName, lastName, yearlySalary()); } public void displayYearlySalaryIncrease() { System.out.printf(\"%s %s = $%.2f\ \", firstName, lastName, yearlySalaryIncrease()); } } |





