Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a class Employee and test it in your Main.java. An employee has a name

ID: 3574891 • Letter: I

Question

Implement a class Employee and test it in your Main.java. An employee has a name (a String) and a salary (a double). Provide a constructor with two explicit arguments.

public Employee(String employeeName, double currentSalary) - Constructor.

and methods

public String getName() - Returns the name of the employee.

public double getSalary() - Returns the current salary.

public void raiseSalary(double byPercent) - Gives the employee a raise according to the explicit argument byPercent.

Sample Usage:

Employee harry = new Employee("Hacker, Harry", 10000);

harry.raiseSalary(10); // Harry gets a 10% raise.

Getting Started

We are going to do this exercise by writing the object that solves the problem first (in a source file called Employee.java) and then testing it using code we write into Main.java. Using the techniques shown on the web page titled How to Start Every Project in this Class create a source file called Employee.java as well as a file called Main.java.

Open up the Employee.java file and replace the code with the code contained in the box below:

/**

* This class implements an emplyee which is a person with a name and a salary.

*

*/

public class Employee {

/**

   * Constructor that creates a new Employee with an initial name and salary.

   *

   */

public Employee(String employeeName, double currentSalary) {

}

// Accessors that are obvious and have no side effects don't have to have

// any documentation unless you are creating a library to be used by other

// people.

public String getName() {

}

public double getSalary() {

}

/**

   * Raise the salary by the amount specified by the explicit argument.

   *

   */

public void raiseSalary(double byPercent) {

}

}

You'll notice that there is only a skeleton of code. It current doesn't do anything. You will have to add instance variables and code to get this class defined properly. Read the comments (and the problem) to understand what the problem is and how you will solve it.

Don't forget to through the code and replace every instance of [CHANGE THIS TO YOUR INFORMATION] to the appropriate item. Be sure that the square brackets are included when you replace the text.

Next, using the same technique you used to create the Employee.java file to create another file called Main.java. This is where your test code will go. Replace the code in that file with the code in the grey box below:

/**

* This class tests the Employee object.

*

*/

public class Main {

/**

   * Create an employee and test that the proper name has been created. Test

   * the initial salary amount and then give the employee a raise. Then check

   * to make sure the salary matches the raised salary.

   *

   *

   * @param args

   * command line values. Not used in this example.

   */

public static void main(String[] args) {

}

}

Explanation / Answer

Main.java


public class Main {

   public static void main(String[] args) {
       Employee harry = new Employee("Hacker, Harry", 10000);
         
       harry.raiseSalary(10); // Harry gets a 10% raise.
       System.out.println(harry.getSalary());
   }

}

Employee.java


public class Employee {
     
   private String name;
   private double salary;
/**
* Constructor that creates a new Employee with an initial name and salary.
*
*/
public Employee(String employeeName, double currentSalary) {
name = employeeName;
salary = currentSalary;

}

// Accessors that are obvious and have no side effects don't have to have
// any documentation unless you are creating a library to be used by other
// people.
public String getName() {
   return name;
}

public double getSalary() {
   return salary;
}

/**
* Raise the salary by the amount specified by the explicit argument.
*
*/
public void raiseSalary(double byPercent) {
   salary = (salary + salary * 10)/100;
}
}

Output:

1100.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote