programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Rob Spoor
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Holloway
• Piet Souris
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Frits Walraven
• Himai Minh

# How should I use this mutator method?

Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:
So I am doing exercises from my book and I am stuck for nth time. I have problems understanding the exercises from this chapter, this one as well...
Add the following method to the Stock class:
public void clear()
Resets this Stock’s number of shares purchased and total cost to 0.

So I did this :
My problem is I don't know where and how to use it in my code and what result should I look for.
Here is a pastebin with the Stock class http://pastebin.com/r3jbPY7r and the client program http://pastebin.com/dXnczE58 .
I will appreciate your help to understand this better.

Sheriff
Posts: 3036
12
• 1
• Number of slices to send:
Optional 'thank-you' note:
It looks like Stock keeps a running total of how many shares of a stock you have purchased, and the total amount you've paid for them over time. (Shares could be purchased at different prices.) It also has a method to compute your total gain on that stock given the current market price. Clearing the totals just lets you start again with the same stock, as if you had sold all your shares to take the profit, and now are buying more. Now, you could argue that it would better just to create a new Stock instance at that point. In fact, I'd argue that way. That's a deeper question though ... not what does this do, but is this the best way to do it?

Sheriff
Posts: 7111
184
• 1
• Number of slices to send:
Optional 'thank-you' note:
You've created an instance method, so you need an instance of Stock to use it. You've already got an instance of Stock in your code.

Maybe after buying some stock, your stockholder wants to sell everything. The total shares and total cost should go to zero. How would you tell the instance stock1 that it's total shares and cost is zero?

Hint: use the dot operator.

Werner Holt
Ranch Hand
Posts: 37
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:You've created an instance method, so you need an instance of Stock to use it. You've already got an instance of Stock in your code.

Maybe after buying some stock, your stockholder wants to sell everything. The total shares and total cost should go to zero. How would you tell the instance stock1 that it's total shares and cost is zero?

Hint: use the dot operator.

Do you mean to put it at this point?

Knute Snortum
Sheriff
Posts: 7111
184
• 1
• Number of slices to send:
Optional 'thank-you' note:
Yes, that is a possible, but not necessary, use of the method. If your assignment is to use .clear() somewhere in your existing code, then that's a good place to put it. If I were using .clear() it would be in a situation where it was not possible to zero. You might add a .sell(int shares) method or a .sellAll() method. If all shares were sold you might want to be sure the total cost was zero. .clear() is such a trivial method you might just write totalCost = 0; but if clearing the stock was more complex, you would want to call .clear(). Clear? ;-)

 I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising