Okay, so I made a method that will take a Track with the MAXIMUM_RATING, which is 10 and make an another ArrayList for those tracks with the highest rating.

public ArrayList allTracksWithMaxRating() {
        ArrayList<Track> maxRatingTracks = new ArrayList();
        int i = 0;
        while (i < tracks.size()) {
            if (tracks.get(i).getRating() == Track.MAXIMUM_RATING)
            maxRatingTracks.add(tracks.get(i));
            i++;
        }
        return maxRatingTracks;
    }

This part works when I test it by hand, but I have to also write a test class for it and I cant get the test to pass.

{
    private Playlist theTracks;
    
    public void testShouldMakeArrayListWithOnlySongOne() {
        theTracks = new Playlist("mo");

		theTracks.add(new Track("song 1", 10));
		theTracks.add(new Track("song 2", 4));
		theTracks.add(new Track("song 3", 6));
		theTracks.add(new Track("song 4", 8));
		
		assertEquals(true, theTracks.allTracksWithMaxRating().contains("song 1"));
    }
}

It keeps coming back false, but by hand It always shows that the Tracks with a rating of ten are in the new ArrayList. I have never had to write a test class for a second ArrayList before so it might just be me writing my test code wrong. Any help would be appreciated.
Also here is my contains method if it might help.

public boolean contains(String targetTracksName) {
        if (targetTracksName == null) {
            throw new IllegalArgumentException("Must not be null");
        }

        return (indexOf(targetTracksName) >= 0);    
    }

Recommended Answers

All 2 Replies

Line 12 you are looking for a String "Song 1" in the ArrayList, but the ArrayList contains Track objects. (Ditto line 7 of final code).

ps Java 1.5 enhanced for-each loop:

int i = 0;
while (i < tracks.size()) {
  if (tracks.get(i).getRating() == Track.MAXIMUM_RATING)
  maxRatingTracks.add(tracks.get(i));
  i++;
}

becomes:

for (Track t : tracks) {
  if (t.getRating() == Track.MAXIMUM_RATING)
  maxRatingTracks.add(t);
}

Line 12 you are looking for a String "Song 1" in the ArrayList, but the ArrayList contains Track objects. (Ditto line 7 of final code).

ps Java 1.5 enhanced for-each loop:

int i = 0;
while (i < tracks.size()) {
  if (tracks.get(i).getRating() == Track.MAXIMUM_RATING)
  maxRatingTracks.add(tracks.get(i));
  i++;
}

becomes:

for (Track t : tracks) {
  if (t.getRating() == Track.MAXIMUM_RATING)
  maxRatingTracks.add(t);
}

I need to get the Track name out in order to ensure that it is there. Here is my indexOf method that my contains method called to if it helps.

public int indexOf(String targetTracksName) {
        if (targetTracksName == null) {
            throw new IllegalArgumentException("Must not be null");
        }

        int i = 0;
        boolean foundTargetTrack = false;

        while (!foundTargetTrack && i < tracks.size()) {
            String trackName = tracks.get(i).getName();

            if (trackName.equals(targetTracksName)) {
                foundTargetTrack = true;                
            }
            else {
                i++;
            }
        }
        
        if (foundTargetTrack) {
            return i;
        }

        return -1;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.