Thursday, September 3, 2015

// // 5 comments

Program To Calculate And Display Employee Salary

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

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 3.14 - Employee Class
 * This Program Calculates The Yearly Salary Of Employees
 *
 */ 

public class Employee {
    
    private String firstName; 
    private String lastName; 
    private double monthlySalary;

    public Employee(String name, String name2, double salary) {
        firstName = name;
        lastName = name2;
        monthlySalary = salary;
    }

    public void setFirstName(String name) {
        firstName = name;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String name) {
        lastName = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setmonthlySalary(double salary) {
        monthlySalary = salary;
    }

    public double getmonthlySalary() {
        return monthlySalary;
    }

    public double yearlySalary() {
        double yearlySalary;
        yearlySalary = (monthlySalary * 12);
        return yearlySalary;
    }

    public double yearlySalaryIncrease() {
        double yearlySalaryIncrease;
        yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary()));
        return yearlySalaryIncrease;
    }

    public void displayYearlySalary() {
        System.out.printf("%s %s's Yearly Salary is $%.2f\n", firstName, lastName,
           yearlySalary());
    }

    public void displayYearlySalaryIncrease() {
        System.out.printf("%s %s = $%.2f\n", firstName, lastName, yearlySalaryIncrease());
    }

}

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

//Exercise 3.14 - Employee Class Test
//This Program Demonstrates Class Employee Capabilities

import java.util.Scanner;

public class EmployeeTest {
    public static void main(String[] args) {
        
        Employee employee1 = new Employee("first", "last", 0.0);
        Employee employee2 = new Employee("first", "last", 0.0);

	Scanner input = new Scanner(System.in);
        
        String firstName;
        String lastName;
        double monthlySalary;

        System.out.println("Enter details of employee1:\n");
        
        System.out.print("Enter First Name: ");
        firstName = input.next();
        employee1.setFirstName(firstName);
        employee1.getFirstName();
        System.out.print("Enter Last Name: ");
        lastName = input.next();
        employee1.setLastName(lastName);
        employee1.getLastName();
        System.out.print("Enter Monthly Salary: ");
        monthlySalary = input.nextDouble();
        if (monthlySalary > 0) //if monthly salary is not positive do not set its value
            employee1.setmonthlySalary(monthlySalary);
        employee1.getmonthlySalary();

        System.out.println();

        System.out.println("Enter details of employee2:\n");

        System.out.print("Enter First Name: ");
        firstName = input.next();
        employee2.setFirstName(firstName);
        employee2.getFirstName();
        System.out.print("Enter Last Name: ");
        lastName = input.next();
        employee2.setLastName(lastName);
        employee2.getLastName();
        System.out.print("Enter Monthly Salary: ");
        monthlySalary = input.nextDouble();
        if (monthlySalary > 0) //if monthly salary is not positive do not set its value
            employee2.setmonthlySalary(monthlySalary);
        employee2.getmonthlySalary();

        System.out.println();

        employee1.yearlySalary();
        employee2.yearlySalary();

        employee1.displayYearlySalary();

        System.out.println();

        employee2.displayYearlySalary();

        System.out.println();

        employee1.yearlySalaryIncrease();
        employee2.yearlySalaryIncrease();

        System.out.printf("Congratulations to %s %s and %s %s. You just earned"
                + " for yourselves a 10%c increase in your yearly salaries. "
                + "\nYour new yearly salaries are:\n\n", employee1.getFirstName(),
                employee1.getLastName(), employee2.getFirstName(), 
                employee2.getLastName(), '%');

        employee1.displayYearlySalaryIncrease();

        System.out.println();

        employee2.displayYearlySalaryIncrease();

    }
}


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

5 comments:

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

    ReplyDelete
    Replies
    1. I'm glad you learnt something from the code. You are welcome.

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

    ReplyDelete
    Replies
    1. Thanks 5faya, you are welcome. I'm glad you got a lot of ideas studying my code. If you don't mind, please share it with us.

      Delete
  3. 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.

    ReplyDelete


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.