1

I'm having a little trouble with this problem. It's simple:

I want to use defineBuyTax(6)/defineSellTax(5) to change txbuy/txsell, so I can use printTaxes and see the values in terminal. But, I'am receiving the values 0 and 0, not 6 and 5 as expected. So, basically it is not changing the values in Conversor();

I managed to fix it, creating a Conversor object to receive defineBuyTax and then manually prints it on the main function. Also, if I use System.out.print(cm.txbuy) inside the defineBuyTax method (as indicated in the code), it returns the right value.

So, what I want to know is: how can I change the variables in Conversor() so my printTaxes method do it's job correctly?

class Conversor { public double txbuy; public double txsell; public Conversor() { this.txbuy = 0; this.txsell = 0; } public Conversor defineBuyTax (double tax) { Conversor cm = new Conversor(); cm.txbuy = tax; System.out.println(cm.txbuy); return cm; } public Conversor defineSellTax (double tax) { Conversor cm = new Conversor(); cm.txsell = tax; System.out.println(cm.txsell); return cm; } void printTaxes() { System.out.println("Buy tax: "+txbuy); System.out.println("Sell tax : "+txsell); } } class Test { public static void main (String[] args) { Conversor cm = new Conversor(); cm.defineBuyTax(6.0); cm.defineSellTax(5.0); cm.printTaxes(); } } 

4 Answers 4

2

use this instead of creating new instance of Conversor in each call of defineBuyTax or defineSellTax:

class Conversor { public double txbuy; public double txsell; public Conversor() { this.txbuy = 0; this.txsell = 0; } public Conversor defineBuyTax (double tax) { this.txbuy = tax; System.out.println(this.txbuy); return this; } public Conversor defineSellTax (double tax) { this.txsell = tax; System.out.println(this.txsell); return this; } void printTaxes() { System.out.println("Buy tax: "+txbuy); System.out.println("Sell tax : "+txsell); } } class Test { public static void main (String[] args) { Conversor cm = new Conversor(); cm.defineBuyTax(6.0); cm.defineSellTax(5.0); cm.printTaxes(); } } 

The this keyword refers to the current object in a method or constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't create a Conversor object inside the define methods. Even if you need to chain the calls it will not work that way. The following code should work as you intend.

public class Conversor { public double txbuy; public double txsell; public Conversor() { this.txbuy = 0; this.txsell = 0; } public void defineBuyTax (double tax) { txbuy = tax; } public void defineSellTax (double tax) { txsell = tax; } void printTaxes() { System.out.println("Buy tax: "+txbuy); System.out.println("Sell tax : "+txsell); } } 

Comments

1

What you're currently doing in defineBuyTax:

  • Create a comepletely new Conversor
  • Set this new Conversor's txbuy to the given tax
  • Return that new Conversor

So you're only working on a new Conversor that is then returned by the method. However, at your method call cm.defineBuyTax(6.0), you actually don't use the returned Conversor (that would be Conversor newConv = cm.defineBuyTax(6.0);).

I suspect that you actually want to work on the existing Conversor instead of a new one, so rewrite your function like this:

public void defineBuyTax (double tax) { this.txbuy = tax; } 

or

public Conversor defineBuyTax (double tax) { this.txbuy = tax; return this; } 

if you want to have the return value. Same for defineBuyTax, of course. The Test class doesn't need to be changed and should work now.

Comments

1

Since your define... methods return a new object you should chain your calls into one.

cm.defineBuyTax(6.0).defineSellTax(5.0).printTaxes(); 

If you on the other hand only want to work with a single instance, then the define... methods should update this and return void.

public void defineBuyTax(double tax) { this.txbuy = tax; } public void defineSellTax (double tax) { this.txsell = tax; } 

Note that the naming standard for setting a value for an instance variable is setX so if you want to use the second solution you should rename the methods to setSellTax and setBuyTax

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.