Write a class TrackListException that extends Exception with the following const
ID: 3686111 • Letter: W
Question
Write a class TrackListException that extends Exception with the following constructors: A default constructor that gives the error message "Too many tracks." A constructor that takes a string and uses this for the error message Part II Write a class AudioDisk with the following attributes: Two instance variables (with corresponding getters and setters): name - a String for the name of the audio disk. tracks - an array of Strings with length 5, holding the names of the tracks the disk holds. A default constructor that initializes name to be "NoName". A constructor that takes a String to set the name of the disk Two methods: assign Tracks (no input parameters or return values) - This method should ask a user to from the console window until the user enters an empty line, which indicates the end the user enters more than 5 tracks, throw a TrackListException writeDisklnfo (no input parameters or return values) - writes the name of the disk and the disk's tracks to the console window. Part III Write a class AudioCd, a subclass of AudioDisk, for disks which can hold at most 2 tracks. The include the following: A default constructor that invokes the superclass constructor A constructor that takes a String to set the name of the disk. An overridden assign Tracks that throws a TrackListException when a user tries to assign tracks to an AudioCd. Part IV Write a class Test Disk that does the following: 1. Creates a Disk with the model The Arcade Fire 2. Assigns some tracks to the disk The Arcade Fire 3. Writes the information about The Arcade Fire to the console window 4. Creates a AudioCd with the name Man Man 5. Assigns tracks to Man Man 6. Writes the information about the Man Man to the console Finally Submit all 4 source files to the dropboxExplanation / Answer
package assignment;
import java.util.Scanner;
public class TestDisk extends Exception {
public static void main(String args[]) {
AudioDisk disk1 = new AudioDisk("The Arcade Fire");
try {
disk1.assignTracks();
} catch (TrackListException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
disk1.writeDiskInfo();
AudioCd cd1 = new AudioCd("Man Man");
try {
cd1.assignTracks();
} catch (TrackListException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cd1.writeDiskInfo();
}
}
class TrackListException extends Exception {
public TrackListException() {
super("Too many tracks");
}
public TrackListException(String msg) {
super(msg);
}
}
class AudioDisk {
private String name;
protected String tracks [] = new String[5];
public AudioDisk() {
name = "No Name";
}
public AudioDisk(String name) {
this.name = name;
}
public void assignTracks() throws TrackListException {
Scanner sc = new Scanner(System.in);
String track = null;
int i = 0;
do {
System.out.println("Enter track name (Empty line to exit):");
track = sc.nextLine();
if(i >=5)
throw new TrackListException();
tracks[i++] = track;
}while(!"".equals(track.trim()));
}
public void writeDiskInfo() {
System.out.println("Disk Name:"+name);
for(int i = 0; i <5; i++) {
System.out.println("Track #"+(i+1)+":"+tracks[i]);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getTracks() {
return tracks;
}
public void setTracks(String[] tracks) {
this.tracks = tracks;
}
}
class AudioCd extends AudioDisk {
public AudioCd() {
super();
tracks = new String[2];
}
public AudioCd(String name) {
super(name);
tracks = new String[2];
}
public void assignTracks() throws TrackListException {
Scanner sc = new Scanner(System.in);
String track = null;
int i = 0;
do {
System.out.println("Enter track name (Empty line to exit):");
track = sc.nextLine();
if(i >=2)
throw new TrackListException();
tracks[i++] = track;
}while(!"".equals(track.trim()));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.