Results 1 to 13 of 13
  1. #1
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Why is a variable not updated?

    Hi, I'm new to Java and I got a problem updating a variable.
    The following codes are syntactically collect but are not returning an expected value.
    When I call calculate() like change it to "public double calculate()" and do System.out.println(ref.calculate());,
    then it returns a value I expected but it's not working when getIncome() is called to get the calculate value.
    I think I insert a new value to income in the calculate() but it seems not.

    Any help would be appreciated.

    public class Tax{
    private static final int SINGLE = 0;
    private static final int MARRIED = 1;
    private static final double RATE1 = 0.1;
    private static final double RATE2 = 0.25;
    private static final double LIMITS = 3200;
    private static final double LIMITM = 6400;
    public double income;
    private int status;

    public Tax(){
    this.income = 0;
    this.status = 0;
    }
    public void details(double amount, int status){
    income += amount;
    status = status;
    }
    private boolean isSingle(){
    return (status == SINGLE);
    }
    private void calculate(){
    if (isSingle()){
    if (income <= LIMITS){
    double newIncome = income - (income * RATE1);
    income = newIncome;
    } else {
    double newIncome = income - (income * RATE2);
    income = newIncome;
    }
    } else {
    if (income <= LIMITS){
    double newIncome = income - (income * RATE1);
    income = newIncome;
    } else {
    double newIncome = income - (income * RATE2);
    income = newIncome;
    }
    }
    }
    public double getIncome(){
    return income;
    }
    }
    ------------------------------------------------------------------------------------------------------------
    public class Test{
    public static void main(String[] args){
    Tax ref = new Tax();
    ref.details(1000, 1);
    System.out.println(ref.getIncome()); // Should be 900
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Why is a variable not updated?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Why is a variable not updated?

    You need to provide more useful information. What does it return and what are you expecting? And what have you tried to investigate this yourself?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    kumarjivi is offline Member
    Join Date
    Jun 2013
    Location
    India
    Posts
    5
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Case-1:
    The code as you have posted should give the output as 1000, as you have called getIncome() method from reference variable of type Tax.
    Again in the code you have posted, you are not using the method private void calculate() anywhere, so when you use getIncome() method, it is simply returning the value which of the instance variable income (which is set by calling the method public void details(double amount, int status) as in ref.details(1000, 1);).

    Case-2:
    When you will replace the line System.out.println(ref.getIncome()); by System.out.println(ref.calculate()); you sould get the expected result (which I am not sure, what you are expecting). But yes, it should work. :)

    Regards
    Kumar Jivi

  5. #5
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Quote Originally Posted by jim829 View Post
    You need to provide more useful information. What does it return and what are you expecting? And what have you tried to investigate this yourself?

    Regards,
    Jim
    Hi,
    sorry I think I got some typo at calculate() part : the condition where if (isSingle()) is not true then (income <= LIMITM) is needed to check. not (income <= LIMITS) again.

    what Im trying to do is to calculate the amount of income depending on a status which is either Single or Married , and the amount of income.

    For example, if a person is Single and his income is less than LIMITS = 3200, then a tax rate RATE1 = 0.1 is applied.
    If the person is Single and his income is greater than LIMITS = 3200 then, a tax rate RATE2 is applied.
    Same goes to a status Married.
    If his income is less than LIMITM = 6400 then, a tax rate is RATE1 = 0.1.
    If the income is greater than that then, a tax rate is RATE2 = 0.25.

    When I input income 1000 and status 1 representing "Married", I was expecting to get 900.
    because the amount of income is 1000 <= LIMITM(6400) so
    double newIncome = income - (income * RATE1); // 1000 - (1000*0.1) = 900
    income = newIncome; // update income
    But the result called from getIncome() was 1000.

    The value of calculated income at the calculate() was collect when I directly called.
    so I think the problem here is that income calculated at the calculate() is not properly inserted into a instance variable "income".

    I hope I explained what needs to be addressed.

    Regards.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Why is a variable not updated?

    hamChan, please go through the two pages I linked at #2 and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Yes.
    About the case2, I could get an expected value when I called calculate().

    About the case1, I first attempted to return income value from calculate() by public double calculate(){~return income)} and get the value from getIncome().
    However, the instance variable "income" seems has not been updated.

    Regards.

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

    Default Re: Why is a variable not updated?

    To translate one of Darryl's useful request, please edit your original post so that your code retains its formatting allowing us to read and understand it. This involves placing [code] [/code] tags around your code. Your compliance with this will be greatly appreciated.

  9. #9
    kumarjivi is offline Member
    Join Date
    Jun 2013
    Location
    India
    Posts
    5
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Okay, Now i have done this on my machine,
    What I did is modified your posted code to:
    public class Test {
    public static void main(String[] args) {
    Tax ref = new Tax();
    ref.details(1000, 1);
    ref.calculate();
    System.out.println(ref.getIncome()); // Should be 900
    }
    }

    but as you have defined the method calculate() as private (in class Tax) : private void calculate()
    so its not accessible from class Test.
    So, for this you need to change the visibility of method calculate() to default or public,
    then with the code i posted in Test class, you will surely get the desired result.

    So, over-all what you need to do is:
    1. In Tax class: [/I]Change private void calculate() { to void calculate() { or public void calculate() {
    2. In Test class: Use the code I posted in the start of this reply.

    Regards
    Kumar Jivi

  10. #10
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Quote Originally Posted by DarrylBurke View Post
    hamChan, please go through the two pages I linked at #2 and edit your post accordingly.

    db
    Sorry about not following the rule here. I make sure do it from next time. Thank you.

  11. #11
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Re: Why is a variable not updated?

    Yes Ill. Im sorry for making trouble.

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

    Default Re: Why is a variable not updated?

    Quote Originally Posted by hamChan View Post
    Yes Ill. Im sorry for making trouble.
    There's no trouble at all. We're just making suggestions that if followed should make it easier for you to get help. Again, consider editing your original post now. Again thanks for your cooperation.

  13. #13
    hamChan is offline Member
    Join Date
    Jun 2013
    Posts
    18
    Rep Power
    0

    Default Re: Why is a variable not updated?

    oh, I didn't think that I needed to compute calculate() in the Test class.
    coz I thought that once calculate() is proceeded along with program execution, the instance variable "income" is updated.
    so that I dont need to call the method in the Test class.

    I now understand why I couldn't get a value I expected.
    Thank you very much for your helps.

    Regards.

Similar Threads

  1. Spinner does not get updated !
    By DennisM in forum NetBeans
    Replies: 0
    Last Post: 02-28-2012, 06:59 AM
  2. Value of the node does not get updated!
    By snajalm in forum Advanced Java
    Replies: 1
    Last Post: 10-12-2011, 08:13 AM
  3. Replies: 11
    Last Post: 09-27-2010, 12:20 PM
  4. batch updated
    By swati.jyoti in forum JDBC
    Replies: 2
    Last Post: 06-09-2009, 11:33 PM
  5. why my image is not updated ?
    By aneuryzma in forum New To Java
    Replies: 4
    Last Post: 08-15-2008, 12:08 AM

Tags for this Thread

Posting Permissions

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