Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Radio station JAVA wants a class to keep track of recordings it plays. Create

ID: 670692 • Letter: A

Question

a. Radio station JAVA wants a class to keep track of recordings it plays. Create a class named Recording that contains fields to hold methods for setting and getting a Recording’s title, artist, and playing time in seconds. Save the file as Recording.java .

b. Write an application that instantiates five Recording objects and prompts the user for values for the data fields. Then prompt the user to enter which field the Recordings should be sorted by — song title, artist, or playing time. Perform the requested sort procedure, and display the Recording objects. Save the file as RecordingSort.java

Explanation / Answer

import javax.swing.*;

public class JukeBox

{

//-----------------------------------------------------------------

// Creates and displays the controls for the juke box.

//-----------------------------------------------------------------

public static void main (String[] args)

{

JFrame frame = new JFrame ("Java Juke Box");

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JukeBoxControls controlPanel = new JukeBoxControls();

frame.getContentPane().add(controlPanel);

frame.pack();

frame.show();

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.AudioClip;

import java.net.URL;

public class JukeBoxControls extends JPanel

{

private JComboBox musicCombo;

private JButton stopButton, playButton;

private AudioClip[] music;

private AudioClip current;

//-----------------------------------------------------------------

// Sets up the GUI for the juke box.

//-----------------------------------------------------------------

public JukeBoxControls()

{

URL url1, url2, url3, url4, url5, url6;

url1 = url2 = url3 = url4 = url5 = url6 = null;

// Obtain and store the audio clips to play

try

{

url1 = new URL ("file", "localhost", "westernBeat.wav");

url2 = new URL ("file", "localhost", "classical.wav");

url3 = new URL ("file", "localhost", "jeopardy.au");

url4 = new URL ("file", "localhost", "newAgeRythm.wav");

url5 = new URL ("file", "localhost", "eightiesJam.wav");

url6 = new URL ("file", "localhost", "hitchcock.wav");

}

catch (Exception exception) {}

music = new AudioClip[7];

music[0] = null; // Corresponds to "Make a Selection..."

music[1] = JApplet.newAudioClip (url1);

music[2] = JApplet.newAudioClip (url2);

music[3] = JApplet.newAudioClip (url3);

music[4] = JApplet.newAudioClip (url4);

music[5] = JApplet.newAudioClip (url5);

music[6] = JApplet.newAudioClip (url6);

JLabel titleLabel = new JLabel ("Java Juke Box");

titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);

// Create the list of strings for the combo box options

String[] musicNames = {"Make A Selection...", "Western Beat",

"Classical Melody", "Jeopardy Theme", "New Age Rythm",

"Eighties Jam", "Alfred Hitchcock's Theme"};

musicCombo = new JComboBox (musicNames);

musicCombo.setAlignmentX (Component.CENTER_ALIGNMENT);

// Set up the buttons

playButton = new JButton ("Play", new ImageIcon ("play.gif"));

playButton.setBackground (Color.white);

playButton.setMnemonic ('p');

stopButton = new JButton ("Stop", new ImageIcon ("stop.gif"));

stopButton.setBackground (Color.white);

stopButton.setMnemonic ('s');

JPanel buttons = new JPanel();

buttons.setLayout (new BoxLayout (buttons, BoxLayout.X_AXIS));

buttons.add (playButton);

buttons.add (Box.createRigidArea (new Dimension(5,0)));

buttons.add (stopButton);

buttons.setBackground (Color.cyan);

// Set up this panel

setPreferredSize (new Dimension (300, 100));

setBackground (Color.cyan);

setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

add (Box.createRigidArea (new Dimension(0,5)));

add (titleLabel);

add (Box.createRigidArea (new Dimension(0,5)));

add (musicCombo);

add (Box.createRigidArea (new Dimension(0,5)));

add (buttons);

add (Box.createRigidArea (new Dimension(0,5)));

musicCombo.addActionListener (new ComboListener());

stopButton.addActionListener (new ButtonListener());

playButton.addActionListener (new ButtonListener());

current = null;

}

//*****************************************************************

// Represents the action listener for the combo box.

//*****************************************************************

private class ComboListener implements ActionListener

{

//--------------------------------------------------------------

// Stops playing the current selection (if any) and resets

// the current selection to the one chosen.

//--------------------------------------------------------------

public void actionPerformed (ActionEvent event)

{

if (current != null)

current.stop();

current = music[musicCombo.getSelectedIndex()];

}

}

//*****************************************************************

// Represents the action listener for both control buttons.

//*****************************************************************

private class ButtonListener 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 (current != null)

current.stop();

if (event.getSource() == playButton)

if (current != null)

current.play();

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote