Files to Submit: Employee.java TestEmployee2.java Constructor Methods Create a c
ID: 3719270 • Letter: F
Question
Files to Submit:
Employee.java
TestEmployee2.java
Constructor Methods
Create a constructor for the Employee class that defines two parameters: one for the number and one for the name. Set department, position to default values – null for strings, and set salary, rank also to default values – 0 for numeric values.
Create a constructor for the Employee class that defines six parameters for all six fields and sets them to these values.
Methods
In your Employee class, create a method called checkBonus(). This method will return true or false depending on whether the employee gets a bonus. The bonus is determined by their rank. If their rank is above 5, they get yearly bonus of $1,000. This method will return true if rank is above 5 and false otherwise.
This method takes no parameters and returns a boolean value.
In your Employee class, create a method called displayEmployee() that prints the employee’s information to standard output. It should print the employee’s number (to 9 digits with leadings 0s filled in), name, department, position, salary and the employee’s rank. It should call the method checkBonus() to see if the employee gets a bonus and print the amount of the bonus if the employee will receive it. If the employee does not get a bonus, nothing is printed.
See the screen shot below for an example of output. Use printf for formatting.
This method is in addition to printSalary(), which only printed the salary for testing.
TestEmployee2
The program TestEmployee2 is provided to begin testing your class. This class defines an Employee object that calls a constructor method with values for name and number. The department, position, salary and rank are added using the set methods. Finally the employee is printed out.
The end of the main method contains two commented out lines of code. The first is a call to a method you will write that returns an Employee object created with values from the contents of a file. The second line of code is that employee printed out.
Create a method in TestEmployee2 called createEmployeeFromFile() that will read data from a file and create, populate and return an Employee object. The file it will read from is called employee1.txt, which is provided. Hard code the name of the file in the method. This file contains the employee’s number, name, department, position, salary and rank. Create a Scanner object and use the Scanner class’s methods to read the data in the file and use this data to create the Employee object. Finally return the employee object. An Employee object will be created in this method using the constructor method that takes as parameters all six fields. This employee object will be returned from this method. This method will define no parameters. The test file, employee1.txt is provided for you to test your program. I will test your program with a file that contains different values but the same format.
When you have completed this method, uncomment out the two lines of code to test the method.
Documentation
Continue documenting all the methods in the Employee class.
TestEmployee2.java
employee1.txt
45
Elaine Benes
Editorial
Associate Editor
75000
6
Explanation / Answer
here is your code :------------>>>>>>>>>.
public class Employee{
private int id;
private String name;
private double salary;
private String department;
private String position;
private int rank;
public Employee(int id,String name){
this.id = id;
this.name = name;
salary = 0.0;
department = null;
position = null;
rank = 0;
}
public Employee(){
}
public Employee(int id,String name,String dep,String pos,double sal,int rank){
this.id = id;
this.name = name;
salary = sal;
department = dep;
position = pos;
this.rank = rank;
}
public void setSalary(double salary){
this.salary = salary;
}
public void setDepartment(String department){
this.department = new String(department);
}
public void setPosition(String position){
this.position = new String(position);
}
public void setRank(int r){
rank = r;
}
public int getID(){
return id;
}
public String getName(){
return name;
}
public Double getSalary(){
return salary;
}
public String getDepartment(){
return department;
}
public String getPosition(){
return position;
}
public int getRank(){
return rank;
}
public boolean checkBonus(){
if(rank >= 5){
return true;
}
return false;
}
public void displayEmployee(){
if(checkBonus()){
System.out.println("Number : "+id+" Name : "+name+" Department : "+department+" Position : "+position);
System.out.printf("Salary : %09.1lf",salary);
System.out.println("Rank : "+rank+" Bonus : $1,000 per year");
}else{
System.out.println("Number : "+id+" Name : "+name+" Department : "+department+" Position : "+position);
System.out.printf("Salary : %09.1lf",salary);
System.out.println("Rank : "+rank);
}
}
}
import java.util.*;
import java.io.*;
public class TestEmployee2{
public static Employee createEmployeeFromFile(){
try{
Scanner sc = new Scanner(new File("employee1.txt"));
Employee e1 = new Employee(Integer.parseInt(sc.nextLine()),sc.nextLine(),sc.nextLine(),sc.nextLine(),Double.parseDouble(sc.nextLine()),Integer.parseInt(sc.nextLine()));
sc.close();
return e1;
}catch(Exception e){
System.out.println("File Opening Error !!! ");
System.exit(0);
}
return null;
}
public static void main(String[] args) throws IOException
{
Employee e1 = new Employee(7,"George Costanza");
e1.setDepartment("Front Office");
e1.setPosition("Assistant to the Traveling Secretary");
e1.setSalary(50000.0);
e1.setRank(2);
e1.displayEmployee();
Employee e2 = createEmployeeFromFile();
e2.displayEmployee();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.