Updated: Monday, August 28,2023-08-28 11:20:53
parent
166ba28236
commit
58a1846dcb
|
@ -172,6 +172,8 @@ public Broadcaster(int numSubscribers)
|
|||
9. Create a class called TestSubscriber in the message package. Copy&paste:
|
||||
|
||||
|
||||
```java
|
||||
|
||||
package message;
|
||||
|
||||
public class TestSubscriber implements ISubscriber
|
||||
|
@ -218,11 +220,15 @@ public class TestSubscriber implements ISubscriber
|
|||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
If you run it, it SHOULD look like this in the output:
|
||||
|
||||
```
|
||||
|
||||
TestSubscriber notified, name = first
|
||||
|
||||
TestSubscriber notified, name = second
|
||||
|
@ -231,12 +237,16 @@ TestSubscriber notified, name = third
|
|||
|
||||
TestSubscriber notified, name = fourth
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
10. change to this for testing purposes:
|
||||
|
||||
|
||||
|
||||
```java
|
||||
Broadcaster b = new Broadcaster(4);
|
||||
|
||||
b.subscribe(new TestSubscriber("first"));
|
||||
|
@ -246,27 +256,32 @@ b.subscribe(new TestSubscriber("second"));
|
|||
b.notify(null); // we'll use a null message for now
|
||||
|
||||
error!
|
||||
```
|
||||
|
||||
|
||||
to fix: don’t call notify method on null array location
|
||||
|
||||
|
||||
|
||||
|
||||
11. Copy&Paste Broadcaster class and rename as Sequencer
|
||||
11. Copy&Paste Broadcaster class and rename as `Sequencer`
|
||||
|
||||
|
||||
|
||||
|
||||
12. Change the notify method so that it looks like this:
|
||||
12. Change the `notify` method so that it looks like this:
|
||||
|
||||
|
||||
create private member variable first: int y = 0;
|
||||
```java
|
||||
int y = 0;
|
||||
|
||||
method:
|
||||
|
||||
public void notify(){
|
||||
|
||||
if(array = null){???}
|
||||
```
|
||||
create private member variable first:
|
||||
|
||||
furthermore fix the sequncermuch like above so that it works if any/all of array location are null
|
||||
|
||||
|
@ -274,6 +289,7 @@ furthermore fix the sequncermuch like above so that it works if any/all of array
|
|||
|
||||
this should be the output if it works:
|
||||
|
||||
```
|
||||
TestSubscriber notified, name = first
|
||||
|
||||
TestSubscriber notified, name = second
|
||||
|
@ -282,6 +298,8 @@ TestSubscriber notified, name = first
|
|||
|
||||
TestSubscriber notified, name = second
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -295,7 +313,7 @@ TestSubscriber notified, name = second
|
|||
|
||||
|
||||
|
||||
15. Make a new class called Clock in model package , download class file from website and paste code into the class
|
||||
15. Make a new class called `Clock` in model package , download class file from website and paste code into the class
|
||||
|
||||
|
||||
|
||||
|
@ -305,11 +323,12 @@ TestSubscriber notified, name = second
|
|||
|
||||
|
||||
|
||||
17. Create a TestClock class in the model package with main method:
|
||||
17. Create a `TestClock` class in the model package with main method:
|
||||
|
||||
|
||||
code should look like this:
|
||||
|
||||
```
|
||||
public static void main(String[] args)
|
||||
|
||||
{
|
||||
|
@ -326,22 +345,22 @@ public static void main(String[] args)
|
|||
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
output should be this (8 times, once every half second):
|
||||
|
||||
TestSubscriber notified, name = clock tick
|
||||
``TestSubscriber notified, name = clock tick``
|
||||
|
||||
|
||||
|
||||
replace the TestSubscriber in the main method with a
|
||||
|
||||
Sequencer, but then put a few TestSubscriber instances in the sequencer. Have the
|
||||
|
||||
sequencer subscribe to the clock. Run it!
|
||||
replace the TestSubscriber in the main method with a Sequencer, but then put a few TestSubscriber instances in the sequencer. Have the sequencer subscribe to the clock. Run it!
|
||||
|
||||
|
||||
|
||||
output should be this:
|
||||
|
||||
```
|
||||
TestSubscriber notified, name = first
|
||||
|
||||
TestSubscriber notified, name = second
|
||||
|
@ -358,6 +377,8 @@ TestSubscriber notified, name = third
|
|||
|
||||
TestSubscriber notified, name = fourth
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -372,9 +393,9 @@ TestSubscriber notified, name = fourth
|
|||
|
||||
|
||||
|
||||
20. Create a class called Sound and paste this (you should hear sound after running and you can choose which sounds to put in ):
|
||||
20. Create a class called `Sound` and paste this (you should hear sound after running and you can choose which sounds to put in ):
|
||||
|
||||
|
||||
```
|
||||
public class TestSound
|
||||
|
||||
{
|
||||
|
@ -393,9 +414,13 @@ public class TestSound
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
21. Create a class called SoundPlayer
|
||||
21. Create a class called `SoundPlayer`
|
||||
|
||||
|
||||
22. make it implements the ISubscriber interface
|
||||
|
@ -410,7 +435,7 @@ public class TestSound
|
|||
|
||||
24. create a constructor with String parameter:
|
||||
|
||||
|
||||
```
|
||||
public Sound _name;
|
||||
|
||||
public SoundPlayer(String x){
|
||||
|
@ -419,13 +444,20 @@ _name = new Sound (x);
|
|||
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
25. create another constructor with Sound parameter:
|
||||
|
||||
|
||||
```java
|
||||
|
||||
pubic SoundPlayer(Sound y){
|
||||
|
||||
y = _name;
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
26. inside the notify method, play the sound?
|
||||
|
@ -433,9 +465,10 @@ y = _name;
|
|||
|
||||
|
||||
|
||||
27. Create TestSoundPlayer class in model package, and copy&paste this:
|
||||
27. Create `TestSoundPlayer` class in model package, and copy&paste this:
|
||||
|
||||
|
||||
```java
|
||||
public static void main(String[] args)
|
||||
|
||||
{
|
||||
|
@ -474,6 +507,10 @@ public static void main(String[] args)
|
|||
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
28. create comments for this class (SoundPlayer)
|
||||
|
@ -486,12 +523,16 @@ public static void main(String[] args)
|
|||
30. create a constructor like this:
|
||||
|
||||
|
||||
```java
|
||||
public Chord(Sound[ ] x){
|
||||
|
||||
super(sounds.length);}
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
31.
|
||||
|
@ -505,6 +546,12 @@ instance. lolwut?
|
|||
|
||||
It works like this:
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
```
|
||||
for(some loop)
|
||||
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue