Spaces:
Running
Running
| The following program defines the category Disc. | |
| Refer to the class definition, and then write the class DiscPlayer, which has the following public properties: | |
| A constructor DiskPlayer() that takes no parameters | |
| Method void insertDisc( Disc disc), which inserts a new disc into the player. | |
| Method void nextTrack(), which selects the next track of the current disc, if there are still tracks left. If there are no more tracks, the method does nothing | |
| Method String currentTrackName(), which returns the name of the current track from the current disc | |
| Note that the class must not be public (i.e. the definition must not have the attribute public) | |
| import java.util.Random; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| public class Test{ | |
| public static void main(String[] args) { | |
| String[] k1 = {"Javaland", "Beyond the Java", "Java-Nooa", | |
| "Wild Python", "Java it up"}; | |
| String[] k2 = {"Fortunate Python", "Have you ever seen Python", | |
| "Roll over Python", "Check my Java", | |
| "Smells like Python", "Java's on Fire"}; | |
| ArrayList<String> kpl1 = new ArrayList<String>(Arrays.asList(k1)); | |
| ArrayList<String> kpl2 = new ArrayList<String>(Arrays.asList(k2)); | |
| Disc d1 = new Disc(kpl1); | |
| Disc d2 = new Disc(kpl2); | |
| Disc[] disks = {d1, d2}; | |
| DiscPlayer player = new DiscPlayer(); | |
| System.out.println("Disc player created"); | |
| for (Disc d : disks) { | |
| player.insertDisc(d); | |
| System.out.println("Disc inserted"); | |
| System.out.println("Playing seven songs:"); | |
| for (int i=0; i<7; i++) { | |
| System.out.println(player.currentTrackName()); | |
| player.nextTrack(); | |
| } | |
| System.out.println(""); | |
| } | |
| } | |
| } | |
| class Disc { | |
| private int amountOfTracks; | |
| private ArrayList<String> songs; | |
| private int currentTrack; | |
| public Disc(ArrayList<String> songs) { | |
| this.songs = songs; | |
| this.amountOfTracks = songs.size(); | |
| this.currentTrack = 0; | |
| } | |
| public boolean hasMoreTracks() { | |
| return currentTrack < (amountOfTracks - 1); | |
| } | |
| public int getCurrentTrack() { | |
| return this.currentTrack; | |
| } | |
| public void nextTrack() { | |
| currentTrack++; | |
| } | |
| public String trackName() { | |
| return songs.get(currentTrack); | |
| } | |
| } | |
| //ADD | |
| class DiscPlayer { | |
| // 1 disc at a time | |
| // not multiple disks in 1 player | |
| // private ArrayList<Disc> listDisks; | |
| private Disc currentDisc; | |
| //CONSTRUCTOR | |
| public DiscPlayer() { | |
| this.currentDisc = currentDisc; | |
| } | |
| public void insertDisc(Disc disc) { | |
| this.currentDisc = disc; | |
| } | |
| public void nextTrack() { | |
| // if (this.currentDisc.hasMoreTracks() == true) { | |
| if (this.currentDisc.hasMoreTracks()) { | |
| this.currentDisc.nextTrack(); | |
| } | |
| // otherwise do nothing if there are no more 'next tracks' | |
| } | |
| public String currentTrackName() { | |
| return this.currentDisc.trackName(); | |
| } | |
| } | |
| Disc player created | |
| Disc inserted | |
| Playing seven songs: | |
| Javaland | |
| Beyond the Java | |
| Java-Nooa | |
| Wild Python | |
| Java it up | |
| Java it up | |
| Java it up | |
| Disc inserted | |
| Playing seven songs: | |
| Fortunate Python | |
| Have you ever seen Python | |
| Roll over Python | |
| Check my Java | |
| Smells like Python | |
| Java's on Fire | |
| Java's on Fire | |