Results 1 to 6 of 6
  1. #1
    d300539 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Help with Creating Classes

    I am given this code.....

    Java Code:
    public class TestMusicCollection
    {
    
      public static void main(String[] args)
      { 
        final int  COLLECTION_SIZE = 4;
        MusicCollection myCollection = new MusicCollection("Dunsul McCoolguy",COLLECTION_SIZE);
        
        for (int i = 0; i < COLLECTION_SIZE; i++)
        {
          myCollection.setMusicAlbum();
          myCollection.setMusicAlbum("Album"+(i+1),"Artist"+(i+1),"Type"+(i+1),1*i,21+1*(i+1));
        }
    
        if (!myCollection.setMusicAlbum("White","Beetles","Rock",2,37))
          System.out.println("Collection already full...");
        
        System.out.println(myCollection);
        System.out.println("==============================");
        
        for (int i = 0; i < myCollection.getNumberOfAlbums(); i++)
        {
          System.out.print((i+1) + ".) ");
          System.out.println(myCollection.getMusicAlbum(i));
        }
        
      }
    }
    In order to print this onto the screen

    Java Code:
    Collection already full...
    Owner Dunsul McCoolguy has 4 albums
    ==============================
    1.) Album: Blank Artist: None Genre: None Length: 0:0
    2.) Album: Album1 Artist: Artist1 Genre: Type1 Length: 0:22
    3.) Album: Blank Artist: None Genre: None Length: 0:0
    4.) Album: Album2 Artist: Artist2 Genre: Type2 Length: 1:23
    I need to make a class called MusicAlbum and class MusicCollection (that models a list for a collection of albums.

    The MusicCollection class must contain the methods
    setMusicAlbum() - which adds a new MusicAlbum object to the collection
    getMusicAlbum(int index) - that returns a reference to a specific album in the collection based on an index valued
    getNumberOfAlbums() - that returns the number of albums in the collection. This is not the maximum number of albums allowed (COLLECTION_SIZE) but represents a number equal to or less than the maximum based current entries up to the that point.

    Addition methods, constructors, etc may be need to produce the TestMusicCollection

    ....

    So far I have

    Java Code:
    public class MusicAlbum {
    
    /* Attributes (instance variables) */
     private String albumName;
     private String artistName;
     private String musicGenre;
     private int hours;
     private int minutes;
    
    /* Constructors */
     public MusicAlbum(String album, String artist, String genre, int hrs, int mins)
     {
      albumName = album;
      artistName = artist;
      musicGenre = genre;
      hours = hrs;
      minutes = mins;
     }
    
    /* Methods */
     public void setMusicAlbum(String albumName, String artistName, String musicGenre, int hours, int minutes)
     {
       this.albumName = albumName;
       this.artistName = artistName;
       this.musicGenre = musicGenre;
       this.hours = hours;
       this.minutes = minutes;
       
     
    }
    }
    I wasn't sure if I need to put a public method for setMusicAlbum since they used it twice in the test method. :(

    Java Code:
    public class MusicCollection {
    
    /* Attributes (instance variables) */
     private String nameOwner;
     private int collectionSize;
     
    /* Constructors */
     public MusicCollection(String owner, int collection)
     {
      nameOwner = owner;
      collectionSize = collection;
     }
       
    /* Methods */
     public void setMusicAlbum(String albumName, String artistName, String musicGenre, int hours, int minutes)
     {
      
     }
    
     public int getMusicAlbum(int index) 
     {
      return 
     }
    
     public int getNumberOfAlbums()
     {
      return collectionSize;
     }
    
    }
    Im not sure for getMusicAlbum or the setMusicAlbum public methods :(

    Please help me! I think I have the basics down but right now my code is really messy because I cant figure it out :S!

    P.S I am using Dr. Java
    Last edited by d300539; 04-06-2013 at 12:54 AM.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help with Creating Classes

    Hi and welcome to the forums.

    Your set and get methods lists above belong in the MusicCollection class. Change the setMusicAlbum() method in the MusicAlbum class to a constructor. You could include a couple of other get methods in there as well if you wish but I would recommend a toString() method at least.
    The getMusicAlbum() method in the MusicCollection class should return a reference to an Album class and not an int.

    Regards.

  3. #3
    d300539 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with Creating Classes

    Quote Originally Posted by Ronin View Post
    Hi and welcome to the forums.

    Your set and get methods lists above belong in the MusicCollection class. Change the setMusicAlbum() method in the MusicAlbum class to a constructor. You could include a couple of other get methods in there as well if you wish but I would recommend a toString() method at least.
    The getMusicAlbum() method in the MusicCollection class should return a reference to an Album class and not an int.

    Regards.
    Thanks I will try that!
    For the getMusicAlbum how do I return a reference to the Album Class? Would it be a string?

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help with Creating Classes

    You can use Album as the return type in the method signature instead of int.

    Regards.

  5. #5
    d300539 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Help with Creating Classes

    I tried your suggestions, but im still pretty new to this :S
    How do I make setMusicAlbum a constructor? Does the constructor have to have like public MusicAlbum(etc) otherwise it tells me to change it to a method since public setMusicAlbum(etc) I would have to do public void setMusicAlbum(etc), but thats a method right? And for the setMusicAlbum method in the MusicCollection what would the body of the code look like?

    I also change my getMusicAlbum to what you recommended but how would I return the all the information (artist name, genre, etc) from a specific album based on the index?

    Sorry for so many questions :( I'm still trying to get a hang of this

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help with Creating Classes

    You are right regarding the constructor.

    As for the setMusicAlbum method. I can't give you the code, that would be cheating. What I can say is you would need an array to hold the instances of MusicAlbum within the MusicCollection class. The method setMusicAlbum would create a new instance of MusicAlbum with any passed parameters and add this instance to the array.

    Your getMusicAlbum would return the instance of MusicAlbum at the index. Assuming you have the necessary get methods, you can call the value of any field from the instance such as artist name, genre, etc. However, implementing a toString() method would be much neater though there is nothing wrong with having a toString() method and all of the get methods.

    Regards.

Similar Threads

  1. Creating objects of Abstract Classes ?
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 05-24-2012, 11:33 AM
  2. I need help creating classes to make a program run!!!
    By semi_noob in forum New To Java
    Replies: 2
    Last Post: 04-01-2012, 04:35 AM
  3. classes and creating objects
    By Latanyar in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 07:14 PM
  4. help creating two classes that function together
    By sinreaver in forum New To Java
    Replies: 6
    Last Post: 10-01-2008, 03:44 AM
  5. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •