Please get the following code to compile AND run: package hvcc; import java.text
ID: 3539685 • Letter: P
Question
Please get the following code to compile AND run:
package hvcc;
import java.text.NumberFormat;
public class Employee {
protected String name;
protected int age;
protected String department;
protected double salary;
public Employee() {
name = "";
department = "";
age = 0;
salary=0.0;
}
public Employee(String name, int age, String department, double
salary){
this.name = name;
this.age = age;
this.department = department;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public boolean equals(Employee otherEmployee) {
return ( ( age == otherEmployee.age) && ( salary == otherEmployee.salary)
&&
( name.compareToIgnoreCase(otherEmployee.name) == 0 ) &&
( department.compareToIgnoreCase(otherEmployee.department)
== 0 ) );
}
public void makeCopy(Employee otherEmployee) {
name = otherEmployee.name;
age = otherEmployee.age;
department = otherEmployee.department;
salary = otherEmployee.salary;
}
public Employee getCopy()
{
Employee copyResult = new Employee(name, age, department, salary);
return copyResult;
}
public String toString() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return "Name: " + name + ". Age: " + age + ". Department: " +
department + ". Salary: " + formatter.format(salary) ;
}
}
Explanation / Answer
Create Driver Class in the same package
......................................................................
public class Driver {
public static void main(String[] args){
//Creating Employee 1 E1 object//
Employee E1 =new Employee( "Mohan", 25 , "Finance", 245689.23);
E1.setSalary(245689.23);
//Employee E1 Info//
System.out.println(E1.toString());
//Creating Employee 2 E3 object//
Employee E2 =new Employee( "Ravi", 25 , "Accounts", 210578.23);
E2.setSalary(210578.23);
//Employee E2 Info//
System.out.println(E2.toString());
boolean x;
//to compare whether E1 and E2 are same or not
//Prints true if both are same otherwise false
x = E1.equals(E2);
System.out.println(x);
//Copying Employee E1 details to Employee E3//
Employee E3 = new Employee();
E3.makeCopy(E1);
E3 = E3.getCopy();
//Employee E3 Info//
System.out.println(E3.toString());
}
}
...........................................................................................................................
Output Execution 1
Name: Mohan. Age: 25. Department: Finance. Salary: $245,689.23
Name: Ravi. Age: 25. Department: Accounts. Salary: $210,578.23
false
Name: Mohan. Age: 25. Department: Finance. Salary: $245,689.23
Output Execution 2
Name: Mohan. Age: 25. Department: Finance. Salary: $245,689.23
Name: Mohan. Age: 25. Department: Finance. Salary: $245,689.23
true
Name: Mohan. Age: 25. Department: Finance. Salary: $245,689.23