tags:

views:

104

answers:

2

I am adding information about different books, cd, dvd from main() I am trying to use inheritance in this project...

First, i am a beginner so keep that in mind when you help me. please try to keep it really simple.. I will post partial code where i need help then i will post the full code at the bottom..

now in the items class

i am not sure what i do with the item being passed in?

class CD extends Item
{

private String artist;
private String members;
private int number;

public CD(Item musicCD, String... members)  // need help
{
    members = members;

}

please keep in mind i am new to java. Thank you..

+3  A: 

This is probably a homework question, but I think you are starting way too big before understanding a lot of the smaller concepts. If this is from a book, you may want to do some simpler exercises first. There are too many concepts you are likely not familiar with that are involved in building this system.

From a quick glance, here are several issues:

1) I didn't see where you were initializing your sets. Right now, they will be null - you just declared them. You would probably want a hashset or a treeset.

2) Your various classes don't support equals or hash codes, you will see problems when you insert and try to retrieve them.

3) Add band members should not create a new CD - you are already passing in a CD, or at least an ID that you can use to look up a CD in your set.

4) Your constructor of CD is messed up. E.g., why are you getting a CD item as the first parameter? In addition, you might not be setting the members right. Try something like this.members = members

Uri
+1 for actually reading the whole thing.
x4u
This is a homework assignment.. This is my first java class and we were thrown into data structures level and told to figure out the rest since we are coming from c++ . . i got rid of the first cd constructor.
icelated
How do i initialize my sets? i also dont know what this measn"Your various classes don't support equals or hash codes"?
icelated
initialize sets like : private Set<CD> theCDs = new HashSet<CD>();Then, so that CD's are not only equal by identity (i.e. cd1.equals(cd2) -> cd1 == cd2), you need to override the equals(Object) method. When you do that, you need to override the hashCode() method to keep from breaking its contract. A proper implementation of hashCode() is required for usage in a HashSet.
ILMTitan
so that is all i need to do?private Set<CD> theCDs = new HashSet<CD>();
icelated
A: 

Since your using HashSets you will need to override the equals and hashcode methods as @Uri has pointed out. Here's a good article with examples for doing this. Its worth a read so you know exactly what your doing.

Equals and Hash Code in Java

Binary Nerd