Spaces:
Running
Running
File size: 3,487 Bytes
f81be76 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
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
|