Post

Created by @nathanedwards
 at October 31st 2023, 10:18:43 pm.

Question:

Consider the following scenario:

A company wants to create a program to manage their employee records. They have decided to use variables and constants to store important information about each employee. The program should be able to store and retrieve the employee's name, age, employee ID, and job title. In addition, the program should be able to calculate and store the employee's salary based on their job title.

  1. Define and initialize the following variables and constants based on the requirements:

    • employeeName to store the employee's name
    • employeeAge to store the employee's age
    • employeeID to store the employee's ID
    • jobTitle to store the employee's job title
    • salary as a constant to store the employee's salary
  2. Write a code snippet that prompts the user to input values for the variables employeeName, employeeAge, employeeID, and jobTitle. Store the user inputs in the respective variables.

  3. Now, write a code snippet that calculates and assigns the appropriate salary based on the following criteria:

    • If the job title is "Manager", set the salary to $80,000.
    • If the job title is "Engineer", set the salary to $70,000.
    • If the job title is "Assistant", set the salary to $50,000.
    • For any other job title, set the salary to $40,000.

Assume that the necessary imports and declarations have already been done.

Provide your code solution below.

Note: Remember to include all necessary steps and explanations in your answer.

Answer:

  1. Define and initialize the following variables and constants based on the requirements:
String employeeName = "";
int employeeAge = 0;
int employeeID = 0;
String jobTitle = "";
final int salary;

In the code above, employeeName, employeeAge, employeeID, and jobTitle are variables to store the employee's information. salary is declared as a final constant as it needs to be assigned based on job title but will remain constant once assigned.

  1. Write a code snippet that prompts the user to input values for the variables employeeName, employeeAge, employeeID, and jobTitle. Store the user inputs in the respective variables.
Scanner scanner = new Scanner(System.in);

System.out.print("Enter employee name: ");
employeeName = scanner.nextLine();

System.out.print("Enter employee age: ");
employeeAge = scanner.nextInt();

System.out.print("Enter employee ID: ");
employeeID = scanner.nextInt();

System.out.print("Enter job title: ");
jobTitle = scanner.nextLine();

In the code above, a Scanner is used to receive user input. The user is prompted to enter values for employeeName, employeeAge, employeeID, and jobTitle. The Scanner.nextLine() method is used after nextInt() to consume the remaining newline character left in the buffer.

  1. Now, write a code snippet that calculates and assigns the appropriate salary based on the following criteria:
switch (jobTitle) {
    case "Manager":
        salary = 80000;
        break;
    case "Engineer":
        salary = 70000;
        break;
    case "Assistant":
        salary = 50000;
        break;
    default:
        salary = 40000;
        break;
}

In the code above, a switch statement is used to determine the job title and assign the appropriate salary value to the salary variable. If the job title matches "Manager", the salary is set to $80,000. If the job title matches "Engineer", the salary is set to $70,000. If the job title matches "Assistant", the salary is set to $50,000. For any other job title, the salary is set to $40,000.

The reason for using a switch statement is to handle different job titles easily and ensure that the correct salary is assigned based on the job title entered by the user.

Overall, this program allows the company to manage employee records by storing their information using variables and constants, and calculates the salary based on the job title using a decision-making statement.