Constructors and displaying data from private fields
I have to write a program with the following information:
Write a class Employee with the following specifications:
Fields:
private First name
private Last name
private Age
private Annual Income
Constructor:
Write a constructor which takes four parameters corresponding to the four fields and assigns them to the fields.
Methods:
A method which computes the net annual pay: subtract 7% medical, 3% insurance.
A method to display all information
Create a main class EmployeeExample which creates two employee objects. Use your own data.
I successfully created a method in which the annual net salary is computed and I can display that information for each employee. However, I haven't a clue on how to display the information found in the constructor, i.e, last name, first name, age, etc.
Here is my code:
Code:
//create EmployeeExample class
public class EmployeeExample
{
//Create main method
public static void main(String [] args)
{
//create constructors for employee1 and employee 2
Employee emp1 = new Employee("George", "Harrison", 100000, 45);
Employee emp2 = new Employee("Ringo", "Starr", 110000, 46);
//display output
System.out.println(emp1.getNetIncome());
System.out.println(emp2.getNetIncome());
}
}
//create Employee class
class Employee
{
//create variables
private String firstName;
private String lastName;
private double annualIncome;
private int age;
//create constructor and arguments
Employee(String fn, String ln, double ai, int ag)
{
firstName = fn;
lastName = ln;
annualIncome = ai;
age = ag;
}
//create method to get annual net income
double getNetIncome()
{
double medical = annualIncome * 0.07;
double insurance = annualIncome * 0.03;
double annualNetIncome = annualIncome - medical - insurance;
return (annualNetIncome);
}
}
I'm not sure what to do to display the last name, first name, etc. I tried putting emp1.firstName, et., into my System.out.println statement, but of course it doesn't recognize it since it is private and in another class. I also tried using emp1.ln but that didn't work either. I then attempted to add the variables into the getNetIncome() method and when that didn't work I tried using the constructor arguments in the getNetIncome() method but that didn't work either.
Please advise as I am out of ideas.