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:
|
9. Create a class called TestSubscriber in the message package. Copy&paste:
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
package message;
|
package message;
|
||||||
|
|
||||||
public class TestSubscriber implements ISubscriber
|
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:
|
If you run it, it SHOULD look like this in the output:
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
TestSubscriber notified, name = first
|
TestSubscriber notified, name = first
|
||||||
|
|
||||||
TestSubscriber notified, name = second
|
TestSubscriber notified, name = second
|
||||||
|
@ -231,12 +237,16 @@ TestSubscriber notified, name = third
|
||||||
|
|
||||||
TestSubscriber notified, name = fourth
|
TestSubscriber notified, name = fourth
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
10. change to this for testing purposes:
|
10. change to this for testing purposes:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
Broadcaster b = new Broadcaster(4);
|
Broadcaster b = new Broadcaster(4);
|
||||||
|
|
||||||
b.subscribe(new TestSubscriber("first"));
|
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
|
b.notify(null); // we'll use a null message for now
|
||||||
|
|
||||||
error!
|
error!
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
to fix: don’t call notify method on null array location
|
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:
|
method:
|
||||||
|
|
||||||
public void notify(){
|
public void notify(){
|
||||||
|
|
||||||
if(array = null){???}
|
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
|
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:
|
this should be the output if it works:
|
||||||
|
|
||||||
|
```
|
||||||
TestSubscriber notified, name = first
|
TestSubscriber notified, name = first
|
||||||
|
|
||||||
TestSubscriber notified, name = second
|
TestSubscriber notified, name = second
|
||||||
|
@ -282,6 +298,8 @@ TestSubscriber notified, name = first
|
||||||
|
|
||||||
TestSubscriber notified, name = second
|
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:
|
code should look like this:
|
||||||
|
|
||||||
|
```
|
||||||
public static void main(String[] args)
|
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):
|
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
|
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!
|
||||||
|
|
||||||
Sequencer, but then put a few TestSubscriber instances in the sequencer. Have the
|
|
||||||
|
|
||||||
sequencer subscribe to the clock. Run it!
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
output should be this:
|
output should be this:
|
||||||
|
|
||||||
|
```
|
||||||
TestSubscriber notified, name = first
|
TestSubscriber notified, name = first
|
||||||
|
|
||||||
TestSubscriber notified, name = second
|
TestSubscriber notified, name = second
|
||||||
|
@ -358,6 +377,8 @@ TestSubscriber notified, name = third
|
||||||
|
|
||||||
TestSubscriber notified, name = fourth
|
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
|
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
|
22. make it implements the ISubscriber interface
|
||||||
|
@ -410,7 +435,7 @@ public class TestSound
|
||||||
|
|
||||||
24. create a constructor with String parameter:
|
24. create a constructor with String parameter:
|
||||||
|
|
||||||
|
```
|
||||||
public Sound _name;
|
public Sound _name;
|
||||||
|
|
||||||
public SoundPlayer(String x){
|
public SoundPlayer(String x){
|
||||||
|
@ -419,13 +444,20 @@ _name = new Sound (x);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
25. create another constructor with Sound parameter:
|
25. create another constructor with Sound parameter:
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
pubic SoundPlayer(Sound y){
|
pubic SoundPlayer(Sound y){
|
||||||
|
|
||||||
y = _name;
|
y = _name;
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
26. inside the notify method, play the sound?
|
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)
|
public static void main(String[] args)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -474,6 +507,10 @@ public static void main(String[] args)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
28. create comments for this class (SoundPlayer)
|
28. create comments for this class (SoundPlayer)
|
||||||
|
@ -486,12 +523,16 @@ public static void main(String[] args)
|
||||||
30. create a constructor like this:
|
30. create a constructor like this:
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
public Chord(Sound[ ] x){
|
public Chord(Sound[ ] x){
|
||||||
|
|
||||||
super(sounds.length);}
|
super(sounds.length);}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
31.
|
31.
|
||||||
|
@ -505,6 +546,12 @@ instance. lolwut?
|
||||||
|
|
||||||
It works like this:
|
It works like this:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
for(some loop)
|
for(some loop)
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue