Spaces:
Running
Running
wrapper class contains 0/1 subclass
Browse files
Week 4: Writing classes/15B. Write class DiscPlaye
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The following program defines the category Disc.
|
| 2 |
+
|
| 3 |
+
Refer to the class definition, and then write the class DiscPlayer, which has the following public properties:
|
| 4 |
+
A constructor DiskPlayer() that takes no parameters
|
| 5 |
+
Method void insertDisc( Disc disc), which inserts a new disc into the player.
|
| 6 |
+
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
|
| 7 |
+
Method String currentTrackName(), which returns the name of the current track from the current disc
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
Note that the class must not be public (i.e. the definition must not have the attribute public)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import java.util.Random;
|
| 18 |
+
import java.util.ArrayList;
|
| 19 |
+
import java.util.Arrays;
|
| 20 |
+
|
| 21 |
+
public class Test{
|
| 22 |
+
public static void main(String[] args) {
|
| 23 |
+
String[] k1 = {"Javaland", "Beyond the Java", "Java-Nooa",
|
| 24 |
+
"Wild Python", "Java it up"};
|
| 25 |
+
String[] k2 = {"Fortunate Python", "Have you ever seen Python",
|
| 26 |
+
"Roll over Python", "Check my Java",
|
| 27 |
+
"Smells like Python", "Java's on Fire"};
|
| 28 |
+
|
| 29 |
+
ArrayList<String> kpl1 = new ArrayList<String>(Arrays.asList(k1));
|
| 30 |
+
ArrayList<String> kpl2 = new ArrayList<String>(Arrays.asList(k2));
|
| 31 |
+
|
| 32 |
+
Disc d1 = new Disc(kpl1);
|
| 33 |
+
Disc d2 = new Disc(kpl2);
|
| 34 |
+
|
| 35 |
+
Disc[] disks = {d1, d2};
|
| 36 |
+
|
| 37 |
+
DiscPlayer player = new DiscPlayer();
|
| 38 |
+
System.out.println("Disc player created");
|
| 39 |
+
|
| 40 |
+
for (Disc d : disks) {
|
| 41 |
+
player.insertDisc(d);
|
| 42 |
+
System.out.println("Disc inserted");
|
| 43 |
+
System.out.println("Playing seven songs:");
|
| 44 |
+
for (int i=0; i<7; i++) {
|
| 45 |
+
System.out.println(player.currentTrackName());
|
| 46 |
+
player.nextTrack();
|
| 47 |
+
}
|
| 48 |
+
System.out.println("");
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
class Disc {
|
| 56 |
+
private int amountOfTracks;
|
| 57 |
+
private ArrayList<String> songs;
|
| 58 |
+
private int currentTrack;
|
| 59 |
+
|
| 60 |
+
public Disc(ArrayList<String> songs) {
|
| 61 |
+
this.songs = songs;
|
| 62 |
+
this.amountOfTracks = songs.size();
|
| 63 |
+
this.currentTrack = 0;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
public boolean hasMoreTracks() {
|
| 67 |
+
return currentTrack < (amountOfTracks - 1);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
public int getCurrentTrack() {
|
| 71 |
+
return this.currentTrack;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
public void nextTrack() {
|
| 75 |
+
currentTrack++;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
public String trackName() {
|
| 79 |
+
return songs.get(currentTrack);
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
//ADD
|
| 88 |
+
class DiscPlayer {
|
| 89 |
+
// 1 disc at a time
|
| 90 |
+
// not multiple disks in 1 player
|
| 91 |
+
// private ArrayList<Disc> listDisks;
|
| 92 |
+
private Disc currentDisc;
|
| 93 |
+
|
| 94 |
+
//CONSTRUCTOR
|
| 95 |
+
public DiscPlayer() {
|
| 96 |
+
this.currentDisc = currentDisc;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
public void insertDisc(Disc disc) {
|
| 100 |
+
this.currentDisc = disc;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
public void nextTrack() {
|
| 104 |
+
// if (this.currentDisc.hasMoreTracks() == true) {
|
| 105 |
+
if (this.currentDisc.hasMoreTracks()) {
|
| 106 |
+
this.currentDisc.nextTrack();
|
| 107 |
+
}
|
| 108 |
+
// otherwise do nothing if there are no more 'next tracks'
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
public String currentTrackName() {
|
| 113 |
+
return this.currentDisc.trackName();
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
Disc player created
|
| 125 |
+
Disc inserted
|
| 126 |
+
Playing seven songs:
|
| 127 |
+
Javaland
|
| 128 |
+
Beyond the Java
|
| 129 |
+
Java-Nooa
|
| 130 |
+
Wild Python
|
| 131 |
+
Java it up
|
| 132 |
+
Java it up
|
| 133 |
+
Java it up
|
| 134 |
+
|
| 135 |
+
Disc inserted
|
| 136 |
+
Playing seven songs:
|
| 137 |
+
Fortunate Python
|
| 138 |
+
Have you ever seen Python
|
| 139 |
+
Roll over Python
|
| 140 |
+
Check my Java
|
| 141 |
+
Smells like Python
|
| 142 |
+
Java's on Fire
|
| 143 |
+
Java's on Fire
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|