hint:is that how you declare an array in java? Or is it an array
String cdName[];
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Take another look at the line you have bolded - the return line. Does it match any variable in your program?
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
cdName() is trying to call a function called "cdName". Elements of string are referenced using [...] (if that is what you want to do). Also, your setter method is pretty much useless, I think you mean cdName = diskName?
sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
Why do you wish to use an array for cd name? A cd only has a single name.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
>but I cannot find any examples of such a beast for me to learn and build off
There are so many, are you sure you have looked hard enough?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
That was part of my misunderstanding. I was going to create an array for each variable. (I thought each variable would be a different type and require its own array).
Now I understand that I can use a single array for all field types. I just left in named cdaName because that made the most sence to me, it will be an array of cd information.
My problem now is that I do not understand where to put this array. I want it to be populated with information input from the user, so I think I need to keep all my get and set code, but where does my array tie in? Do I need to create another class completley?
I think my array should look something like
String cdaName[]= new String{cdName, price, itemno, nstock};
but I cannot find any examples of such a beast for me to learn and build off of.
Actually, that is not going to help you in the way that you think it might. With the array you have, you will have string entries for the name of each field - but no values for them. Now, you could use a two dimensional array to allow for the name/value pair, but how does that make your class more functional? It doesn't. It just makes it more confusing to use.
In short, there is no compelling reason for you to combine all your variables into an array at this point. Keep them as individual properties.
There can sometimes be value in using a hash table or array for multiple properties like this, but at your current level of programming you are only making things more complicated for yourself. Strive for clear and easy to manage code and leave the tricky stuff for later - and even then, only if it's called for.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
What class uses the CD class? A person or store may have many CDs and that would be an appropriate use for an array.
Compactdisk[] myCDs = new Compactdisk[100];
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Think about how the different entities interact. An inventory is simply a collection of things. Each of those things have properties such as a name, price, etc.
If you were using an inventory, what would it need to do? Perhaps list it's items, add a new item, remove an item. Arrays and other things like vectors allow you to manage a collection of things.
The things in the collection have various properties that describe them. To interact with each one, they provide methods to let you get or set their various properties and perhaps they may have other functions they can do. These methods define what you can do with a type of thing.
Keeping that in mind, consider how your inventory would keep track of many CDs, add a new CD, etc. A new CD object does not have any definition (or "state") until you set those properties on it.
I hope that helps a bit. I'm being a bit vague on purpose so that you will consider the parts of your program and how they should interact. Try to build them to act as real world entities, each with their own state and abilities.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847