Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a musicPlayer java programming homework public class MusicPlayer { public musicPlayer(int givenNumCategories, int givenCategorySize,String givenFilename) { } /** * Sets the playlist to

I have a musicPlayer java programming homework

public class MusicPlayer {

public musicPlayer(int givenNumCategories, int givenCategorySize,String givenFilename) {

}

/**

* Sets the playlist to the given playlist number. If negative, the playlist number is set to 1,

* and if greater than the number of playlists, the station is set to n, where n is the number of stations.

* The method also updates the category number to the category in which the new playlist is a part of.

Parameters:

givenPlaylistNumber - given playlist number to be set

* @param givenPlaylistNumber

*/

public void setPlaylist(int givenPlaylistNumber) {

}

/**

* Sets the music player to the next playlist number after the current one within a category,

wrapping around to playlist 0 when the current playlist is n-1 (where n is

the number of playlists within a category). eg:

If category number is 0 and there are 6 playlists in each category,

then a nextPlaylist() from playlist 5 should be playlist 0.

*/

public void nextPlaylist() {

}

/**

* Sets the music player to the previous category number before the current one,

wrapping around to category n-1 when the current category is 0 (where n is the number of categories).

Once it selects a category, it then selects a random playlist from that category (similar to setPlaylistFromCategory() method). eg:

If number of categories is 6, then a previousCategory() from category 0 should be category 5.

And the new playlist will be a random playlist in category 5.

*/

public void previousCategory() {

}

/**

* Sets the music player to the next category number before the current one,

wrapping around to category 0 when the current category is n-1 (where n is the number of categories). Once it selects a category,

it then selects a random playlist from that category (similar to setPlaylistFromCategory() method). eg:

If number of categories is 6, then a nextCategory() from category 5 should be category 0. And the new playlist will be a random playlist in

*/

public void nextCategory() {

}

/**

* The method updates the instance variable for category and then updates the current playlist.

The current playlist number is a random number from the range of playlists within a category.

eg: If category is 1, and the size of each category is 6, the playlist range will be 0 to 5 (both inclusive),

and the current playlist number will be any number between 0 and 5.

Parameters:

givenCategory - category to select from

* @param givenCategory

*/

public void setPlaylistFromCategory(int givenCategory) {

}

/**

* The method updates the instance variable for current playlist and then updates the current category.

The current category number is the category which contains the range of playlists

in which the given playlist belongs. eg: If playlist is 4, and the size of each category is 6,

then the playlist will belong to range 0 to 5, and the category number will be 0.

Parameters:

givenPlaylist - playlist to select from

* @param givenPlaylist

*/

public void setCategoryFromPlaylist(int givenPlaylist) {

}

some more informations

Selecting Playlists from Categories

in each category, there are a possible S number of categories represented as integers 0 through P-1.

For simplicity, we assume that:

the total number of categories is a given number N and are numbered 0 through N - 1,

In each of the N categories, there are C number of playlists, and

the current playlist is a random number P from each category.

You can select the range in each category by calculating the starting playlist number in that category as (N*C) and the ending playlist number as ((N+1)*C)-1. For example, for category 1 and size as 6, the range should be between 6 and 11.

Once you implement the basic operations, try the next steps:

Implement the "wrap around" behavior at the endpoints in the nextCategory and

previousCategory methods. That is, if you are at category 0 and attempt to go previous, it goes to category N - 1, and if you are at category N - 1 and attempt to go next, you'll end up at category 0. Selecting a playlist from that category can simply be done by calling thesetPlaylistFromCategory method.

The nextPlaylist and previousPlaylist is implemented the same way. But the twist is: wrap around only happens within a category.

Next, generate category from playlist name. This should be fairly simple. Just check which range the playlist lies in and select that category.

Lastly, generate a random playlist within a category. If you have figured out the wrap around for the playlist methods, this should be implemented the same way.

Where's the main() method?

Like most Java classes, this isn't a complete program and you can't "run" it by itself. It's just a single class, that is, the definition for a type of object that might be part of a larger system. To try out your class, you can write a test class with a main method such the example below.

Sample usage

A good way to think about the specification is to try to write some simple test cases and think about what behavior you expect to see.

This program needs a playlist file as an input in order to run. The input file is simply a list of playlist names. Each column represents a different category and each row represents the playlists in that category. Remember, each row must have the same number of strings. For example, a file calledfile.txt will contain the following:

the input files

PinkFloyd RHCP ACDC Radiohead TheBeatles TheRollingStones BobDylan SimonAndGarfunkel Avicii Skrillex Deadmau5 Tiesto Rihanna Beyonce KatyPerry TaylorSwift Passenger Coldplay JohnMayer Hozier

Here are a few examples:

public class MusicPlayerTest { 
 public static void main(String[] args) { 

MusicPlayer r = new MusicPlayer(5, 4, file.txt);

// setting playlists and categories System.out.println(r.getPlaylist()); // expected PinkFloyd (playlist 0) r.setPlaylist(19); System.out.println(r.getPlaylist()); // expected Hozier (playlist 19) r.nextPlaylist(); r.nextPlaylist(); System.out.println(r.getPlaylist()); // expected Coldplay (playlist 17) r.previousPlaylist(); System.out.println(r.getPlaylist()); // expected Passenger (playlist 16) System.out.println(r.getCategory()); // expected 0 r.setCategory(4); System.out.println(r.getCategory()); // expected 4

// playlist calculations from category r.setPlaylistFromCategory(2); System.out.println(r.getPlaylist()); // expected name of any playlist between

8 and 11 (Avicii Skrillex Deadmau5 Tiesto).

System.out.println(r.getCategory()); // expected 2

// category calculations from playlist r.setCategoryFromPlaylist(2); System.out.println(r.getCategory()); // expected 0 System.out.println(r.getPlaylist()); // expected 2

} }

Now, create the constructor and initialize all the instance variables to either the given parameters or the default value of 0 or 0.0 or null, as required.

Before you write code for a method, always write a simple usage example or test case, similar to themain method shown above. This will make sure you understand what the code is really supposed to

You might start with the basic accessors.

For example, look at getCategory(). It's an accessor that returns the current category. That information has to be stored somehow within the MusicPlayerobject, which tells you that you probably need an instance variable to represent the category number. Then setCategory() is also simple to write. Just set your instance variable to the parameter passed into the method. Note there is a requirement that if someone tries to set a negative category number, it gets set to category 0. You can do this easily using Math.max to select the larger of two values, for example

int newCategory = Math.max(0, category); Likewise, use Math.min to handle the possibility of category that is more than N - 1. Now, once you set a category, select the current playlist as a random playlist from that category by using thesetPlaylistFromCategory method. Similarly, implement the getters and setters for volume and playlist. The only additional step in the getPlaylist() method is that instead of directly returning the playlist number P, it searches the input file and returns the name that is in the Pth row of the file.

For nextCategory() and previousCategory() you're just adding or subtracting 1, but after doing that, you need to make it wrap around to the first category or last category.

For nextPlaylist() you're also adding 1, but after adding, you need to make it wrap around to the first playlist in that category. To do this, it is very helpful if you first compute the initial and final playlist numbers in the current category. For example, if each category has 4 playlists, category 1 will have playlists between 4 and 7. Now, suppose youre in playlist 7. If you add 1 to the playlist, it should be in playlist 8. But since category 1 only has playlists between 4 and 7, youll need to wrapit around to playlist 4. Similarly, for previousPlaylist() if youre in playlist 4 and you go to theprevious playlist, you should be in playlist 7. Carefully do the calculation by hand first. Your code can just imitate the way you do it by hand.

Next, try implementing setCategoryFromPlaylist(). The category number of a playlist can be calculated using the same range as you did for the wrap around. You can find the initial and final playlist number in that category and then update the category number corresponding to that range. For example, if you are given playlist 5 as input. If each category has 4 playlists, then 5 will be a part of the playlists ranging between 4 and 7, which will be in category 1.

Finally, try implementing setPlaylistFromCategory(). The playlist number in a category can also be calculated using the same range as you did for the wrap around. You can set the initial and final playlist number in that category and then generate a random number between that range. For example, if each category has 4 playlists, category 1 will have playlists between 4 and 7. Now, use the Random class to generate an integer between 4 and 7.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

ISBN: 3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

1. Describe how cultures differ in time perspective.

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago

Question

Define co-ordination?

Answered: 1 week ago