By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General - Last java program of the semester need HALP!

seriously I'm in crunch time and I have the first half done now the second half is giving me fits

This is given:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.AudioClip;
import java.net.URL;

public class JukeBox extends JPanel
{
  private JList playList;
  private JList musicList;
  private JButton addButton, removeButton;
  private Song[] song;
  private PlayList list;
  private Song currentSong;
  private JButton stopButton, playButton;
  private AudioClip[] clip;
  private AudioClip currentClip;
  private String[] songNames;

  //-----------------------------------------------------------------
  //  Sets up the GUI for the juke box.
  //-----------------------------------------------------------------
  public JukeBox()
  {
    songNames = new String[0];
    song = new Song[6];
    song[0] = new Song ("Western Beat", "westernBeat.wav");
    song[1] = new Song ("Classical Melody", "classical.wav");
    song[2] = new Song ("Jeopardy Theme", "jeopardy.au");
    song[3] = new Song ("New Age Rhythm", "newAgeRythm.wav");
    song[4] = new Song ("Eighties Jam", "eightiesJam.wav");
    song[5] = new Song ("Alfred Hitchcock's Theme", "hitchcock.wav");

    list = new PlayList();

    JLabel titleLabel1 = new JLabel ("Play List");
    titleLabel1.setAlignmentX (Component.CENTER_ALIGNMENT);

    String[] songNames = {"Western Beat",
      "Classical Melody", "Jeopardy Theme", "New Age Rhythm",
      "Eighties Jam", "Alfred Hitchcock's Theme"};

    playList = new JList (songNames);

    //  Set up the buttons
    addButton = new JButton ("Add");
    addButton.setBackground (Color.white);
    addButton.setMnemonic ('a');
    removeButton = new JButton ("Remove");
    removeButton.setBackground (Color.white);
    removeButton.setMnemonic ('r');

    JPanel listButtons = new JPanel();
    listButtons.setLayout (new BoxLayout (listButtons, BoxLayout.X_AXIS));
    listButtons.add (addButton);
    listButtons.add (Box.createRigidArea (new Dimension(5,0)));
    listButtons.add (removeButton);
    listButtons.setBackground (Color.white);
    songNames = new String[0];
    clip = new AudioClip[0];
    JLabel titleLabel2 = new JLabel ("Juke Box");
    titleLabel2.setAlignmentX (Component.CENTER_ALIGNMENT);
    musicList = new JList();

    //  Set up the buttons
    playButton = new JButton ("Play");
    playButton.setBackground (Color.white);
    playButton.setMnemonic ('p');
    stopButton = new JButton ("Stop");
    stopButton.setBackground (Color.white);
    stopButton.setMnemonic ('s');

    JPanel playButtons = new JPanel();
    playButtons.setLayout (new BoxLayout (playButtons, BoxLayout.X_AXIS));
    playButtons.add (playButton);
    playButtons.add (Box.createRigidArea (new Dimension(5,0)));
    playButtons.add (stopButton);
    playButtons.setBackground (Color.white);

    //  Set up this panel
    setPreferredSize (new Dimension (200, 375));
    setBackground (Color.white);
    setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
    add (Box.createRigidArea (new Dimension(0,5)));
    add (titleLabel1);
    add (Box.createRigidArea (new Dimension(0,5)));
    add (playList);
    add (Box.createRigidArea (new Dimension(0,5)));
    add (listButtons);
    add (Box.createRigidArea (new Dimension(0,25)));
    add (titleLabel2);
    add (Box.createRigidArea (new Dimension(0,5)));
    add (musicList);
    add (Box.createRigidArea (new Dimension(0,5)));
    add (playButtons);
    add (Box.createRigidArea (new Dimension(0,5)));

    playList.addListSelectionListener (new ComboListener1());
    removeButton.addActionListener (new ButtonListener1());
    addButton.addActionListener (new ButtonListener1());
    musicList.addListSelectionListener (new ComboListener2());
    stopButton.addActionListener (new ButtonListener2());
    playButton.addActionListener (new ButtonListener2());
    currentSong = null;
    currentClip = null;
  }

  //*****************************************************************
  //  Represents the list selection listener for the play list.
  //*****************************************************************
  private class ComboListener1 implements ListSelectionListener
  {
    public void valueChanged (ListSelectionEvent event)
    {
      currentSong = song[playList.getSelectedIndex()];
    }
  }

  //*****************************************************************
  //  Represents the action listener for both control buttons.
  //*****************************************************************
  private class ButtonListener1 implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {
      if(playList.getSelectedIndex() < 0)
        return;
      if (event.getSource() == addButton)
        list.add(currentSong);
      else
        list.remove(currentSong);
      Song[] songs = list.getList();
      clip = new AudioClip[songs.length];
      currentClip = null;
      songNames = new String[songs.length];
      URL url = null;
      for(int i = 0; i < songs.length; i++)
      {
        try
        {
          url = new URL ("file", "localhost", songs.getFileName());
        }
        catch (Exception exception) {System.out.println("Exception caught");}
        clip = JApplet.newAudioClip (url);
        songNames = songs.getSongName();
      }
      musicList.setListData(songNames);
    }
  }
  //*****************************************************************
  //  Represents the list selection listener for the music list.
  //*****************************************************************
  private class ComboListener2 implements ListSelectionListener
  {
    //--------------------------------------------------------------
    //  Stops playing the current selection (if any) and resets
    //  the current selection to the one chosen.
    //--------------------------------------------------------------
    public void valueChanged (ListSelectionEvent event)
    {
      if(musicList.getSelectedIndex() < 0)
        return;
      if (currentClip != null)
        currentClip.stop();

      currentClip = clip[musicList.getSelectedIndex()];
    }
  }

  //*****************************************************************
  //  Represents the action listener for both control buttons.
  //*****************************************************************
  private class ButtonListener2 implements ActionListener
  {
    //--------------------------------------------------------------
    //  Stops the current selection (if any) in either case. If
    //  the play button was pressed, start playing it again.
    //--------------------------------------------------------------
    public void actionPerformed (ActionEvent event)
    {
      if (currentClip != null)
        currentClip.stop();

      if (event.getSource() == playButton)
        if (currentClip != null)
        currentClip.play();
    }
  }

  public static void main (String[] args)
  {
    JFrame frame = new JFrame ("Java Juke Box");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(new JukeBox());
    frame.pack();
    frame.setVisible(true);
  }
}

 

This is the first part which is done

//**************************************************************************
//Song.java
//
//By: Patrick Maxwell
//**************************************************************************

public class Song
    {
        private String songName;
        private String fileName;

        public Song(String Music1, String Music2)
        {
            songName = Music1;
            fileName = Music2;
        }

        public String getSongName()
        {
            return songName;
        }

        public String getFileName()
        {
            return fileName;
        }

        public boolean equals(Object Trombone)
        {
            if (Trombone == this)
            return true;
            if (!(super.getClass().equals(Trombone.getClass())))
            return false;
            Song localSong = (Song)Trombone;
            return ((songName.equals(localSong.getSongName())) && (fileName.equals(localSong.getFileName())));
        }

        public String toString()
        {
            return "Song name: " + getSongName() + " file name: " + getFileName();
        }
    }

 

Here is the instructions on the other half

Specifications for the PlayList class:    Code a public class named PlayList as described below. As always, you are not authorized to add, modify, or remove members of this class.

Data member:         

A private array of Songs named list.

Constructor:

A public default constructor that initializes list to an empty (zero elements) array of Songs.

Methods:

getList: a public method that returns an array of Songs and takes no parameters. It returns a deep copy of list. It creates a local array of Songs with the same number of elements as list, copies the elements from list into the local array, and returns the local array.

add: a public method that returns nothing and takes one Song parameter. If the song isn’t already in the play list (determined by calling the inList method described below) then:

A local array of Songs is declared and set to a new array of Songs containing room for the songs in list plus one more Song.

The Songs from list are copied into the local array.

The parameter Song is placed in the extra element.

The local array is assigned to list so that list now points to the new array containing the added Song.

Finally, a message containing the Song followed by " was added to the play list." is printed to the console.

If the parameter Song is already in the play list then a message containing the Song followed by " is already in the play list." is printed to the console.

remove: a public method that returns nothing and takes one Song parameter. If the song is in the play list (determined by calling the inList method described below) then:

A local array of Songs is declared and set to a new array of Songs containing room for one less Song than list contains.

Each Song in list is compared to the parameter Song and if the Songs aren’t equal (using the equals method) then the Song is copied into the local array. Note that you will have to manage separate indexes for list and the local array.

The local array is assigned to list so that list now points to the new array with the Song removed.

Finally, a message containing the Song followed by " was removed from the play list." is printed to the console.

If the parameter Song is not in the play list then a message containing the Song followed by " is not in the play list." is printed to the console.

inList: a public method that returns a boolean takes one Song parameter.

A boolean variable is declared and initialized to false.

Each Song in list is compared to the parameter Song and if they are equal (using the equals method) then the boolean variable is set to true. The comparisons should stop if a match is found.

Finally, the boolean variable is returned.

 

And I have a base outline for the above but it was done by the professor and A it doesn't work with what he gave us and B it's using things we've not learned

public class PlayList
{
  private Song[] list;

  public PlayList()
  {
    list = new Song[0];
  }

  public Song[] getList()
  {
    Song[] arrayOfSong = new Song[list.length];
    for (int i = 0; i < list.length; ++i)
      arrayOfSong = list;
    return arrayOfSong;
  }

  public void add(Song paramSong)
  {
    if (!(inList(paramSong)))
    {
      Song[] arrayOfSong = new Song[list.length + 1];

      for (int i = 0; i < list.length; ++i)
        arrayOfSong = list;
      arrayOfSong = paramSong;
      list = arrayOfSong;
      System.out.println(paramSong + " was added to the play list.");
    }
    else {
      System.out.println(paramSong + " is already in the play list.");
    }
  }

  public void remove(Song paramSong) {
    if (inList(paramSong))
    {
      Song[] arrayOfSong = new Song[list.length - 1];
      int i = 0;
      for (int j = 0; j < list.length; ++j) {
        if (list[j].equals(paramSong))
          continue;
        arrayOfSong = list[j];
        ++i;
      }
      list = arrayOfSong;
      System.out.println(paramSong + " was removed from the play list.");
    }
    else {
      System.out.println(paramSong + " is not in the play list.");
    }
  }

  private boolean inList(Song paramSong) {
    int i = 0;
    for (int j = 0; (j < list.length) && (i == 0); ++j)
      if (list[j].equals(paramSong))
        i = 1;
    return i;
  }
}

And I'm in a jam :( halp?



MaxwellGT2000 - "Does the amount of times you beat it count towards how hardcore you are?"

Wii Friend Code - 5882 9717 7391 0918 (PM me if you add me), PSN - MaxwellGT2000, XBL - BlkKniteCecil, MaxwellGT2000