Organizing Classes public class Employee private int empNum private String empLa
ID: 3711609 • Letter: O
Question
Organizing Classes public class Employee private int empNum private String empLastName; private String empFirstName; private double empSalary; public int getEmpNumO 131 return empNum public void setEmpNum (int emp) empNum-emp public String getEmpLastName O return empLastName; public void setEmpLastName (String name) empLastName name; public String getEmpFirstNameO return empFirstName public void setEmpFirstName(String name) empFirstNamename; public double getEmpSalaryO return empSalary; public void setEmpSalary(double sal) empSalarysal; Figure 3-22 The Employee class with several data fields and corresponding methodsExplanation / Answer
Solution
As we know that in a java apllication we can include as many
as java(.java extension files) necessary , with each file
adding a functionality to the application . We have to store
the first class Employee on a separate file .
Now considering the following code ,
// Class to store the details of an employee
public class Employee // Defines a class with the name Employee
{
private int empNum; // A private variable empNum of integer type
private String empLastName; // A private variable empLastName of String type
private String empFirstName; // A private variable empFirstName of String type
private double empSalary; // A private variable empSalary of double(float) type
// The private vaariables are accessible only inside the class and its methods
public void getEmpNum() // method to retrieve the employee number
{
return empNum;
}
public void setEmpNum(int emp) // method to set the employee number
{
empNum=emp;
}
public String getEmpLastName() // method to retrieve the employee last name
{
return empLastName;
}
public void setEmpLastName(String name) // method to set the employee last name
{
empLastName=name;
}
public String getEmpFirstName() // method to retrieve the employee fisrt name
{
return empFirstName;
}
public void setEmpFirstName(String name) // method to set the employee first name
{
empFirstName=name;
}
public double getEmpSalary() // method to retrieve the employee salary
{
return empSalary;
}
public void setEmpSalary(double sal) // method to set the employee salary
{
empSalary=sal;
}
// These are all the setters and getters for each of the attributes
// All the setters get their respective as a paramter to the function
}
The main file , here we make objects of type Employee ,i.e, instantiate
a type of Employee object to store values .
// MethodsThatUseAnEmployee class
import java.util.Scanner; // A library that supports a class which allows to read
// input from user
// This class is used to check all methods defined in employee class
class MethodsThatUseAnEmployee
{
public static void main(String args[]) // main is where the execution of the program starts
{
Employee myEmployee; // Make an objectof Employee type.
myEmployee = getEmployeeData(); // getEmployeeData will return an object of Employee type
// that is assigned to myEmployee.
displayEmployee(myEmployee); // The legal initial values are displayed in this method
// by passing an object as parameter .
}
public static Employee getEmployeeData() // Method to read data from user and store in a temporary object that // is returned to main .
{
Employee tempEmp = new Employee(); // Create an object of employee type
int id; // Variable to store id read from user
double sal;
Scanner input = new Scanner(System.in); // Create input object that can read through standard system input console.
System.out.print("Enter Employee Id "); // Print a message to give input
id = input.nextInt(); // Read the next integer that is typed as input
tempEmp.setEmpNum(id); // Assign the input id read as the objects Employee number through setter function .
System.out.print("Enter Employee salary "); // Repeat the same for salary
sal = input.nextDouble();
tempEmp.setEmpSalary(sal);
return tempEmp; // The temporary object with all the input values read from user is returned to main .
// Which is assigned to another object for further use .
}
public static void displayEmployee(Employee anEmp) // Method to display data contained in object passed as a // parameter
{
System.out.println(" Employee #" + anEmp.getEmpNum() + // The getter function retrieves the data of the passed // object .
" Salary is " + anEmp.getEmpSalary()); // And displays the data on standard output .
}
}
Feel free to reach out regarding any queries . And please do rate the answer . Thank you .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.