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:
- Author - The author of the sound, a String.
- File name - The name of the sound file, a String.
- 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
- SoundPlayer class
- Create a new project named HW5.
- Create a new class named SoundPlayer, copy and paste the following.
- Copy some WAV files to the HW5 project directory;
download this test sound file.
- 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()); }
}
} |
- Test by creating a SoundPlayer object, it should play the sound
automatically if the sound file exists.
- Sound class

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

- Create a new class named Jpod.
- To use ArrayList and Iterator classes, add the
first two lines of Jpod exactly as:
import java.util.ArrayList;
import java.util.Iterator;
- Add playList field, an ArrayList that contains Sound
objects. Hint: ArrayList <Sound>
- Add a Jpod constructor that initializes the playList
field to an ArrayList object. Hint: new ArrayList <Sound>()
- 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)
- 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.
- 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).
- 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.
- Add an accessor method getSize that returns the size of the play
list.
- Test by:
- Create a Jpod instance
- Call the getSize method.
- 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.
- (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():
- Test by:
- Create a Jpod instance
- Call the indexPlayList method.
- 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:
- To call the printSound method using the Sound object:
- iter.next().printSound();
- Test by:
- Create a Jpod instance
- Call the iteratorPrintList method.
- (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:
- To call the playSound method using the Sound object:
- Test by:
- Create a Jpod instance
- Call the iteratorPlayList method.
- 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.
- 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.
- Create a HW5 folder in the Dropbox by selecting Add to the right of
your name:

- Select Create Folders from the list and enter the name HW5.
- Select the HW5 folder, Add and Upload files.

- Select Browse, locate the folder on your computer with HW5
files.

- From the file list, select the WAV and JAVA files, one-at-a-time, by
clicking Open.
- When finished select:
