Create a class called Employee that includes three pieces of information as inst
ID: 3871138 • Letter: C
Question
Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (type double). Your class should have 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, set it to 0.0.
Write a test application named EmployeeTest that demonstrates class Employee’s capabilities through creating two Employee objects and displaying first name, last name and the yearly salary for each Employee. Assume that a manager uses this application to view and modify employees’ information. Include functionalities that ask the manger whether she wants to modify any employee information and if so which one, then apply the requested modifications (first name, last name, and/or salary) to the employee information and display the updated employees information one more time.
Explanation / Answer
Employee.java
public class Employee {
//Declaring instance variables
private String firstname;
private String lastname;
private double monthlysal;
//Parameterized constructor
public Employee(String firstname, String lastname, double monthlysal) {
super();
this.firstname = firstname;
this.lastname = lastname;
setMonthlysal(monthlysal);
}
//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 getMonthlysal() {
return monthlysal;
}
public void setMonthlysal(double monthlysal) {
if (monthlysal < 0)
this.monthlysal = 0.0;
else
this.monthlysal = monthlysal;
}
}
_______________________
EmployeeTest.java
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args) {
//Declaring variables
String fname, lname;
double sal;
/* Creating an Scanner class object which is used
* to get the inputs entered by the user
*/
Scanner sc = new Scanner(System.in);
//Creating Two Employee objects by passing data as arguments
Employee e1 = new Employee("Johnson", "Mek", 50000);
Employee e2 = new Employee("Pointing", "Ricky", 40000);
//Displaying the Employee#1 info
System.out.println(":: Employee#1 Info::");
System.out.println("First name :" + e1.getFirstname());
System.out.println("Last name :" + e1.getLastname());
System.out.println("Yearly salary :" + e1.getMonthlysal() * 12);
//Displaying the Employee#2 info
System.out.println(" :: Employee#2 Info::");
System.out.println("First name :" + e2.getFirstname());
System.out.println("Last name :" + e2.getLastname());
System.out.println("Yearly salary :" + e2.getMonthlysal() * 12);
//Prompting the user whether he want to modify Employee object info
//Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to Modify Employee Object(Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y') {
while (true) {
System.out.print("Which Employee Info you want to modify :");
int e = sc.nextInt();
if (e == 1) {
//Getting the inputs entered by the user
System.out.print("Enter firstname :");
fname = sc.next();
System.out.print("Enter lastname :");
lname = sc.next();
System.out.print("Enter monthly salary :");
sal = sc.nextDouble();
//Modifying the Employee info by using Setter methods
e1.setFirstname(fname);
e1.setLastname(lname);
e1.setMonthlysal(sal);
//Displaying Employee info after modifying
System.out.println(":: After Modifying Employee#1 Info::");
System.out.println("First name :" + e1.getFirstname());
System.out.println("Last name :" + e1.getLastname());
System.out.println("Yearly salary :" + e1.getMonthlysal() * 12);
break;
} else if (e == 2) {
//Getting the inputs entered by the user
System.out.print("Enter firstname :");
fname = sc.next();
System.out.print("Enter lastname :");
lname = sc.next();
System.out.print("Enter monthly salary :");
sal = sc.nextDouble();
//Modifying the Employee info by using Setter methods
e2.setFirstname(fname);
e2.setLastname(lname);
e2.setMonthlysal(sal);
//Displaying Employee info after modifying
System.out.println(" ::After Modifying Employee#2 Info::");
System.out.println("First name :" + e2.getFirstname());
System.out.println("Last name :" + e2.getLastname());
System.out.println("Yearly salary :" + e2.getMonthlysal() * 12);
break;
} else {
System.out.println("** Invalid Employee **");
continue;
}
}
} else {
System.out.println(":: Program Exit ::");
}
}
}
______________________
Output:
:: Employee#1 Info::
First name :Johnson
Last name :Mek
Yearly salary :600000.0
:: Employee#2 Info::
First name :Pointing
Last name :Ricky
Yearly salary :480000.0
Do you want to Modify Employee Object(Y/N) ::Y
Which Employee Info you want to modify :1
Enter firstname :Sachin
Enter lastname :Tendulkar
Enter monthly salary :67000
:: After Modifying Employee#1 Info::
First name :Sachin
Last name :Tendulkar
Yearly salary :804000.0
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.