Results 1 to 10 of 10
- 02-09-2013, 04:22 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Using an item in another class which is created in another class.
I have a class which creates an array, ill call this class1. In another class (class2) I want to use this array, i have created the class1 object within class2 but I want to loop through the array from class1 in class2. How would i do this? This is what i tried so far.
public void play()
{
POC.createFullDeck();
POC.shuffle();
for(Card c : POC){
..
}
}
Thanks for any help.
-
Re: Using an item in another class which is created in another class.
Class2 should create a Class1 object and place it in a Class1 variable, and then it should call public methods on it in order to change its state or to ascertain its state.
- 02-09-2013, 07:30 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Using an item in another class which is created in another class.
Do you mean like this? I get an incompatible type error when i try to compile.
ArrayList<Card> playDeck = POC.createFullDeck();
POC.shuffle();
for(Card c : playDeck){
}
Thanks.
-
Re: Using an item in another class which is created in another class.
I don't really have an idea of what you're trying to do with this small unformatted code block. Consider telling us more and showing us more, including some well-formatted code that is posted here using tags so that it retains its formatting. Consider posting any and all error messages and indicating with a comment in the code the line(s) that cause the errors.
- 02-10-2013, 06:11 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Using an item in another class which is created in another class.
I have to make a card game. In a class called PileOfCards, an array is created of type Card and type Card has two parameters; a suit and a value. In the play class, I need to use the arraylist which is created in the PileOfCards class. I don't know how to use this method whilst keeping the method public. I don't want to post any of the code given to me by the lecturer and i understand that that will make it harder to help me, so if you can't without it, it doesnt matter.
Thanks for your help.
-
Re: Using an item in another class which is created in another class.
PileOfCards should not expose its fields to any other class, including Play, but instead should expose public methods. If you want PileOfCards to deal a Card, then give it a deal() method that removes a Card from the Card array and returns the removed Card. Then Play can create a PileOfCards object and call its deal method like so:
Make sense?Java Code:PileOfCards pileOfCards = new PileOfCards(); Card someCard = pileOfCards.deal(); // we call this on the variable, not the class
- 02-11-2013, 01:25 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Using an item in another class which is created in another class.
Thank you so much! My code now works!
So should I always return individual objects from an ArrayList to the class I want to use them in rather than trying to return the whole ArrayList?
- 02-11-2013, 02:06 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Using an item in another class which is created in another class.
Generally, yes.
The reason is that if you return the array itself then it can be altered by code other than the code that "owns" the array. So cards could be added or removed from it, which shouldn't be allowed.Please do not ask for code as refusal often offends.
- 02-12-2013, 10:08 AM #9
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Re: Using an item in another class which is created in another class.
Okay, so is that why my IDE kept giving me a warning message?
- 02-12-2013, 10:14 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Amount of .class files created
By Lowest0ne in forum Advanced JavaReplies: 4Last Post: 12-03-2012, 04:02 PM -
Access object created in another class
By BalintD in forum New To JavaReplies: 5Last Post: 01-04-2012, 11:06 AM -
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
create a tree when a new class is created
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks