package examples; import java.util.*; // needed for Date import java.text.*; // needed for DateFormat /** * CS401 Example MusicCD * * Simple class to represent a music CD. As we discussed in Lecture, if we want * to represent a general CD (could contain data or music or some other media) * the class would be more complex. The class here is very simple -- containing * only a single constructor and a toString() method. As an exercise you may * want to add some additional constructors, accessors and mutators. See * CDTest.java for a simple test program. If you add to this class you can also * add to CDTest.java to demonstrate the new features. * * @author Dr. Ramirez, PJ Dillon */ public class MusicCD { // As we also discussed, sometimes we want to give a restricted // set of values to an instance variable. One way to do this is we make a // static (part of the class, not an object) array listing the possible // values. This variable is shared among all objects (saving memory) and is // also accessible directly through the class, even if no objects are // declared. See how it is accessed in below and in CDTest.java. // NOTE: Static variables are not necessarily public. I have made this // one public to show how it can be accessed through the class, as // shown in CDTest.java public static String [] music_genres = {"Popular", "Rock", "Jazz", "Country", "R&B", "Classical", "Other"}; private String title; private String artist; private int genre; private Date releaseDate; // We are storing the release date as a // Date object. This will allow us to manipulate it // as a Date if we so choose. Recall that this could be // stored in various ways int tracks; private String[] trackTitles; private int length; // in seconds /** * Constructs a new CD from the given title, artist, type, release date, track * count, and length parameters. * * @param t * String title of the CD * @param a * String artist of the CD * @param t2 * int genre of the CD that corresponds to the index of the\ * corresponding genre in the music_genres array. * @param d * release date of the CD. This must be formatted such that it can be * parsed by the SHORT format of the DateFormat class * @param tr * track count * @param l * length in seconds of the CD */ public MusicCD(String t, String a, int g, String d, int tr, String[] tt, int l) { title = new String(t); artist = new String(a); if (g >= 0 && g < music_genres.length) genre = g; else genre = music_genres.length-1; // Code below is used to convert a String representation of a // date (ex: 10/21/2004) into a Date object. Don't worry too // much about the details at this point (i.e. the // DateFormat class and the try - catch block). We may cover // these later. DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); try { releaseDate = df.parse(d); } catch (ParseException e) { releaseDate = null; } tracks = tr; length = l; trackTitles = new String[tt.length]; for(int i = 0; i < tt.length; i++) trackTitles[i] = tt[i]; } public String toString() { StringBuffer S = new StringBuffer(); S.append("CD: " + title + "\n"); S.append("Artist: " + artist + "\n"); S.append("Genre: " + music_genres[genre] + "\n"); // As we discussed in lecture last week, it is good to set reference // variables to null if they are not referring to a valid object. // That way we can still test the reference. if (releaseDate != null) { S.append("Release Date: " + releaseDate.toString() + "\n"); } else { S.append("No release date \n"); } // Even though the length is stored as a single integer, users don't // typically want to see it represented that way. Thus, we instead // show it as minutes and seconds. int min = length / 60; int sec = length % 60; S.append("Number of tracks: " + tracks + "\n"); S.append("Length: " + min + " min. " + sec + " sec. \n"); return S.toString(); } public int compareTo(Object r) { MusicCD rhs = (MusicCD) r; return (this.title.compareTo(rhs.title)); } }