[Cst-1b] CST1-b Question: Java 98/4/3 (solution)

M.Y.W.Y.B. mywyb2@hermes.cam.ac.uk
Wed, 03 May 2000 13:41:59 +0100


--On Donnerstag, 4. Mai 2000, 00:47 +0100 "Martin Harper" <mcnh2@cam.ac.uk>
wrote: 
[...]
> This is something of a clumsy answer, however - and I'm not sure if it
would
> lose marks for that. Something better can be done.
[...]

Yes, I think that's the right way to go. I suggest just two subtle
corrections:

[...]
>     int read() {
>         int readernum = rm.turn();
>         rs.queue(readernum);
>         int temp = val;
>         rs.next();
>         return temp;
>     }

Corrected version of read():

     int read() {
         int readernum = rm.turn() + 1;  // CHANGED
         rs.queue(readernum);
         int temp = val;
         ws.next();                      // CHANGED
         return temp;
     }

The second change is obvious. The first one is necessary because in the
original version you could 
1) read before there was a value in the buffer
2) store something and read twice
Now the first reader ticket is 1 and not 0 and everything should be fine.


Thanks!

Moritz