开发者

help with a function

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

  1. project - main()

  2. libaray - addBandMembers function

  3. 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 Object but 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.

 
CompactDisk cd = (CompactDisk)item;
cd.addband(...);

or you can do it as a one-liner as

((CompactDisk)cd).addband(...);

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.


There are a lot of things that don't make sense in this question. Add suggests increasing the number of items in a collection. So addBandMembers should mean you are adding Band members to a band. so I would expect there to be a Band class that contained that method. It should look something like addBandMembers(Set<BandMember> bandMembers) Your addMusicCD is definitley an acceptable way of adding a item to a collection, but requires some value for each parameter, therefore you may want to require just a CD whose constructor can have those specific parameters required.

I would suggest a base class for your media possibly called media that might have a Band property and all other basic properties common to all the types of media. Then you can inherit from the media class in your CD, DVD and Book classes and add specific properties to those media types.

This should get you started.


If by

//musicCD.addband(members);   // both cant find addband function..
 //Item.addband(members);

You mean that it won't compile then probably the other class where you have

 private String [] members;  


    public void addband(String... member)
    {
        this.members = member; 

    }

is not the Item class. That is why musicCd.addband(members) won't work as musicCD is an Item. Item.addband(members) won't work as addband is not a static method.

If I understood correctly the addband method is on your CD class. You should have something like this to make it work.

Note: If you 15 is the CD price, then you should consider using BigDecimal instead of int, double or whatever you are using. If it was something else then change the BigDecimal for and int/double and its corresponding parameter name.

Also, I assumed that the out parameter was a PrintStream

import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) {


        Band band = new Band("Jerry Garcia Band");
        band.addMember("Jerry Garcia");
        band.addMember("Keith Godcheaux");

        MusicCD cd = new MusicCD("Don't Let Go", band, new BigDecimal(15),
                "acid rock", "jam bands");

        Library library = new Library();
        library.addMusicCD(cd);

        library.printItem(System.out, cd);

    }

}

public interface Item {

}

public class Book implements Item {

}


public class DVD implements Item {

}

public abstract class CD implements Item{

    private String title;
    private BigDecimal price;
    private String information;

    public CD(String title, BigDecimal price, String information) {
        this.title = title;
        this.price = price;
        this.information = information;
    }

}

import java.util.HashSet;
import java.util.Set;

public class Band {

    private Set<String> members;
    private String name;

    public Band(String name) {
        this.members = new HashSet<String>();
    }

    public void addMember(String member) {
        members.add(member);
    }

}

import java.io.PrintStream;
import java.util.HashSet;
import java.util.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>();

    public void addMusicCD(MusicCD cd) {
        theCDs.add(cd);
    }

    public void printItem(PrintStream out, Item item) {
        out.print(item);
    }

}

I am sure things can be added to this. I tried not change it a lot so you could understand what was needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜