This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes OO problem .. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "OO problem .. " Watch "OO problem .. " New topic
Author

OO problem ..

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi,









I have these classes .. Let's say class Access was ran and it sets f[0]'s stock to 1. My problem is that how can class ProblemHere's object get a stock of 1 when it tries to do something like this:

f[0].getStock();

Obviously, when I create a new object of Fruits2, everything is reset .. Any help? I've been scratching my head for a few hours now ..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Obviously, when I create a new object of Fruits2, everything is reset ..

ProblemHere has to know about the Fruits2 instance you have created. What about passing obj to a method of ProblemHere ?

(I'm a bit concerned about the fact that the array in Fruits2 is not initialized)


[My Blog]
All roads lead to JavaRanch
Ashutosh Chauhan
Greenhorn

Joined: Feb 26, 2009
Posts: 14
My friend listen carefully just do what i say just intialize the Arrray fruits
like this


Fruits f[]=new Fruits[10];

that is it your done the code will execute properly ,also you have missed a semicolon in your code see carefully and make the adjustment rest all is fine
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
@Ashutosh Chauhan

Sorry, not sure how will that fix my problem..

@Christophe

I was thinking about that too, is that the best way to do it?

It's actually an assignment and a similar class was given to us so I can't change much about the design of the classes..

The fruits2 array will always be initialized in class Access since the create() method is used..
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The fruits2 array will always be initialized in class Access since the create() method is used..


No it doesn't. The create method does not initialize the array. Hint: To initialize an array you commonly must use the new opeator.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Oh yes, thanks for pointing that out, I'm getting more confused by the second

I tried initializing that array Fruits[] f = new Fruits[6] .. still not sure how it could fix it...

I just remembered that when class ProblemHere sets the stock, class Access has to know it to .. Is there anyway for both classes to 'share' one single object of Fuits2 so that when one class changes anything, the other will know it?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
In general, that's not how it should work. For a good OO solution, you should be able to ask Fruits2 how many Apples are left and it should just tell you. You shouldn't have that much access to its internal representation. For this example let's change the name of Fruits2, which means nothing, to FruitStock which has a more intuitive meaning. The code which exercises the FruitStock class might look like this:



Notice how none of the code that uses FruitStock knows exactly how FruitStock stores its information. Nor does it care, FruitStock just provides convenient methods for adding removing and querying the current stock. It's free to implement these methods any way it wants to, as long as it still does what it promises to do.


Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Thanks for the example Garrett, I'm still wondering though how will for example, a separate class named "FruitQuery" get the current values of the fruits set inside FruitStockTester?

I'm sure I just missed this but I tried my very best to understand your example. I'm very new to OO thus, the major confusion it gives me.

Thanks again.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I'm still wondering though how will for example, a separate class named "FruitQuery" get the current values of the fruits set inside FruitStockTester?


FruitStockTester doesn't have any values per se. It just uses a FruitStock object to store and manage it's Fruit. If you're asking what I think you're asking, it would be synonymous to your imagined FruitQuery class. It creates (or gets from somewhere) a FruitStock object. It can ask the FruitStock how much Fruit it has, and it can even ask for particular Fruit if it is available.
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Ah, ok. I think I finally got it.

What I'm looking for is what Christophe said -- pass FruitStock object inside FruitStockTester to FruitQuery.

Thanks!
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
ran into another problem ..

What I did was I passed an object of FruitStock inside FruitStockTester to FruitQuery's constructor when I initialized it.

If I'm not mistaken, since I'm passing by reference, changes in the object made inside FruitQuery class will be 'seen' by FruitStockTester.

Problem is since I passed the object to the constructor, is there anyway for other methods inside FruitQuery class to change the object?

What I did was I did something like this:



changes aren't seen by FruitStockTester .. Any help with this ?

EDIT: I understand that obj can be passed around but my program is an frame and changes to obj are supposed to be made through events ..
 
Don't get me started about those stupid light bulbs.
 
subject: OO problem ..
 
Similar Threads
Arrays are confusing
Extending the VendingMachine
My Array
cant retrieve data.
Rate my code