Results 1 to 14 of 14
  1. #1
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default a little help with the music tracker: how to restart an Array???

    Hi, I need some tips on how to solve this problem I'm having for an assignment. I'm so close but something is clearly missing.
    In short MusicAlbum class and MusicCollection class must be able to run TestMusicCollection and produce a certain output.


    Part A:
    First, you will write a new class called MusicAlbum that will model the information needed about an individual music album. The attributes of an object of this class will be the title of the album, the artist, the music genre and theduration of the album). The class will contain the methods described below.
    Functional Specifications for Part A
    a) You are to write a new class MusicAlbum that will be stored in the file
    MusicAlbum.java.
    b) It will have at least five attributes (instance variables), three of type String: albumname, artist name, and the music genre (i.e. classical. rock, rap, jazz, etc.)and two attributes of type integer representing the hours and minutes comprising the duration of the album (i.e. 2 hours and 37 minutes will be stored separately as a2 and 37).
    c) It will have one or more constructors that meets the requirements of the test
    program provided found in TestMusicCollection.java.
    d) The public methods for the class will be any necessary that meets therequirements of the test program provided found in TestMusicCollection.java.

    Part B:
    You will write another new class called MusicCollection that models a list for a collection of albums.Sample information for all albums in the class will be artificially generated by a for loop in the test program provided.
    a) You are to write a new class MusicCollection that will be stored in the file MusicCollection.java.
    b) It must have a string attribute to store the name of the owner of the collection.
    c) A method getMusicAlbum(int index) that returns a reference to a
    specific album in the collection based on an index value passed as an
    actual parameter.
    A method getNumberOfAlbums() that returns the number of albums currently entered into the collection.
    A method setMusicAlbum() that will add a new MusicAlbum object to the collection. This is where I am stuck in!

    Java 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));
        }
         
      }
    }
    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've created my classes and everything but I do not receive the output provided:

    for MusicAlbum, I created the five instance variables set to private, a to string method, and a constructor that takes in five parameters.
    My problem is in the set MusicAlbum;
    For part two I have added an array private MusicAlbum[] musicArray, And have placed it in the constructor as musicArray = new MusicAlbum[collectionSize]. My problem is my Array does Not reset So when I run my code, it keeps going on and on and on until the for loop in test ends.

    When I use my setMusicAlbum methods, I get an ouput of

    1.) Album: Blank Artist: None Genre: None Length: 0:0
    2.) Album: Album2 Artist: Artist2 Genre: Type2 Length: 1:23
    3.) Album: Blank Artist: None Genre: None Length: 0:0
    4.) Album: Album4 Artist: Artist4 Genre: Type4 Length: 3:25

    my set MusicAlbum is currently:
    Java Code:
     if(i%2 == 0) 
             {
               this.musicArray[i] = new MusicAlbum("Blank","None","None",0,0);}
       
            
            else
            {musicArray[i] = new MusicAlbum(AlbumName,Name,type,hours,minutes);
            }
        i++;    
          
      
        return false;
    I realize there is anouther set MusicAlbum. I am not quite sure what to do with this. Somehow I have to set my Array so that it restarts. I have declared a static variable "i" but that doesn't seem to do it either. I have tried different ways so that execution in the setMusicAlbum stops but it doesnt seem possible. Any help will be appreciated, I feel I'm really close and just missing something.

    Edit: So now I realize that the second set Method is where I place my array. Thats great but still no luck with making it so it resets itself!
    Last edited by bob2500; 06-16-2014 at 06:41 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    my Array does Not reset
    What does "reset an array" mean?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    well I tried to put the music array first in the constructor and then in the setMusicAlbum methods. if I place it in the constructor
    This will go onto my next set method. but after array[0] it gives me
    1.) Album: Blank Artist: None Genre: None Length: 0:0
    2.) Album: Album2 Artist: Artist2 Genre: Type2 Length: 1:23
    3.) Album: Blank Artist: None Genre: None Length: 0:0
    4.) Album: Album4 Artist: Artist4 Genre: Type4 Length: 3:25

    the second line in the output should be something more like this:

    Album: Album1 Artist: Artist1 Genre: Type1 Length: 0:22

    it seems to me that I am supposed to restart the array index, the array[index].

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    It looks like the code is skipping elements in the array.
    0 is empty/default
    1 is filled
    2 is empty/default
    3 is filled


    restart the array index
    Not sure what "restart" means. Normal array index usage is to start at 0 and increment by 1 until reaching the array length-1.
    Last edited by Norm; 06-16-2014 at 07:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    Oh yes I see that so I have attempted a few things but I still cannot get the output. I have tried to create an if statement that ignores every other line but still not getting it. I have also tried to setmusicAlbum with no parmenters to this.musicArray[i] = new MusicAlbum("Blank","None","None",0,0);
    Then the for loop will go to the next set music album with five parameters:
    musicArray[i] = new MusicAlbum(theAlbumName,ArtistName,thetype,thehour s,theminutes);

    then i will increment but this gives me something out of bounds. Do you have any suggestions? is it possible with an if statment?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    Can you post the code you are having problems with? Be sure to wrap the code in code tags to preserve formatting.
    gives me something out of bounds
    Array indexes range in value from 0 to the array's length-1
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    here was my previous set methods
    Java Code:
     public void setMusicAlbum() 
      { 
     musicArray[i]=null;
      }
    and second one
    Java Code:
      public boolean setMusicAlbum(String theAlbumNamew, String ArtistName2, String thetype, int thehours, int theminutes) 
      {
    
             if(i%2 == 0) 
             {
               this.musicArray[i] = new MusicAlbum("Blank","None","None",0,0);
             }
       
            
            else
            {musicArray[i] = new MusicAlbum(theAlbumNamew,ArtistName2,thetype,thehours,theminutes);
            }
         
       
    
       
        i++;    
          
      
        return false;
        
    }
    But I have also tried to start an musicArray at the no paramater setMusicAlbum with new MusicAlbum("Blank","None","None",0,0);
    Last edited by bob2500; 06-16-2014 at 11:28 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    What is the purpose of the if statement on line 17?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    you mean the else? or the blank? if you mean the blank that wasnt supposed to be there. But I was trying to meet the conditions of the assignment. In the second if because every other line seems to be a blank; i made it so if the array at [i] is an even then this line stores a blank music Album in the array. Else it will store the MusicAlbum requested.
    Also the [i] was initialized as a public static variable as an attribute in the beggining of the class.

    as so
    Java Code:
     
    //constructors
    private String name;
      private int collectionSize;
      private MusicAlbum[] musicArray;
      public static int i = 0;
    I do not know if this is correct though.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    After the edit, the if statement moved to line 12. This is the statement:
    Java Code:
      if(i%2 == 0)
    What is that for?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    well i was trying to fill every other line in the array at blank. I though I could do this with an if statement. So if array[i] is an even number set out blank music album. Else, set music Album to parameter.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    fill every other line in the array
    Ok. Why?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    bob2500 is offline Member
    Join Date
    Jun 2014
    Posts
    7
    Rep Power
    0

    Default Re: a little help with the music tracker: how to restart an Array???

    the output at every other line seems to be filled in with a musicAlbum at a passed parameter when setMusicAlbum is invoked. I was simply trying to format the same output. But i see that it doesnt make much sense. So i was then wonder if there is a way to increase the size of an array one by one, but that doesnt make much sense either.
    Last edited by bob2500; 06-16-2014 at 11:12 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: a little help with the music tracker: how to restart an Array???

    a way to increase the size of an array
    an array is defined with a size that can't be changed. If you want a larger array with the same data, you need to define the new array and copy the contents of the old array to the new array. There are utility methods to do that.
    And then assign the reference to the new array to the variable that referenced the old array.

    Use a class like ArrayList to hold the data. It will automatically take care of increasing its capacity.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Tournament Pool and Bracket Tracker 0.6.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-22-2013, 10:22 PM
  2. BORG Calendar and Task Tracker 1.7.6
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-18-2012, 05:10 PM
  3. How to load image using Media Tracker
    By syarizma in forum Advanced Java
    Replies: 8
    Last Post: 08-07-2009, 04:12 AM
  4. How to load image using Media Tracker
    By syarizma in forum Advanced Java
    Replies: 0
    Last Post: 08-05-2009, 05:21 AM
  5. GPS Tracker 0.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:35 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
  •