Question: / / fill out the code / / observer . java package question 1 ; public interface Observer { void valueHasChanged ( T newValue )

// fill out the code
//observer.java
package question1;
public interface Observer {
void valueHasChanged(T newValue);
}
//statevariable.java
package question1;
// Create an observer that can alert an observer of a value changed
public class StateVariable {
public StateVariable(Observer observer){
}
public void setValue(T newValue){
}
}
//question1.java
package question1;
import org.junit.Assert;
import org.testng.annotations.Test;
class Watcher implements Observer{
int count;
T lastValueSeen;
@Override
public void valueHasChanged(T newValue){
count++;
lastValueSeen = newValue;
}
}
public class Question1{
/*
* Create an observer that alerts when a value has changed
*/
@Test
public void test(){
var watcher = new Watcher();
StateVariable intState = new StateVariable<>(watcher);
intState.setValue(1);
Assert.assertEquals(watcher.count, 1);
Assert.assertEquals((int)watcher.lastValueSeen, 1);
intState.setValue(2);
Assert.assertEquals(watcher.count, 2);
Assert.assertEquals((int)watcher.lastValueSeen, 2);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!