import java.util.*; // access class ArrayList /** * CompactDisc describes the contents of a Compact disk consisting of * zero or more songs. CSE142 Su01 lecture example. * * @author Hal Perkins * @version 7/15/01 */ public class CompactDisc { // instance variables private String title; // CD title private ArrayList songs; // songs on this CD in order private int totalSeconds; // total length of songs on this CD in seconds /** * Construct an empty CompactDisc with the given title * @param title CD title */ public CompactDisc(String title) { this.songs = new ArrayList(); this.title = title; this.totalSeconds = 0; } /** * Add a new song at the end of the list of songs in this CompactDisc * @param song new song to be added */ public void add(Song song) { this.songs.add(song); this.totalSeconds = this.totalSeconds + song.getSeconds(); } /** * Return Song at given position in this CompactDisc * @param pos Song position in CompactDisc (numbered from 0) * @return Song at the specified position */ public Song get(int pos) { return (Song)this.songs.get(pos); } /** * Return title of this CompactDisc * @return CompactDisc title */ public String getTitle() { return this.title; } /** * Return total length of the songs in this CompactDisc * @return total length of songs in seconds */ public int getSeconds() { return this.totalSeconds; } /** * Print the titles of the songs on this CompactDisc on System.out */ public void printTitles() { Iterator songIter = this.songs.iterator(); while (songIter.hasNext()) { Song currentSong = (Song)songIter.next(); System.out.println(currentSong.getTitle()); } } /** * Return String description of this CompactDisc * @return String containing CompactDisc title, number of songs, * and total time in seconds */ public String toString() { return "CompactDisc: title = " + this.title + " with " + this.songs.size() + " songs with total time of " + this.totalSeconds + " seconds."; } }