Results 1 to 6 of 6
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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:

    Java 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    You write the method in the Employee class. Since the method and private variables are in the same class you have direct access to them. So just use them in your print statements.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You write the method in the Employee class. Since the method and private variables are in the same class you have direct access to them. So just use them in your print statements.
    If that is the case then what is this used for?

    Java Code:
    Employee(String fn, String ln, double ai, int ag)
    {
        firstName = fn;
        lastName = ln;
        annualIncome = ai;
        age = ag;
    }
    I thought the reason for defining these arguments was because the variables were private and so had to be called some how by using the fn, ln, ai, and ag.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    The parameters to a method or constructor have nothing to do with the accessibilty of the class variables. Other than if the variable is private then access can only be made via a setter.

    Java Code:
    class Foo {
        private int value = 100;
    
        public void doStuff(int divisor) {
            System.out.println(value / divisor);
        }
    }
    It makes no difference to the output of the method if the variable value is public, protected or private.

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Ok. Thank you. I'm going to have to chew on this a while. I appreciate your help.
    Last edited by jim01; 05-20-2011 at 06:36 AM. Reason: typo

  6. #6
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    I figured it out. Or at least it works anyway.

    Java Code:
    //create EmployeeExample class
    public class EmployeeExample
    {
         //Create main method
         public static void main(String [] args)
         {
     	//create constructor for employee 1
     	Employee emp1 = new Employee("George ", "Harrison ", 100000, 45);
    
     	//output for employee 1
     	emp1.display();
     	System.out.println("Net income: " + emp1.getNetIncome() + "\n");
    
            //create constructor for employee 2
     	Employee emp2 = new Employee("Ringo", "Starr", 110000, 46);
    
     	//output for employee 1
     	emp2.display();
    	System.out.println("Net income: " + emp2.getNetIncome());
         }
     }
     //create Employee class
     class Employee
     {
         //create variables
        private String firstName;
        private String lastName;
        private int age;
        private double annualIncome;
    
        //create constructor and arguments
        Employee(String fn, String ln, double ai, int ag)
        {
    	firstName = fn;
    	lastName = ln;
    	annualIncome = ai;
    	age = ag;
        }
        //create display method to print information
        void display()
        {
    	System.out.println("First name: " + firstName);
    	System.out.println("Last name: " + lastName);
    	System.out.println("age: " + age);
    	System.out.println("Gross income: " + annualIncome);
        }
        //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);
        }
     }
    Once again, thank you for the help.

Similar Threads

  1. Arraylist errors with private fields
    By DJRome in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 12:00 PM
  2. private data members?
    By blueduiker in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 11:13 AM
  3. java data fields parsing
    By sardare in forum Advanced Java
    Replies: 8
    Last Post: 10-02-2009, 12:17 PM
  4. simple question about private data
    By littleBean in forum New To Java
    Replies: 12
    Last Post: 07-02-2008, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •