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

    Default Dr.Java Need Help! Boolean and Methods

    In summary, MusicAlbum class and MusicCollection class must be able to run TestMusicCollection and run and produce the last part.

    I can most of the stuff to work but I can't seem to get get the following lines to work...

    Java Code:
    public class MusicAlbum {
    
    /* Attributes (instance variables) */
     private String albumName;
     private String artistName;
     private String albumGenre;
     private int albumHours;
     private int albumMinutes;
    
    /* Constructors */
     public MusicAlbum(String album, String artist, String genre, int hrs, int mins)
     {
      albumName = album;
      artistName = artist;
      albumGenre = genre;
      albumHours = hrs;
      albumMinutes = mins;
     }
    
     public MusicAlbum()
     {
    
     }
     
    /* Methods */
     public String toString()
     {
       String s = "";
          s = "Album: " + albumName + "\t" + "Artist: " + artistName + "\t" + "Genre: " +
            albumGenre + "\t" + "Length: " + albumHours + ":" + albumMinutes;
          return s;
     }
    }
    Line 13
    I need to create an array so that it can be references to through an index
    *Type cannot be resolved error*

    Line 22
    This is a boolean method that is supposed to check if I can add another MusicAlbum, in TestMusicCollection, when they use the method it returns false. I can't seem to get it to work though :(
    *This does not return type boolean error*

    Java Code:
    public class MusicCollection {
    
    /* Attributes (instance variables) */
     private String nameOwner;
     private MusicAlbum[] collectionArray;
     private int collectionSize;
     
    /* Constructors */
     public MusicCollection(String owner, int collection)
     {
      nameOwner = owner;
      collectionSize = collection;
      MusicAlbum[] collectionArray = new collettionArray[collection];
     }
       
    /* Methods */
     public void setMusicAlbum()
     {
       new MusicAlbum();
     }
     
     public boolean setMusicAlbum(String albumName, String artistName, String musicGenre, int hours, int minutes)
     {
       boolean result = false;
       if (this.collectionSize >= 0)
       {
         return result;
       }
     }
    
     public MusicAlbum getMusicAlbum(int index) 
     {
       return collectionArray[index];
     }
    
     public int getNumberOfAlbums()
     {
       return this.collectionArray.length;
     }
     
     public String toString()
     {
       String s = "Owner" + this.nameOwner + "has" + this.collectionArray.length + "albums";
         return s;
     }
     
    }
    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));
        }
        
      }
    }
    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
    Last edited by d300539; 04-10-2013 at 10:24 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,986
    Rep Power
    9

    Default Re: Dr.Java Need Help! Boolean and Methods

    We can't do your homework for you, and I would warn you that posting all of it here like this is a great way to get hit by your school's academic dishonesty policy.

    Instead, take your problems one at a time and break them down into an SSCCE that demonstrates your question in as few lines as possible (while still being self-contained). Also, you're going to have to be more specific than saying stuff "doesn't work". Does it generate a compiler error? Runtime error? Something else? We can answer questions like "this piece of code generates this kind of error, I've tried to fix it by doing this, but I still get the error, what am I doing wrong?" much more effectively than "I don't know how to do this."
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Dr.Java Need Help! Boolean and Methods

    For the empty constructor, that's up to you.
    You could give the attributes default values other than null and 0 (which is what they are given at the moment), or just leave them.

    For the collection, the given 'collection' parameter to the constructor is the maximum size of the collection, which is why you;re using it for the creation of the array. That's correct. But you are also giving it to the 'collectionSize' attribute, which represents the actual number of things in the collection (from what I can see). And that is not correct. When you first create the collection you have nothing inthe collection, so that should be 0.

    So that has a knock on effect on getNumberOfAlbums(), which should return the proper value for number of albums, which is not the length of the array.

    You also seem to be missing a setMusicAlbum() with no parameters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Dr.Java Need Help! Boolean and Methods

    Quote Originally Posted by Tolls View Post
    For the empty constructor, that's up to you.
    You could give the attributes default values other than null and 0 (which is what they are given at the moment), or just leave them.

    For the collection, the given 'collection' parameter to the constructor is the maximum size of the collection, which is why you;re using it for the creation of the array. That's correct. But you are also giving it to the 'collectionSize' attribute, which represents the actual number of things in the collection (from what I can see). And that is not correct. When you first create the collection you have nothing inthe collection, so that should be 0.

    So that has a knock on effect on getNumberOfAlbums(), which should return the proper value for number of albums, which is not the length of the array.

    You also seem to be missing a setMusicAlbum() with no parameters.
    When I try to change the colelctionArray to 0 I get "this type cannot be resolved"

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Dr.Java Need Help! Boolean and Methods

    Line 13 - You are trying to declare an array of type collettionArray which does not exist.
    Line 22 - If a method signature contains a return type then it must be able to return a value of that type. What does it return if the condition on line 25 is not met?

    Regards.

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

    Default Re: Dr.Java Need Help! Boolean and Methods

    Quote Originally Posted by Ronin View Post
    Line 13 - You are trying to declare an array of type collettionArray which does not exist.
    Line 22 - If a method signature contains a return type then it must be able to return a value of that type. What does it return if the condition on line 25 is not met?

    Regards.
    Ok I got it to work
    Java Code:
     public MusicCollection(String owner, int collection)
     {
      nameOwner = owner;
      collectionSize = collection;
      MusicAlbum[] collectionArray = new MusicAlbum[collection];
     }
    But I'm still stumped on how to make setMusicAlbum to work properly. I have two setMusicAlbum methods, one to just create a new MusicAlbum object (which works), but the other needs to put the information into the MusicAlbum it just set but it needs to check if there is room in the collection size. :S Im clueless on how to start.

    Also for getNumberOfAlbums I need to make a loop to check how many albums are in the collection but thats the furthest ive gotten..

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Dr.Java Need Help! Boolean and Methods

    That's incorrect.
    collectionSize is still being set to the maximum size of the collection, rather than the number of albums in the collection.
    And collectionArray goes out of scope as soon as that constructor exists.
    Remove the 'MusicAlbum[]' from it.
    (I missed that earlier).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. problems with boolean and recursive methods
    By mflb94 in forum New To Java
    Replies: 4
    Last Post: 09-27-2012, 10:40 AM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  4. Boolean methods & how to read this in plain English
    By bigsonny in forum New To Java
    Replies: 6
    Last Post: 06-19-2011, 09:27 PM
  5. Boolean methods
    By sonny in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 10:16 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
  •