#Can an array of objects, itself be a different class than that of it's contents?

1 messages · Page 1 of 1 (latest)

shell peak
#

My prof has very very very very very specifically required this to function exactly as follows:
There are 2 classes. Die & dicecollection. This part of the project wants me to store multiple Die objects in an array, and this array HAS to be dicecollection. I can't make a representation i can't make a translation, etc. It very specifically has to be creating an array using the dicecollection class, then storing die inside that array. From everything i've googled and tested for the past hour and a half this simply seems to not exist. Every solution includes doing some sort of translation or transformation or representation of a corresponding array. Any answers greatly appreciated.

void copperBOT
#

<@&987246399047479336> please have a look, thanks.

void copperBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

forest mason
#

do you mean if type of array could be different than it's contents?

shell peak
#

not the type of the array, the class. I have to have 2 classes. Die & Dicecollection. The array HAS to be an object of Dicecollection, and the array HAS to contain Die objects

forest mason
#

i think that's possible

#

did you run into any troubles?

shell peak
#

yes, if i try to do this directly, the IDE tells me it cannot convert the types

forest mason
#

show us what you have done, let me see

shell peak
#

i'll make it more readable just a moment

#

DiceCollection[] lmao;

for (int i = 0; i < diceCount; i++){
    Die die = new Die();

    lmao[i] = die;
    
}
#

it tells me this. Type mismatch: cannot convert from Die to DiceCollection

forest mason
#

can you share the question details?

shell peak
#

I've also spoken to the prof and they confirmed yes it absolutely HAS to be an array object dicecollection and that array HAS to hold die objects

forest mason
#

i don't think it means it has to be of type diceCollection

#

that class is just meant to be holder of array

#

and array is of type dice

shell peak
#

how would i have a different class hold the array though? isn't that also against what they're saying "A DiceCollection object holds a set of Die objects in an array"

forest mason
#

it could be a field which holds array of dies

shell peak
#

so declare array inside the class of dicecollection, then edit that array in the main method?

forest mason
#

is there a way you can confirm again with them? ask if array has to be of type Diecollection and hold object of type die

#

declare an array inside die collection? yes

shell peak
#

i wish i could but it's due in 4 hours and it's 4am so they're definitely not awake. usually they do their emails around noon too.

forest mason
#

pass array of integers to constructor of diecollection to initialize the no of sides each die has

#

i don't think it'll be end of world, if you got confused and tried your best

shell peak
#

so the dicecollection class would create the array, but then wouldn't it be unable to adapt to the array size required?

#

i'll have to do some testing. Thanks a billion for the advice

forest mason
#

from what i can read in questions, each die is supposed to have number of sides represented by an integer value

#

now how many sides each die will have, that's decided by what you pass in constructor of die collection

shell peak
#

yes i can definitely do this. My biggest concern was that at that point i'm technically not having die objects held by a dicecollection object, but at the same time i kind of am? it's odd

forest mason
#

your die collection is holding an array which is holding die objects so yea

shell peak
#

yeah i see, it's layered but still technically in there

forest mason
#

if you have more time, it's better to wait and hope someone else responds that's the best i got for now

shell peak
#

anything is way more than nothing so again thanks a billion for the advice

forest mason
#

yw 😄

shell peak
#

wait to be clear @forest mason are you saying that i would actually have the die objects stored in an array that's in the dicecollection, or are you saying that i would have a representation of them? I'm assuming the latter but wanted to be sure

forest mason
#

i meant the former

shell peak
#

oh wait then nevermind i don't see how that'd be possible. In the end i would still run into the conversion issue, no?

forest mason
#

not really, array would be of type die

#

collection just holds that array of type die

shell peak
#

how though? That just gives me the same error

#

ohhhh are you talking about the util collection?

weak turtle
normal flax
#

(btw, oxford standard approved that dice can also be used as singular to avoid the troublesome word die)

shell peak
#

yeah that's what i'm going towards but now i can't actually pass the user's input because for it to be in a constructor it can't be coming from something static. Unless i construct it within the same main app but then it's being constructed with an array that's just going to be replaced anyway because i need the array's size BEFORE it's construction

shell peak
shell peak
#

God bless all 3 of u i got it working... for now. But still working holy shit hell yeah ty a bunch