Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default How to use fields from other methods?

    I am writing a program that has the following methods:

    Java Code:
    class Employee
    {
        //declare variables
        private String name;
        private int id;
        private double salary;
        private int age;
        private String position;
        public static double ratePercent;
    
        //create constructor with four arguments
        Employee(String nm, int empid, double sa, int ag)
        {
    	name = nm;
    	id = empid;
    	salary = sa;
    	age = ag;
        }
        //create constructor with no arguments
        Employee(){}
        //create getFedTax() method
        double getFedTax()
        {
    	double fedTax = (salary - 800) * 0.17;
    	return ((int)fedTax);
        }
        //create getSsTax(rate) method
        double getSsTax(int rate)
        {
    	ratePercent = rate / 100;
    	double ssTax = salary * ratePercent;
    	return ((int)ssTax);
        }
        double getHealthFee(int rate)
        {
    	ratePercent = rate / 100;
    	double empContribution = salary * ratePercent;
    	return ((int)empContribution);
        }
        double getInsurance()
        {
    	double rate = 0;
    	if (age < 40) rate = salary * 0.03;
    	else if (age >= 40 && age < 50) rate = salary * 0.04;
    	else if (age >=50 && age <=60) rate = salary * 0.05;
    	else if (age > 60) rate = salary * 0.06;
    	double empInsContribution = salary - rate;
    	return ((int)empInsContribution);
        }
    }
    I need to add one more method, called getNetPay()
    which returns the net pay after deducting the amounts found in the previous methods. The problem is that I can't figure out how to do this. I tried this:

    Java Code:
    double getNetPay()
    {
        double deductions = ssTax + empContribution + fedTax + empInsContribution;
        double netPay = salary - deductions;
        return (netPay);
    }
    but it didn't work. I believe that it didn't work because the variables I attempted to use are only recognized inside of the methods they were created in. That being the case, how do pull that information out of the methods they are found in so that I can input them into the getNetPay method?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're close, but you don't use the fields, you use the methods that are available to you. So in your getNetPay method, call the getSsTax(), getInsurance(), getHealthFee(...) (you'll need to know the rate for this), and getFedTax() methods instead of trying to use fields that don't exist.

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

    Default

    Quote Originally Posted by Fubarable View Post
    You're close, but you don't use the fields, you use the methods that are available to you. So in your getNetPay method, call the getSsTax(), getInsurance(), getHealthFee(...) (you'll need to know the rate for this), and getFedTax() methods instead of trying to use fields that don't exist.
    Ok. Like this then:

    Java Code:
    double getNetPay()
    {
        double deductions = getSsTax(7) + getHealthFee(3) + getFedTax() +  getInsurance();
        double netPay = salary - deductions;
        return (netPay);
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way to test it: run it and see if the numbers make sense when you compare the results obtained by doing the calculations on a spread sheet.

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

    Default

    Quote Originally Posted by Fubarable View Post
    One way to test it: run it and see if the numbers make sense when you compare the results obtained by doing the calculations on a spread sheet.
    Well it compiles, and the methods with no parameters work fine but the results for the methods with the inputted rates do not work. I receive 0 for the getSsTax(int rate) and getHealthFee(int rate) methods. I thought that adding the rates into the getNetPay() method would do it but it's not. Therefore I need to find someway to initialize initialize getSsTax(int rate). I tried replacing (int rate) with int 7 and then with 7, but neither work.

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

    Default

    Quote Originally Posted by jim01 View Post
    Well it compiles, and the methods with no parameters work fine but the results for the methods with the inputted rates do not work. I receive 0 for the getSsTax(int rate) and getHealthFee(int rate) methods. I thought that adding the rates into the getNetPay() method would do it but it's not. Therefore I need to find someway to initialize initialize getSsTax(int rate). I tried replacing (int rate) with int 7 and then with 7, but neither work.
    Something else I tried was to add a rate variable to the getSsTax(int rate) and getHealthFee(int rate) methods

    Java Code:
    double getSsTax(int rate)
    {
        rate = 7;
        ratePercent = rate / 100;
        double ssTax = salary * ratePercent;
        return ((int)ssTax);
    }
    //create getHealthFee(int rate) method
    double getHealthFee(int rate)
    {
        rate = 3;
        ratePercent = rate / 100;
        double empContribution = salary * ratePercent;
        return ((int)empContribution);
    }
    This didn't change anything though. I still get 0.

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

    Default

    Here is the whole program:

    Java Code:
    //create ProgrammingProblem2 class
    class ProgrammingProblem2
     {
         //create main method
         public static void main(String [] args)
         {
     	//invoke constructor
     	Employee emp1 = new Employee("George Harrison", 12345, 100000, 45);
     	emp1.displayInfo();
         }
    }
    //create Employee class
    class Employee
    {
        //declare variables
        private String name;
        private int id;
        private double salary;
        private int age;
        private String position;
        public static double ratePercent;
    
        //create constructor with four arguments
        Employee(String nm, int empid, double sa, int ag)
        {
    	name = nm;
    	id = empid;
    	salary = sa;
    	age = ag;
        }
        //create construcotr with no arguments
        Employee(){}
        //create getFedTax() method
        double getFedTax()
        {
    	double fedTax = (salary - 800) * 0.17;
    	return ((int)fedTax);
        }
        //create getSsTax(rate) method
        double getSsTax(int rate)
        {
    	rate = 7;
    	ratePercent = rate / 100;
    	double ssTax = salary * ratePercent;
    	return ((int)ssTax);
        }
        //create getHealthFee(int rate) method
        double getHealthFee(int rate)
        {
    	rate = 3;
    	ratePercent = rate / 100;
    	double empContribution = salary * ratePercent;
    	return ((int)empContribution);
        }
        //create getInsurance()method
        double getInsurance()
        {
    	double rate = 0;
    	if (age < 40) rate = salary * 0.03;
    	else if (age >= 40 && age < 50) rate = salary * 0.04;
    	else if (age >=50 && age <=60) rate = salary * 0.05;
    	else if (age > 60) rate = salary * 0.06;
    	double empInsContribution = salary - rate;
    	return ((int)empInsContribution);
        }
        //create getNetPay()method
        double getNetPay()
        {
    	double deductions = getSsTax(7) + getHealthFee(3) + getFedTax() +  getInsurance();
    	double netPay = salary - deductions;
    	return (netPay);
        }
        //create displayInfo()method
        void displayInfo()
        {
    	System.out.println("Employee name: " + name);
    	System.out.println("Employee ID: " + id);
    	System.out.println("Employee age: " + age);
    	System.out.println("Employee salary: " + salary);
    	System.out.println("Federal Tax: " + getFedTax());
    	System.out.println("Social Security Tax: " + getSsTax(7));
    	System.out.println("Health Contribution: " + getHealthFee(3));
    	System.out.println("Insurance Contribution: " + getInsurance());
    	System.out.println("Net Pay: " + getNetPay());
        }
     }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:
    Java Code:
        double getSsTax(int rate)
        {
    	rate = 7;
    	ratePercent = rate / 100;
    	double ssTax = salary * ratePercent;
    	return ((int)ssTax);
        }
    You've got problems:
    • You're ignoring the rate that is being passed in, but instead are creating your own rate of 7. Don't do this.
    • You're doing int division which will result in an int, here, 0. To fix this divide by 100.0 (a double), not 100 (an int).
    • You may want to give the class a ssTaxRate field so you can store the rate and use it later when calculating your net pay.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Here:
    Java Code:
        double getSsTax(int rate)
        {
    	rate = 7;
    	ratePercent = rate / 100;
    	double ssTax = salary * ratePercent;
    	return ((int)ssTax);
        }
    You've got problems:
    • You're ignoring the rate that is being passed in, but instead are creating your own rate of 7. Don't do this.
    • You're doing int division which will result in an int, here, 0. To fix this divide by 100.0 (a double), not 100 (an int).
    • You may want to give the class a ssTaxRate field so you can store the rate and use it later when calculating your net pay.
    Yes, I removed the rate as a variable. I figured it was wrong when I did it but i was grasping for straws. Changing the rate from int to double fixed the problem. It works fine now. Unfortunately, the professor is requiring rate to be an int type. I tried type casting but that doesn't work. I have an e-mail into the professor to verify that rate needed to be int.

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

    Default

    Why are you casting your result to an int if the return type is double?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That rate passed in can still be an int. Integer division occurs when you divide two integers, 15/2 == 7 in integer division, not 7.5 as you'd expect. All integer division truncates any places after the decimal. To remedy this you simply need to change one item to a double(the compiler is smart enough to change the other for you). So 15.0/2 == 7.5 as you expected. This is not integer division.

    If you divide the passed in rate by 100.0, it will return a double representing the correct percentage rate and the passed in value is still an integer.
    Last edited by sunde887; 05-23-2011 at 01:47 AM.

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

    Default

    Quote Originally Posted by Junky View Post
    Why are you casting your result to an int if the return type is double?
    Here are the directions given concerning the methods:

    Methods: (You can decide whether they should be public or private)

    1. getFedTax() - Returns (int) federal tax withheld. Computed as follows:
    multiply (salary - 800) by 17%.

    2. getSsTax(rate) - returns (int) social security tax withheld, rate% of salary;
    rate is int.

    3. getHealthFee(rate) - returns(int) health coverage employee contribution,
    rate% of salary; rate is int.

    4. getInsurance() - returns (int) employee contribution for insurance coverage.
    Amount of deduction is computed as follows:
    If the employee is under 40 rate is 3% of salary, if the employee is between
    40 and 50 rate is 4% of salary, and if the employee is between 50 and 60
    rate is 5% of salary. If the employee is above 60 the rate is 6% of salary.

    5. getNetPay() -returns (double) the net pay for the employee after deducting
    from salary, the deductions: ss(1), health(3), fed_tax() and insurance.

    So I cast it because I kept getting a "possible loss of precision" error when I tried to compile it witrhout casting it.

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

    Default

    Quote Originally Posted by sunde887 View Post
    That rate passed in can still be an int. Integer division occurs when you divide two integers, 15/2 == 7 in integer division, not 7.5 as you'd expect. All integer division truncates any places after the decimal. To remedy this you simply need to change one item to a double(the compiler is smart enough to change the other for you). So 15.0/2 == 7.5 as you expected. This is not integer division.

    If you divide the passed in rate by 100.0, it will return a double representing the correct percentage rate and the passed in value is still an integer.
    I see. Ok. I changed the rates back to int and changed 100 to 100.0.

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

    Default

    Quote Originally Posted by jim01 View Post
    2. getSsTax(rate) - returns (int) social security tax withheld, rate% of salary;
    rate is int.
    In that case your method signature is wrong.

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

    Default

    Quote Originally Posted by jim01 View Post
    I see. Ok. I changed the rates back to int and changed 100 to 100.0.
    Which is what Fubar advised you do. It amazes me how people asking for help don't fully read the replies they get.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    In that case your method signature is wrong.
    I was just about to bring this up.

    If you say you return a double in the method signature you should ensure you return a double. Likewise, if the assignment requires you to return an integer, the method signature should have a return type of int, and you should ensure the method returns one.

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

    Default

    Quote Originally Posted by Junky View Post
    Which is what Fubar advised you do. It amazes me how people asking for help don't fully read the replies they get.
    I misunderstood what he meant.

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

    Default

    Quote Originally Posted by Fubarable View Post
    divide by 100.0 (a double), not 100 (an int).
    How do you misunderstand that?

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

    Default

    Quote Originally Posted by sunde887 View Post
    I was just about to bring this up.

    If you say you return a double in the method signature you should ensure you return a double. Likewise, if the assignment requires you to return an integer, the method signature should have a return type of int, and you should ensure the method returns one.
    Ok. I made the changes. Thank you for your help and your patience.

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

    Default

    Quote Originally Posted by Junky View Post
    How do you misunderstand that?
    Because I zeroed in on the double and int and thought I had my type wrong.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM
  3. Demonstration of text fields in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  4. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 09:39 PM
  5. Top menu fields hiding
    By youayshu in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-13-2007, 07:10 AM

Posting Permissions

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