I am trying to add information from main()
to an items class where i am storing the information in a 3 different hashsets
i have 3 classes
-
project – main()
-
libaray – addBandMembers function
-
Item – addband(String… member)
i am adding CD information.
first, i add band, # of songs, title – which works good
Where i am having a problem is adding band members..
I think i need to cast musicCD object to CD class then invoke the
addband function?
Im just not sure how to do that.
Here is the parts of code i think you will need to help me..
this is what i have:
Main()
item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
library.printItem(out, item);
}
Then, here the first function thats called..
This is where i need help!!!!
public void addBandMembers(Item musicCD, String... members)
{
//musicCD.addband(members); // both cant find addband function..
//Item.addband(members);
}
Then in another class i am trying to add the information..
private String [] members;
public void addband(String... member)
{
this.members = member;
}
oh ya, here is my set..
public class Library
{
private Set<CD> theCDs = new HashSet<CD>();
private Set<DVD> theDVDs = new HashSet<DVD>();
private Set<Book> theBooks = new HashSet<Book>();
So, from the function public void addBandMembers()
i am trying to add members to addband
is my addband function wrong?
I do have a background in C++ and i am trying to apply what i know to java so please be nice. I know i have some more reviewing to do i just cant find what i need on the web..
Thank you..
There appears to be several issues you need to address…
First, in addBandMembers(), if it can’t find musicCD.addBand then you either need to define the addBand() method for Item or find the appropriate class that has an addBand() method based on the objects that you can access from Item.
Second, you need to understand the difference between class methods and object methods. Class methods, identified by the “static” keyword, operate on the base class in a way that’s shared by all instantiated objects of that class. For example,
static foo(x){ this.x = x; }would set the class’s static “x” variable, and any access to the “x” variable will use the last set value from calling foo() (assuming no other ways to set x). So, if you have object1 and object2, both of the class that defines foo, object1.x and object2.x would be the same location in memory, both set at the same time when calling foo(). Instance variables, identified by the distinctly missing “static” keyword, are not shared.public bar(y){ this.y = y; }would set a different location in memory for each object of the class – object1.y would be a different memory location, and a (potentially) different value than object2.y.Third, “Item” is a rather non-descriptive name. Is your library guaranteed to always be for music, or do you need to be more generic? Renaming the class to either LibraryItem or Media (as suggested in another answer) would clarify your code.
Fourth, you didn’t provide nearly enough information to really diagnose what’s going on. When you ask for help, you should provide relevant output (what gets printed at the end of main?), classes where relevant variables/functions are defined (where are the addband() and addBandMembers() functions defined?) and any error messages (what error do you get when you uncomment either line within addband()?). With complete information, it’s much easier for people to help. Without complete information, it’s often impossible for people to give really good answers.
Fifth, you talk about casting to
Objectbut mention you don’t know how. Casting in Java is very similar to casting in C++ :Foo myFoo = (Foo)myBar;. You’ll get a ClassCastException at runtime if myBar is not a subclass of myFoo and myFoo is not a subclass of myBar. Note that you don’t need to cast subclasses to their superclasses, as the JVM already knows the class heirarchy, just like the compiler knows in C++. All classes in Java inherit from Object, so there’s almost never a need to cast to Object. On the other hand, if you happen to have a subclass of Item where addband() is defined, you can cast item (in main) to the appropriate subclass, and call the addband() method on the casted object.or you can do it as a one-liner as
The first one would be useful if you need to use the object as a CompactDisk more than once. The second one would be acceptable if you only need to cast once, maybe twice both next to each other – more than that, creates readability and maintenance problems.