Write a class based on the following UML diagram. Your class will be called “Emp
ID: 3693089 • Letter: W
Question
Write a class based on the following UML diagram. Your class will be called “Employee” and reside in a source file named “Employee.
The class Employee will be used to define or instantiate objects that contain the fields and the methods defined in the UML diagram above.
The “set” methods will allow data to be passed into an object. The “get” methods will allow data to be retrieved from an object.
The “input” methods will allow data to be entered (from the keyboard) by a user directly into the instant variables of an object. You may use the Scanner class to handle keyboard input.
The “inputLastname” method will return the boolean value true if a lastname was successfully entered and the value false if a blank was entered.
The “inputAll” method will invoke (call) all the input methds. It will return a boolean value based on the Boolean value returned by the “intputLastname” method. If no last name is entered, none of the other employee information will be prompted for.
The “equals” method will return true if both the last and first names of the current object are equal to the last and first names of the objects passed as an argument. If either the last or first names do not match, the method will return false.
The “toString” method will return a string containing the formatted output. It should look like:
LAST NAME : Murphy
FIRST NAME : Audie
EMPLOYEE ID : 57831
EMPLOYEE PHONE : 619-555-1212
EMPLOYEE SALARY : $40,000.00
BONUS : $2,000.00
MONTHLY PAY : $3,500.00
You may use the main method in the “EmployeeTest” class below to test your class. If you instantiate the Employee class into an object (e.g., person1), the main method is ignored.
public class EmployeeTest
{
public static void main(String[] args)
{
Employee person1 = new Employee();
person1.setLastname( "Murphy" );
person1.setFirstname( "Audie" );
person1.setId( "57831" );
person1.setPhone( "619-555-1212" );
person1.setYearlySalary( 40000 );
person1.calcBonus( 5 );
person1.calcMonthlyPay();
System.out.println( person1 );
System.out.println();
Employee person2 = new Employee();
if ( person2.inputAll())
{
person2.calcBonus( 2 );
person2.calcMonthlyPay();
System.out.println( person2 );
System.out.println( person1.equals( person2 ));
}
else
{
System.out.println( "No EMployee 2" );
}
}
}
SAMPLE OUTPUT:
Information Entered for 2nd Employee
LAST NAME : Murphy
FIRST NAME : Audie
EMPLOYEE ID : 57831
EMPLOYEE PHONE : 619-555-1212
EMPLOYEE SALARY : $40,000.00
BONUS : $2,000.00
MONTHLY PAY : $3,500.00
Enter New Employee Information
Enter Employee Last Name: York
Enter Employee First Name: Alvin
Enter Employee ID: 58943
Enter Employee Phone: 619-555-1234
Enter Employee Salary: 36000
LAST NAME : York
FIRST NAME : Alvin
EMPLOYEE ID : 58943
EMPLOYEE PHONE : 619-555-1234
EMPLOYEE SALARY : $36,000.00
BONUS : $720.00
MONTHLY PAY : $3,060.00
false
No Information Entered for 2nd Employee
LAST NAME : Murphy
FIRST NAME : Audie
EMPLOYEE ID : 57831
EMPLOYEE PHONE : 619-555-1212
EMPLOYEE SALARY : $40,000.00
BONUS : $2,000.00
MONTHLY PAY : $3,500.00
Enter New Employee Information
Enter Employee Last Name:
No Employee Information Entered
Information Entered for 2nd Employee
LAST NAME : Murphy
FIRST NAME : Audie
EMPLOYEE ID : 57831
EMPLOYEE PHONE : 619-555-1212
EMPLOYEE SALARY : $40,000.00
BONUS : $2,000.00
MONTHLY PAY : $3,500.00
Enter New Employee Information
Enter Employee Last Name: York
Enter Employee First Name: Alvin
Enter Employee ID: 58943
Enter Employee Phone: 619-555-1234
Enter Employee Salary: 36000
LAST NAME : York
FIRST NAME : Alvin
EMPLOYEE ID : 58943
EMPLOYEE PHONE : 619-555-1234
EMPLOYEE SALARY : $36,000.00
BONUS : $720.00
MONTHLY PAY : $3,060.00
false
Explanation / Answer
import java.util.*;
class Employee
{
private String lastname;
private String firstname;
private String id;
private String phone;
private double yearlySalary;
private double monthlyPay;
private double percent;
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getLastname()
{
return lastname;
}
public boolean inputLastName()
{
System.out.println("Enter Last Name");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
if(str == "")
return false;
else
{
setLastname(s.nextLine());
return true;
}
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getFirstname()
{
return firstname;
}
public void inputFirstName()
{
System.out.println("Enter First Name");
Scanner s = new Scanner(System.in);
setFirstname(s.nextLine());
}
public void setId(String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void inputId()
{
System.out.println("Enter the ID");
Scanner s = new Scanner(System.in);
setId(s.nextLine());
}
public void setPhone(String phone)
{
this.phone = phone;
}
public String getPhone()
{
return phone;
}
public void inputPhone()
{
System.out.println("Enter The Phone Number");
Scanner s = new Scanner(System.in);
setPhone(s.nextLine());
}
public void setYearlySalary(double yearlySalary)
{
this.yearlySalary = yearlySalary;
}
public double getYearlySalary()
{
return yearlySalary;
}
public void inputYearlySalary()
{
System.out.println("Enter The Yearly Salary");
Scanner s = new Scanner(System.in);
setYearlySalary(s.nextDouble());
}
public void calcBonus(double percent)
{
this.percent = percent;
}
public double calcMonthlyPay()
{
return (yearlySalary/12) + ((yearlySalary/12)*(percent/100));
}
public boolean equals(Employee e)
{
System.out.println(this.firstname);
if(this.firstname.equals(e.firstname) && this.lastname.equals(e.lastname))
return true;
else
return false;
}
public String toString()
{
String str = "";
str += " LAST NAME: " + lastname + " FIRST NAME: " + firstname + " EMPLOYEE ID: " + id + " EMPLOYEE PHONE: " + phone + " EMPLOYEE SALARY: " + getYearlySalary() + " BONUS: " + percent + "% MONTHLY PAY: " + calcMonthlyPay();
return str;
}
public boolean inputAll()
{
if(inputLastName())
{
inputFirstName();
inputId();
inputPhone();
inputYearlySalary();
}
return false;
}
}
public class EmployeeTest
{
public static void main(String[] args)
{
Employee person1 = new Employee();
person1.setLastname( "Murphy" );
person1.setFirstname( "Audie" );
person1.setId( "57831" );
person1.setPhone( "619-555-1212" );
person1.setYearlySalary( 40000 );
person1.calcBonus( 5 );
person1.calcMonthlyPay();
System.out.println( person1 );
System.out.println();
Employee person2 = new Employee();
if ( person2.inputAll())
{
person2.calcBonus( 2 );
person2.calcMonthlyPay();
System.out.println( person2 );
System.out.println( person1.equals( person2 ));
}
else
{
System.out.println( "No EMployee 2" );
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.