• Post Reply Bookmark Topic Watch Topic
  • New Topic

How should I use this mutator method?  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? ;-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!