Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I complete all of these tasks in Java? Introduction Nick and Norah are going to catch a concert, and on the way, they

How would I complete all of these tasks in Java?

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Introduction Nick and Norah are going to catch a concert, and on the way, they have a playlist to listen to together on Norah's awesome Zune player. Norah's player has a wonderful feature, a playlist that never ends. Your task is to implement Norah's playlist in a data structure. With 30 GB of storage, Norah's Zune player can hold an incredible 6000 songs (it was incredible for the year 2006). But you need a playlist for every mood. The Zune allows Norah to create an infinite playlist of songs. - Infinite playlists have no beginning and no ending. The songs just keep playing in a circular loop. - You can easily move from the current song to the next song, or from the current song to the previous song, because you might want to hear it again. - You can quickly add songs to the playlist. Classes Your job is to implement these classes in Song.java, InfinitePlaylist.java and Songlterator.java. The playlist holds songs, therefore the item in the playlist is of class Song. A Song has these public methods: - Song(String title, String[] artists, int durationSeconds) - The constructor creates a song object. - String gettitle() - Accessor for the title - String[] getArtists() - Accessor for the list of artists - int getDurationSeconds () - Accessor for the duration of the song InfinitePlaylist A playlist holds a list of songs. The class is named InfinitePlaylist. Here are the public methods for InfinitePlaylist. - InfinitePlaylist(String title) -- Create a playlist with the given title - String getTitle() - Return the title of the playlist - int size () -- Return the number of songs in the playlist - Song play( ) - Return the current song, and then move the current song to the next item in the list (plays current song, then moves to the next song) - void add(Song song) - Add a new song to be the next song The playlist works with an iterator interface called Songlterator, so that the user can move forward or backwards through the list without changing the currently playing song. The playlist exports these public methods that use an iterator. - SongIterator iterator() - This returns an instance of a Songlterator starting from the current song. - void reset(SongIterator it) - Set playback to start from the song pointed to by the song iterator - void remove(SongIterator it) - Remove the song pointed to by the iterator. If the current song is pointed to by the iterator, set it to the next song, or null if there are no more songs. Songlterator The Songlterator interface represents an iterator that allows you to move through the list from the current point. The Zune uses this to allow you to move through songs forwards and backwards. The iterator's abstract methods are: - Song current() -- Get the current song that the iterator is pointing to - void next() - Advance the iteration - void previous ( ) - Move backwards in the iteration Since the iterator is defined as an interface in Songlterator.java (provided for you), you have to provide a concrete implementation of this interface for your iterator. You can read all about Java interfaces here: - https://www.tutorialspoint.com/java/java interfaces.htm - https://docs.oracle.com/javase/tutorial/java/concepts/interface.html - https://www.w3schools.com/java/java interface.asp Provide your own concrete class that implements Songlterator. Create your iterator implementation as a (non-public) class inside InfinitePlaylist.java. Call it anything you want. Stylistically, a lot of people append "Impl" to their classes that implement an interface, so you could call it SongIterator Impl, for example. The Zune is a low powered device, and we want to conserve battery as much as possible. So your implementation has to be as efficient as possible. Each of the above operations must occur in constant O(1) time. Data Structure The data structure you want to use is a circular linked list, illustrated in the following diagram. The InfinitePlaylist has a pointer to the current song, held in a SongNode object. If there are no songs, then this should point to null. A SongNode object holds a Song, and two pointers. One goes to the previous SongNode, and one goes to the next SongNode. The example shows an infinite playlist with three songs. As you can see from the diagram, the list never ends -- it is like a snake eating its own tail. All SongNodes are joined in a circular fashion. The Songlterator allows you to look ahead (or look behind). It also holds a pointer to a SongNode, but it may be a different SongNode than the current song. Compiling and Testing This project comes with a Makefile. It allows you to use a program called "make" that helps you build and test your code. The Makefile expects three files: - Song.java - InfinitePlaylist.java - Songlterator.java The test file is located in the Test directory, and is called TestlnfinitePlaylist.java. The test framework uses Unit, which is how software tests are automated in Java. All tests are prefixed with a "@Test" annotation to signify that it is a test. The junit4.jar file is a Java Archive that contains the JUnit library. You don't really have to worry about this right now. You do have to test your code! You can test your files by typing: make test This will compile the tests and run it against all your code. These are the same tests that will run in the autograder. Casting This homework will require the use of an operation called casting. You can read about it here: - https://www.baeldung.com/java-type-casting - https://www.geeksforgeeks.org/class-type-casting-in-javal - https://www.wideskills.com/java-tutorial/java-object-typecasting Casting is the process of converting one type to another. In the homework, the InfinitePlaylist. remove(SongIterator it) method takes a Songlterator as an argument. That's great, except that Songlterator is an interface. You really need the class that implements the Songlterator, which is Songlteratorlmpl. However the argument is of type Songlterator. So how do we change a Songlterator to a Songlteratorlmpl? Inside the remove function, we can cast it, like this

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

Students also viewed these Databases questions

Question

What are negative messages? (Objective 1)

Answered: 1 week ago