Homework 5
Grouping Objects

Modified

Overview - The purpose of the homework is to practice grouping objects by implementing three classes: a SoundPlayer that plays a sound (given below), a Sound class that defines a single sound track; a Jpod class that defines a group of sounds. A fourth class, JpodSkin, creates a simple, graphical user interface to the Jpod.

Problem description - A sound player plays a single sound file. A single sound has three parts:

  1. Author - The author of the sound, a String.
  2. File name - The name of the sound file, a String.
  3. Duration - The duration in seconds of the sound, an int.

For example, the author "2U" with a sound stored as filename "ICanSqueal.wav" of duration 3000 seconds.

A Jpod is a grouping of an arbitrary number of sounds. We will first implement and test a Sound class, then implement a Jpod class to group individual sounds.

Assignment

  1. SoundPlayer class
    1. Create a new project named HW5.
    2. Create a new class named SoundPlayer, copy and paste the following.
    3. Copy some WAV files to the HW5 project directory; download this test sound file.
    4. SoundPlayer will play the test sound given the file name and duration by:

          new SoundPlayer("HW5.wav", 3);
         

      import java.applet.*;
      import java.io.*;
      import java.net.*;

      public class SoundPlayer
      {
         public SoundPlayer(String fileName, int duration)
         {
             AudioClip testClip;
             URL testUrl;
             System.out.println("Playing " + fileName);
             try{
                  testUrl=new URL("file:" + new File(".").getCanonicalPath() + "/" + fileName);
                  testClip=Applet.newAudioClip(testUrl);
                  testClip.play();
                  Thread.sleep(duration*1000);
                  testClip.stop();
            } catch(Exception e){ System.out.println(e.toString()); }
         }
      }
    5. Test by creating a SoundPlayer object, it should play the sound automatically if the sound file exists.

     

  2. Sound class
    1. Create a new class named Sound.
    2. Add three fields named and typed as:
      1. author - a String for the author.
      2. fileName - a String for the sound file.
      3. duration - an int for the sound duration.
    3. Add a constructor with parameters to initialize all three fields. Use the constructor signature similar to:
      • public Sound(String newAuthor, String newFileName, int newDuration)
    4. Add accessor method getDuration to return the value of duration.
    5. Add accessor method getAuthor to return the value of author.
    6. Add a method printSound to print the sound similar to:
       
      • Author: 2U File Name: ICanSqueal.wav Duration: 3000
         
    7. Add a method playSound to play the sound.
    8. Test by:
      1. Create a Sound instance,
      2. print the sound information,
      3. call accessor methods to return the values of author and duration.
      4. play the sound.

       

  3. Jpod class
    1. Create a new class named Jpod.
    2. To use ArrayList and Iterator classes, add the first two lines of Jpod exactly as:

      import java.util.ArrayList;
      import java.util.Iterator;

    3. Add playList field, an ArrayList that contains Sound objects. Hint: ArrayList <Sound>
    4. Add a Jpod constructor that initializes the playList field to an ArrayList object. Hint: new ArrayList <Sound>()
    5. Add a mutator method addSound that adds a sound to the play list. The addSound method has one parameter of class Sound.

      Test by:

      • Create two Sound objects, for example: sound1 and sound2.
      • Create a Jpod object, for example: jpod1.
      • Add the two sounds to the play list using the addSound. For example, jpod1.addSound(sound1)
         
    6. To speed up the testing process we'll automate creating sounds and adding to the play list. Add the following mutator method createPlayList that creates and adds three sounds to the play list. You can copy and paste. Change the file names to your own.
      public void createPlayList() {
          Sound s;
          s = new Sound("2U", "ICanSqueal.wav", 3);
          addSound(s);
          s = new Sound("Elvis", "ICanMakeUSqueal.wav", 4);
          addSound(s);
          s = new Sound("Rain", "Sleep.wav", 2);
          addSound(s);
      }
      • Test by creating a Jpod instance and calling the createPlayList method. Inspect the Jpod instance down several levels until you reach a Sound object.

      • Add a call to createPlayList() at the end of the Jpod constructor to automatically initialize a Jpod with a list of sounds for testing.
         
    7. Add a mutator method removeSound that removes a sound from the play list. The removeSound method has one parameter of type int, the index of the sound to remove.

      Test by:

      • Create a Jpod object, for example: jpod1.
      • Call jpod1.removeSound(0).
      • Call jpod1.removeSound(20).
         
    8. Add an accessor method getSound to return the Sound object at an index specified in the method parameter. The method has one int parameter.
      • For example, for the Jpod object jpod1:

        jpod1.getSound(2)

      • Test by:
        • Create a Jpod instance.
        • Call the getSound method.

       

    9. Add an accessor method getSize that returns the size of the play list.
      • Test by:
        • Create a Jpod instance
        • Call the getSize method.

       

    10. Add an indexPrintList method to print all sound information in the play list by indexing through the play list. Use the Sound class printSound method to perform printing.
      • Use the getSound method to access a Sound object from the play list.
        • To call the printSound method using the Sound object returned by getSound():
          • getSound(2).printSound();
             
      • Test by:
        • Create a Jpod instance
        • Call the indexPrintList method.
           
    11. (Extra credit) Add an indexPlayList method to play all sound information in the play list by indexing through the play list. Use the Sound class playSound method to perform playing.
      • Use the getSound method to access a Sound object from the play list.
        • To call the playSound method using the Sound object returned by getSound():
          • getSound(2).playSound();
             
      • Test by:
        • Create a Jpod instance
        • Call the indexPlayList method.
           
    12. Add an iteratorPrintList method to print all sound information in the play list by iterating through the play list. Use the Sound class printSound method to perform printing.
      • Note that an object stored in an ArrayList must be cast as a Sound object.
        • To retrieve the next object from iterator iter:
          • iter.next()
        • To call the printSound method using the Sound object:
          • iter.next().printSound();
      • Test by:
        • Create a Jpod instance
        • Call the iteratorPrintList method.
           
    13. (Extra credit) Add an iteratorPlayList method to play all sound information in the play list by iterating through the play list. Use the Sound class playSound method to perform playing.
      • Note that an object stored in an ArrayList must be cast as a Sound object.
        • To retrieve the next object from iterator iter:
          • iter.next()
        • To call the playSound method using the Sound object:
          • iter.next().playSound();
      • Test by:
        • Create a Jpod instance
        • Call the iteratorPlayList method.
           
    14. Add the method totalDuration that returns the sum of all the sound duration in the play list. The result is of type int.
      • Call the Sound class accessor method getDuration for each sound in the play list.
      • Test by:
        • Create a Jpod instance
        • Call the totalDuration method.
           
    15. Just for fun, add a basic skin graphical interface for your Jpod.
      • Create a new class name JpodSkin; copy and paste the following.
      • Create a JpodSkin object, click on the author's name in the JpodSkin window to play the sound.
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class JpodSkin
      {
         public JpodSkin()
        {
           JFrame frame = new JFrame("JpodSkin");
           final Jpod jpod=new Jpod();
           final JList playList;

           JPanel contentPane = (JPanel)frame.getContentPane();
           String [] data=new String[jpod.getSize()];
           for (int i=0; i<jpod.getSize(); i++) data[i]=(i+1) + " " + jpod.getSound(i).getAuthor();
           playList=new JList(data);

           MouseListener mouseListener = new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
               int index = playList.locationToIndex(e.getPoint());
               jpod.getSound(index).playSound();
             }
           };
           playList.addMouseListener(mouseListener);
           contentPane.add(playList, BorderLayout.CENTER);
           frame.pack();
           frame.setVisible(true);
        }
      }

Turn in

From the View menu, Show Terminal, then Record method calls. Clear the window before starting.

Repeat the tests of j-n.

Copy and paste the Terminal window to a Notepad or Word file in the HW5 project directory, name the file HW5.
 

C201 OnCourse Dropbox - upload the HW5 project directory Terminal window file, and the WAV and JAVA files to the C201 OnCourse dropbox as HW5.

  1. Create a HW5 folder in the Dropbox by selecting Add to the right of your name:
  2. Select Create Folders from the list and enter the name HW5.
  3. Select the HW5 folder, Add and Upload files.
  4. Select Browse, locate the folder on your computer with HW5 files.
  5. From the file list, select the WAV and JAVA files, one-at-a-time, by clicking Open.
  6. When finished select: