help with one method in java

Discussion in 'Mac Programming' started by macman2790, Mar 19, 2007.

  1. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #1
    my method is short an nearly complete, i just need to check to see if that element in the array is occupied.
    Code:
     public void insertSong (int track, String artist, String title, int length){
            
            Song newSong = new Song(length, artist, title);
               songs[track - 1] = newSong;
    
         }
    
    
    i had one method like this that was pretty easy, but its parameter wa an array of song objects, but i have to have this one to override it. it's for school.

    thanks
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    I guess it sort of depends on what constitutes "occupied".

    How about something like this:
    Code:
    if (songs[track - 1] instanceof Song) { 
        // A song already exists in that slot 
    }
    else { 
        // The array element does not contain a Song, but might contain something else 
    }
    Todd
     
  3. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #3
    thanks, theres one more thing i need help with. my findSong() method needs to use this equals() method thats in another class, i figured out how to do this a different way(without equals()).
    heres what i figured out, next is the equals method
    Code:
            int track = 0;
    
            for (int i = 0; i < songs.length; i++) {
                if (songs[i].getArtistName() == artist &&
                    songs[i].getSongName() == title)
                    track = i + 1;
                //else track = -1;
            }
            if(track == 0){
                System.out.print("This song or the artist is not on the album. result:");
                track = -1;
            }
            if (track > 0)
                System.out.print("The track number from the artist and song title" +
                                " you provided is track ");
    
            return track;
    
    heres the equals method, which uses the compareTo interface
    Code:
    public boolean equals (Object obj) {
            try {
                 if (compareTo(obj) == 0)
                     return true;
            }catch(Exception e)
            {
            }
            return false;
            }
    
    
     
  4. thread starter macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Location:
    Texas
    #4
    nevermind figured it out, dont worry about it:D
     

Share This Page