The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 3 Exercise 3.14.

Question: Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide 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, do not set its value. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case Employee.java and EmployeeTest.java). Then compile both classes and run EmployeeTest. Employee will not run because it does not have a main method.

To compile both classes at the same time using the command prompt, use the command

javac Employee.java EmployeeTest.java

To run EmployeeTest.java, use the command

java EmployeeTest.java

Below is Class EmployeeTest.java to test Class Employee.java

Click here to see other answers to Java How to Program.

10 Comments

  1. wow i think it's a good job, i get new lesson from this post and this post can be inspiring for all, thakns for your post

  2. give a java program to print name ,salary and calculate tax of an employee

  3. import java.util.ArrayList; import java.util.Scanner;

    public class Employee{

    public static void main(String[] args) {
    ArrayList obj = new ArrayList<>();
    String empFirstName;
    do {

    Scanner input = new Scanner(System.in);

    System.out.printf("Employee First Name :");
    empFirstName = input.nextLine();
    if (empFirstName.equals("johndoe")) {
    break;
    }

    System.out.printf(("Employee Last Name :"));
    String empLastName = input.nextLine();

    System.out.printf("Enter current salary :$");
    double empPayAmount = input.nextDouble();

    System.out.printf("Enter hourly rate :$");
    double hourRate = input.nextDouble();

    System.out.printf("Enter number of hours :");
    double numHours = input.nextDouble();
    Employee objEmp = new Employee();
    objEmp.setName(empFirstName, " " + empLastName);
    objEmp.addPay(empPayAmount);

    objEmp.addPay(numHours, hourRate);

    obj.add(objEmp);

    } while (true);
    for (Employee obEmployee : obj) {
    System.out.println("Total Pay for Employee " + obEmployee.getName() + " is " + obEmployee.getTotalPayAmount());
    }
    System.out.printf("Company employees are here : ", obj.size());

    }
    }

  4. write a code in java that input salary and grade of an employee and apply below conditions:
    i) in case of grade 15 or above than bonus is 15%
    ii) in case of grade 16 or above than bonus is 20%
    iii) in case of grade 18 or above than bonus is 25%
    iv) after calculating total salary deduct 13% GST in case that salary is 15000 or above. deduct 15% GST and in case that salary is 22000 or above.
    v) Add 6% bonus at the end
    Calculate net salary according to above condition and display it.

    Give me solution of this question I need emergency solution of this question. Thanks

  5. Can you please tell me what's the point of having the parameterized constructor when we never really use the parameters. Since we get our arguments from the user.

  6. thanks A lot , i just got a lot of ideas when i was studying your code
    you're the best
    Keep on :<3

  7. Very much thanks to you! Saved me a bit of time and I learned something by reading your code!

Write A Comment