nus-cs2030 / 2122-s2

CS2030 repository and wiki for AY 2021/2022 Sem 2
MIT License
0 stars 0 forks source link

What is level 3 onwards trying to say? #55

Open yongchein-o opened 2 years ago

yongchein-o commented 2 years ago

Summary

I am having trouble comprehending what Level 3 onwards is trying to say as it is very confusing. Can someone please explain to me what does all this mean

Level 3 A token can be registered to a registry. Additionally, a registry has write access to a data store via the DataStore::write method. The DataStore class is provided for you and is used to simulate writing information into the data store by invoking a println output statement instead. You are NOT allowed to modify the DataStore class.

class DataStore { void write(String info) { System.out.println("DataStore: " + info); } } You may assume that invoking new DataStore() will establish a connection to the same data store. With a DataStore object created, the registry can write to the data store via the void store(String message) method.

Once a token is registered, a void contact() method can be invoked within the token to test the contact with the registry. The test method will write a string "Test contact" to the data store.

While adhering to good abstraction principles, create a RegisteredToken class and implement the contact method. At this juncture, we will also need to create a RegistryStub object in place of a Registry object in order to test the functionality of our classes. In particular, RegistryStub has two constructors: one that takes in a string identifier, and the other with no arguments (i.e. the identifier is an empty string).

jshell> DataStore db = new DataStore() jshell> db.write("Hello") DataStore: Hello jshell> Token t1 = new Token(1) jshell> Token t2 = new Token(2) jshell> t1 = t1.ping(t2, 10) jshell> t1 t1 ==> Token #1: #2@10 jshell> new RegistryStub() $.. ==> [] jshell> Registry stub = new RegistryStub("Test Stub") jshell> stub stub ==> [Test Stub] jshell> stub.store("Hello world") DataStore: Hello world jshell> new RegisteredToken(t1, stub) $.. ==> Token #1: #2@10 jshell> new RegisteredToken(t1, stub).contact() DataStore: Test contact jshell> Token t = new RegisteredToken(t1, stub) jshell> t t ==> Token #1: #2@10 jshell> /exit Level 4 We are now ready to write the actual registry implementation class RegistryImpl. This object is created whenever we need to perform contact tracing; that is to say, the tokens would already have pinged one another. A list of (unregistered) tokens is passed to the RegistryImpl constructor. Then, each token needs to be registered to the registry (see level 3 above).

For the purpose of testing, the output of a registry will include the identifier of the registry, the number of tokens registered, and the corresponding output of each token. The tokens are output in the same order as the list presented to the constructor. If you are concerned about possible data leaks, we can always make the toString methods less verbose later on.

jshell> Token t1 = new Token(1) jshell> Token t2 = new Token(2) jshell> t1 = t1.ping(t2, 10) jshell> Registry r1 = new RegistryImpl("r1") jshell> r1 r1 ==> [r1]: 0 tokens registered jshell> Registry r2 = new RegistryImpl("r2", List.of(t1, t2)) jshell> r2 r2 ==> [r2]: 2 tokens registered Token #1: #2@10 Token #2: none jshell> Registry r3 = new RegistryImpl("r3", List.of()) jshell> r3 r3 ==> [r3]: 0 tokens registered jshell> /exit Level 5 Once the tokens are registered into the registry, a void alert(int time) method that takes a positive (> 0) integer time as the argument can be called to alert all tokens to write their data to the data store only if the ping-time recorded and the specified time coincide exactly.

Ensure that only the Registry class has a dependency on the DataStore class.

Hint: Keep in mind the Tell-Don't-Ask principle. Specifically, the role of RegistryImpl is just to initiate the alert, and tell each token to proceed with writing data to the data store. Once the token is alerted, it initiates the process to write data to the data store.

jshell> Token t1 = new Token(1) jshell> Token t2 = new Token(2) jshell> t1 = t1.ping(t2, 10).ping(new Token(3), 5) jshell> t1 t1 ==> Token #1: #2@10 #3@5 jshell> t2 t2 ==> Token #2: none jshell> new RegistryImpl("r1", List.of(t1, t2)).alert(5) DataStore: Token #1: #3@5 DataStore: Token #2: none jshell> new RegistryImpl("r2", List.of(t1, t2)).alert(8) DataStore: Token #1: none DataStore: Token #2: none jshell> t2 = t2.ping(t1, 5) jshell> new RegistryImpl("r3", List.of(t1, t2)).alert(5) DataStore: Token #1: #3@5 DataStore: Token #2: #1@5 jshell> /exit Level 6 We are now ready to include more devices into the registry. A safe-entry device with a positive (> 0) identifier can be registered into the registry. Unlike a token, a safe-entry device has a direct connection with the data store.

Include add methods into the registry so it can have either another safe entry device or an unregistered token added.

Ensure that only Registry and SafeEntry classes have dependencies on the DataStore class.

For simplicity, you may assume there is only one single positive time recorded on a safe-entry device. jshell> Token t1 = new Token(1) jshell> Token t2 = new Token(2) jshell> t1 = t1.ping(t2, 10).ping(new Token(3), 5) jshell> t1 t1 ==> Token #1: #2@10 #3@5 jshell> t2 t2 ==> Token #2: none jshell> new RegistryImpl("r1", List.of(t1, t2)).alert(5) DataStore: Token #1: #3@5 DataStore: Token #2: none jshell> new RegistryImpl("r2", List.of(t1, t2)).alert(8) DataStore: Token #1: none DataStore: Token #2: none jshell> t2 = t2.ping(t1, 5) jshell> Registry r = new RegistryImpl("r3", List.of(t1, t2)) jshell> r.alert(5) DataStore: Token #1: #3@5 DataStore: Token #2: #1@5 jshell> new SafeEntry(10, 5) $.. ==> SafeEntry #10@5 jshell> Registry s = r.add(new SafeEntry(10, 5)).add(new Token(4).ping(t2, 5)) jshell> s s ==> [r3]: 4 tokens registered Token #1: #2@10 #3@5 Token #2: #1@5 SafeEntry #10@5 Token #4: #2@5 jshell> s.alert(5) DataStore: Token #1: #3@5 DataStore: Token #2: #1@5 DataStore: SafeEntry #10@5 DataStore: Token #4: #2@5 jshell> s.alert(8) DataStore: Token #1: none DataStore: Token #2: none DataStore: SafeEntry #10: none DataStore: Token #4: none jshell> r r ==> [r3]: 2 tokens registered Token #1: #2@10 #3@5 Token #2: #1@5 jshell> /exit

Description

Describe the problem and any details if neceesary

Idea s

Describe the approach used (if applicable)

Screenshots (if any):

Insert Images here if necessary

teresasee commented 2 years ago

Hi, for me I just take it that it means Registry has to define a method which makes use of the method write in DataStore which saves the token's data to the datastore.

In level 3, they wanted to test whether token/registered token works so they told us to create a RegistryStub (not the actual Registry, more simplified version which can help to check whether Token/RegisteredToken works correctly).

In level 4, you are asked to make RegistryImpl which is the actual more complicated version of a Registry.

In level 5, you are asked to implement the alert(int time) method which will show the tokens registered in that registry + their pinged tokens with same time as the input argument (if any)

Finally in level 6, you are asked to create a SafeEntry class which acts a little differently from Token + define add method which adds a device into a registry (can include both SafeEntry and Token).

Not sure if I understood the question correctly as well but I hope this helped you to get a better picture of the question.

tanweiming00 commented 2 years ago

Encountered this isssue for Level 3.

RegisteredToken.class -> DataStore.class RegistryStub.class -> DataStore.class Forbidden dependency on DataStore detected. Grading terminated.

kh1js commented 2 years ago

Hi @tanweiming00, I encountered the same situation too. This is likely to be due to the requirements in level 6, where "Ensure that only Registry and SafeEntry classes have dependencies on the DataStore class." What I did was to make RegistryStub a subclass of Registry, where RegistryStub no longer has to directly write to the DataStore. You may want to try something similar for RegisteredToken. Get the RegisteredToken to write to the DataStore through the registry.

Hope this helps!

wk1267 commented 2 years ago

Hi, apologies for the question but where is the DataStore class? Do we need to make it ourselves like so? class DataStore { void write(String info) { System.out.println("DataStore: " + info); } }

alantay11 commented 2 years ago

Hi, apologies for the question but where is the DataStore class? Do we need to make it ourselves like so? class DataStore { void write(String info) { System.out.println("DataStore: " + info); } }

Yeah, you need to copy paste the code from the question into a new DataStore.java file you create on your own.