Question: In this project, you will implement your own version of an ArrayList and use it to store Track objects that are songs played on an
In this project, you will implement your own version of an ArrayList and use it to store Track objects that are songs played on an MP3 player.
You will be provided with a fully functional MP3 player that uses the Java librarys ArrayList class. Your task is to get the MP3 player running exactly as given to you using your own implementation of an ArrayList.
The MP3Player.java file contains all code related to the GUI and playing of Mp3 files, as well as a main class that will test your data structure. You do not modify this file. In fact, you do not even need to understand all of the details of the Mp3Player.java file.
For your reference, I am using the javazoom mp3 decoder which is imported at the top of MP3Player.java in the following way: import javazoom.jl.player.advanced.*; // This is the MP3 decoder and player import java.io.FileInputStream; // This allows us to read files from disk
Setting up the jar files and the MP3s
The javazoom jar file and the MP3s are in zip file. You must extract this file to access the jar and MP3s. In File Explorer right click on the zip file and select Extract All. This will open the zip file and place all of the files in a new folder. You can now use those files in the instructions below.
Setting up the MP3 jar file
To run the player you must import the javazoom jar. For Eclipse, here are the instructions for adding the javazoom mp3 player to your classpath: Drag and drop the jar file into your /src folder of your project. Right-click the project and select Properties under the Java Build Path menu, select the Libraries button.
On the right-hand side, press the Add External JARs button and add the javazoom jar from your project source folder.
Placement of MP3s.
Place the MP3 files in the root project directory.
Placement of java files:
Place the java files in the src directory of the Java Project youve created for this project. Run MP3Player.java to run the music player.
Part 1 - MyArrayList.java (90 points)
Make sure to run the music player BEFORE you start writing code. This will give you a chance to test if the player works and to see the expected behavior.
Once the player is working as expected, you can start creating your own ArrayList for use in the MP3 player. Create a new Java File called, MyArrayList.java that implements the ListInterface interface provided to you. A description of what each method should do is found in the interface. The underlying encapsulated data structure in MyArrayList.java must be an array.
Once you have MyArrayList.java implemented, you can remove the ArrayList declaration and instantiation in Mp3Player.java and replace it with MyArrayList. Where this should be done is clearly marked in Mp3Player.java. You know you have most likely implemented the methods correctly if the program runs exactly the same with MyArrayList as it does with Javas ArrayList. We will run Mp3Player.java with your implementation as well as some additional tests to fully test the implementation.
ListInterface.java -
/**
* A generic collection of elements stored as a list
* @author "Your name"
*
* @param elements to store in list
*/
public interface ListInterface {
final int DEFAULT_CAPACITY = 5;
/**
* Adds an element to the end of the list, keeping the order of the list in tact
* Enlarge array when full
* @param t element to add
*/
public void add(T t);
/**
* Gets element at position
* @param pos in list to get element
* @return element at position
* @throws MyIndexOutOfBoundsException - if attempt to get an element outside of
* the current elements in the array. For example, if there are 2 elements in
* the array and attempt to get(3).
* @throws ArrayIndexOutOfBoundsException - if attempt to get element at a
* position larger than the array size.
*/
public T get(int pos) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException;
/**
* Finds and removes the first occurrence of the element from list, keeping the order of the list in tact
* For example, if list is 1,2,3,4 remove(2) will leave list is 1,3,4
* @param t element to remove
* @return true if item is successfully removed, otherwise return false
*/
public boolean remove(T t);
/**
* Removes element at given position, keeping the order of the list in tact
* For example is list is 1,2,3,4 remove(0) will leave list as 2,3,4
* @param pos
* @throws MyIndexOutOfBoundsException - if attempt to remove an element outside of
* the current elements in the array. For example, if there are 2 elements in
* the array and attempt to remove(3).
* @throws ArrayIndexOutOfBoundsException - if attempt to remove element at
* at position larger than the array size.
*/
public void remove(int pos) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException;
/**
* Inserts element at given position, keeping the order of the list in tact
* For example, if list is 1,2,3,4 add(0, 5) will leave list at 5,1,2,3,4
* @param pos position in list to add element
* @param t element to add
* @throws MyIndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
* @throws ArrayIndexOutOfBoundsException - if attempt to add element at
* at position larger than the array size.
*/
public void add(int pos, T t) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException;
/**
* Replaces element at given position in the list with the given element
* For example, for list 1,2,3,4 set(0,5) will leave list at 5,2,3,4
* @param pos position in list to replace element
* @param t element to set
* @throws MyIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
* @throws ArrayIndexOutOfBoundsException - if attempt to set element at
* at position larger than the array size.
*/
public void set(int pos, T t) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException;
/**
* Checks if element is in list
* @param t element to search for
* @return true if it is, otherwise returns false
*/
public boolean contains(T t);
/**
* Size of the list
* @return the number of elements in the list
*/
public int size();
/**
* Remove all elements from list
*/
public void clear();
/**
* Check if list is empty
* @return true if empty, false otherwise
*/
boolean isEmpty();
}
MP3.java
import javazoom.jl.player.advanced.*; import java.io.FileInputStream; import java.util.ArrayList; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame;
import java.awt.GridLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class Mp3Player extends JPanel implements ActionListener{ /* * THIS IS WHERE YOU MUST COMMENT OUT JAVA'S ARRAYLIST AND USE * YOUR OWN VERSION */ static ArrayList
songList = new ArrayList<>(); //static MyArrayList
songList = new MyArrayList<>(); private static final long serialVersionUID = 1L; static JButton play,pause,next,remove,addTrack; JLabel songname, songlist, artistName; PlayThread runnable; Thread t; static int selectedSong; Track selectedTrack; public Mp3Player() { selectedSong = 0; selectedTrack = new Track(); JPanel gl = new JPanel(new GridLayout(4,1)); setPreferredSize(new Dimension(300,350)); play = new JButton("Play"); play.setVerticalTextPosition(AbstractButton.CENTER); play.setHorizontalTextPosition(AbstractButton.LEADING); play.setActionCommand("play"); next = new JButton("Next"); next.setVerticalTextPosition(AbstractButton.BOTTOM); next.setHorizontalTextPosition(AbstractButton.CENTER); next.setActionCommand("next"); pause = new JButton("Stop"); pause.setActionCommand("pause"); pause.setEnabled(false); remove = new JButton("Remove"); remove.setActionCommand("remove"); addTrack = new JButton("Add Eminem to Playlist"); addTrack.setActionCommand("addTrack"); play.addActionListener(this); pause.addActionListener(this); next.addActionListener(this); remove.addActionListener(this); addTrack.addActionListener(this); selectedTrack = songList.get(selectedSong); songname = new JLabel(selectedTrack.getSongName()); songname.setPreferredSize(new Dimension(200,30)); String htmlString = "
PlayList "; for (int i=0;i Track ct = songList.get(i); htmlString += ct.toString()+" "; } htmlString = htmlString+"
Track.java
public class Track {
String songName;
String artist;
String mp3FileLocation;
public String getMp3() {
return mp3FileLocation;
}
public void setMp3(String mp3) {
this.mp3FileLocation = mp3;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String toString() {
return getSongName();
}
public boolean equals(Object o) {
if(o.toString().equalsIgnoreCase(getSongName()))
return true;
else
return false;
}
}
WHAT NEEDS TO BE EDITED
public class MyArrayListMP3 implements ListInterface {
protected final int DEFAULT_CAPACITY = 5;
protected T[] array ;
protected int TopIndex = -1;
MyArrayListMP3(){
array = (T[]) new Object ([DEFAULT_CAPACITY]);
}
@Override
public void add(T t) {
}
@Override
public T get(int pos) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException {
return null;
}
@Override
public boolean remove(T t) {
return false;
}
@Override
public void remove(int pos) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException {
}
@Override
public void add(int pos, T t) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException {
}
@Override
public void set(int pos, T t) throws MyIndexOutOfBoundsException, ArrayIndexOutOfBoundsException {
}
@Override
public boolean contains(T t) {
return false;
}
@Override
public int size() {
return 0;
}
@Override
public void clear() {
}
@Override
public boolean isEmpty() {
return false;
}
}
THE BASE CODE HAS BEEN GIVEN TO SHOW HOW THE PROGRAM SHOULD WORK. WHAT NEEDS TO BE DONE IS THE METHOD IMPLEMENTATION IN THE MYARRAYMP3. The only thing that needs to be edited is after the bold part. The other parts are just used to help with implementation
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
