user_01 user_01 - 1 month ago 5
Java Question

How to get number of different words in Java?

I am working with Musical Jukebox program. I have two main classes: Song.java and Playlist.java. The Song.java is as follows:

public class Song {


String name;
String title;
double length;

public Song(String name,String title,double length)
{
this.name=name;
this.title=title;
this.length=length;
}

public void setArtist(String songname)
{
name=songname;
}

public String getArtist()
{
return name;

}

public void setTitle(String songtitle)
{
title=songtitle;
}

public String getTitle()

{
return title;
}

public void setLength(double songlength)
{
length=songlength;
}

public double getLength()

{
return length;
}
public String toString()
{
return "Title: " + getTitle() + ", Artist: " + getArtist()
+ ", Track Length: " + getLength();
}


And Playlist.java is as follows:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")
public class Playlist<E extends Song> extends java.util.Vector<E> {
java.util.Iterator<E> itr = this.iterator();

String name;
ArrayList<Song> songList;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public ArrayList<Song> getSongList() {
return songList;
}

public void setSongList(ArrayList<Song> songList) {
this.songList = songList;
}

public void PlayList() {
name = "Untitled";
songList = new ArrayList<Song>();
}

public Playlist(String name) {
this.name = name;
songList = new ArrayList<Song>();
}

public Object getTitle()
{

return "Playlist Title";
}

public boolean addtoPlist(Song song1) {

songList.add(song1);
return true;


}



public Song getSong(int index) {
songList.trimToSize();
if(songList.size() >= index){

return songList.get(index);
}
else
return null;
}

public boolean hasTitle(String string) {
if( string.equals("Playlist Title"))
return true;
return false;
}

public boolean hasArtist(String string) {
if(string.equalsIgnoreCase("artist1"))
return true;
return false;
}

public Object numberOfSongs() {
// TODO Auto-generated method stub
return songList.size();
}

public Object numberOfArtists()
{
return 0;

}

public Object numberOfTitles()
{
return null;

}

public double playTime() {

return 0;
}

public Object findSong(Song song1) {

if(song1.equals("song1")&&song1.equals("song2")&&
song1.equals("song3")&&song1.equals("song4"))
itr.next();
return true;


}
public void sortByArtist()
{

}

public boolean removeFromPlist(Song str) {

songList.remove(str);
return true;

}





}


And this is playlisttest.java for unit testing:

import junit.framework.TestCase;
import java.util.Vector;

public class PlaylistTest extends TestCase {

private Playlist<Song> aPlaylist;
private Song song1, song2, song3, song4, duplicate_song, nullSong;

public void setUp() {
aPlaylist= new Playlist<Song>("Playlist Title");
song1 = new Song("Artist1", "AA", 6.00);
song2 = new Song("Artist1", "BB", 3.50);
song3 = new Song("Artist2", "BB", 3.00);
song4 = new Song("Artist2", "CC", 5.50);
duplicate_song = new Song("ARTIST1", "TITLE1", 5.00); // Same song with song 1
nullSong = null;
}

protected void tearDown() throws Exception
{
super.tearDown();
}

protected void fillPlaylist() {
aPlaylist.addtoPlist(song1);
aPlaylist.addtoPlist(song2);
aPlaylist.addtoPlist(song3);
aPlaylist.addtoPlist(song4);
}

public void test_Constructor() {
assertNotNull(aPlaylist);
assertTrue(aPlaylist instanceof Vector);
assertTrue(aPlaylist.isEmpty());
}

public void test_getTitle() {
assertTrue(aPlaylist.getTitle().equals("Playlist Title"));
}

public void test_addtoPList() {
assertTrue(aPlaylist.isEmpty());

assertTrue(aPlaylist.addtoPlist(song1));
assertEquals(1, aPlaylist.size());

assertTrue(aPlaylist.addtoPlist(song2));
assertTrue(aPlaylist.addtoPlist(song3));
assertEquals(3, aPlaylist.size());
assertFalse(aPlaylist.addtoPlist(nullSong));
assertEquals(3, aPlaylist.size());

assertFalse(aPlaylist.addtoPlist(duplicate_song));
assertEquals(3, aPlaylist.size());
}

public void test_removeSong() {
fillPlaylist();
int size = aPlaylist.size();

assertFalse(aPlaylist.removeFromPlist(nullSong));
assertEquals(size, aPlaylist.size());

assertFalse(aPlaylist.removeFromPlist(new Song("Artist1", "Title1", 1.00)));
assertEquals(size, aPlaylist.size());

assertTrue(aPlaylist.contains(duplicate_song));
assertTrue(aPlaylist.removeFromPlist(duplicate_song)); // Removing "duplicate_song" is removing "song1"
assertEquals(size - 1, aPlaylist.size());
}

public void test_getSong() {
fillPlaylist();
assertTrue(aPlaylist.getSong(0) instanceof Song);

assertEquals(song1, aPlaylist.getSong(0));
assertEquals(duplicate_song, aPlaylist.getSong(0));
assertEquals(song2, aPlaylist.getSong(1));
assertEquals(song3, aPlaylist.getSong(2));
assertEquals(song4, aPlaylist.getSong(3));
}

public void test_hasTitle() {
fillPlaylist();
assertTrue(aPlaylist.hasTitle("Playlist Title"));
assertFalse(aPlaylist.hasTitle("wrong title"));
}

public void test_hasArtist() {
fillPlaylist();
assertTrue(aPlaylist.hasArtist("artist1"));
assertFalse(aPlaylist.hasArtist("wrong artist"));
}

public void test_numberOfSongs() {
fillPlaylist();
assertEquals(4, aPlaylist.numberOfSongs());
}

public void test_numberOfArtists() {
fillPlaylist();
assertEquals(2, aPlaylist.numberOfArtists());
}
public void test_numberOfTitles() {
fillPlaylist();
assertEquals(3, aPlaylist.numberOfTitles());
}

public void test_playTime() {
fillPlaylist();
assertTrue(aPlaylist.playTime() == 19.00);
}

public void test_findElement() {
fillPlaylist();
assertEquals(0, aPlaylist.findSong(song1));
assertEquals(1, aPlaylist.findSong(song2));
assertEquals(2, aPlaylist.findSong(song3));
assertEquals(3, aPlaylist.findSong(song4));
//assertEquals(-1, aPlaylist.findSong(new Song("Not", "There", 0)));
}

public void test_sortByArtist() {
// TODO: Assignment 6 -- create new test case here: sort by artist

}

public void test_sortByTitle() {


}
}


I would like to implement numberOfTitles method that retrieves number of different titles but I really do not know how to take different titles and return the count (which is 3 in our case as it could be seen).If possible could you help me to solve this please? Your help is greatly appreciated!

Answer

You can store the unique titles in a Set and get the size of this once you're done:

Set<String> uniqueSongs = new Set<>();
for (Song song : songList)
{
    uniqueSongs.add(song.getTitle());
}

Then you can use uniqueSongs.size() to get the number of unique songs.

You can also use streams if you're in Java 8 (thanks to Boris the Spider for pointing it out):

Set<String> uniqueSongs = songList.stream().map(Song::getTitle).collect(Collectors.toS‌​et())
Comments