The following program uses a
DoubleBufferto implement a random-access queue. The input data could represent 24 temperatures measured at each hour of a typical day. The program reads the first 12 data items into the buffer. Then it prints the temperature at index 9. Then it copies the first 6 buffer items to an output array. Then it reads the remaining input data items into the buffer. Finally, it copies the remaining buffer items to the output array. Insert and append comments that explain each operation, and in particular, explain how the buffer’s mark and limit variables relate to the head and tail of the queue.
/*************************************************************** Temperatures.java* Dean & Dean** This uses a buffer to implement a random-access data queue.* mark is ____________ limit is __________________*************************************************************/import java.nio.DoubleBuffer; public class Temperatures { public static void main(String[] args) { double[] dataIn = new double[] {1.3, 0.4, -0.6, -1.2, -1.8, -2.1, -2.3, -1.3, -0.8, 0.2, 1.3, 2.3, 3.5, 4.8, 5.7, 6.5, 5.1, 4.2, 3.0, 1.9, 0.6, 0.1, -0.1, -0.6}; double[] dataOut = new double[24]; DoubleBuffer buffer = DoubleBuffer.allocate(24); buffer.mark(); buffer.put(dataIn, 0, 12); buffer.limit(buffer.position()); System.out.println(buffer.get(9)); buffer.reset(); buffer.get(dataOut, 0, 6); buffer.mark(); buffer.position(buffer.limit()); buffer.limit(buffer.capacity()); buffer.put(dataIn, buffer.position(), buffer.remaining()); buffer.reset(); buffer.get(dataOut, buffer.position(), buffer.remaining()); } // end main } // end class Temperatures
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.