Java Reference
In-Depth Information
8.
Write a program to simulate job scheduling in an operating system. Jobs are generated at random times. Each job
is given both a random priority from 1 to 4—where 1 is the highest priority—and a random amount of time to
complete its execution.
Jobs do not begin execution and run to completion, but instead share the processor. The operating system exe-
cutes a job for a fixed unit of time called a time slice . At the end of the time slice, the current job's execution is
suspended. The job is then placed on a priority queue, where it waits for its next share of processor time. The job
having the highest priority is then removed from the priority queue and executed for a time slice.
When a job is first generated, it will begin executing immediately if the processor is free. Otherwise it will be
placed on the priority queue.
9.
Repeat the online Project 6 for Appendix B to create the class Huge of large integers. Use a deque instead of an
array to represent the value of an integer.
10.
One way to shuffle playing cards is to use a perfect shuffle . First, you divide a deck of 52 cards into two halves of
26 cards each. Next, you merge the halves by interleaving the cards as follows. Beginning with the top half and
alternating halves, you take the bottom card from a half and place it on top of a new deck.
For example, if our deck contains the six cards 1 2 3 4 5 6, the top half is 1 2 3, and the bottom half is 4 5 6. The 3 at
the bottom of the top half becomes the bottom card in the shuffled deck. We then place the 6, which is at the bottom of the
bottom half, on top of the shuffled deck. Next, we place 2 on top, then 5, 1, and finally 4. The shuffled deck is then 4 1 5
2 6 3. Notice that the card that was on top of the original deck is now second in the shuffled result, and the bottom card in
the original deck is now second from the bottom in the shuffled deck. This shuffle is called an in-shuffle and is achieved
by beginning with the top half when you move cards into the shuffled result. If you begin with the bottom half, you get an
out-shuffle , whereby the original top card and bottom card remain in their positions in the shuffled deck.
Define a class of playing-card decks by using a deque to contain the cards. Your class should define methods
to perform perfect in-shuffles and perfect out-shuffles. Using your class,
a. Determine the number of perfect out-shuffles needed to return a deck of n cards to its original order.
b. Determine the number of perfect in-shuffles needed to return a deck of n cards to its original order.
c. You can move a deck's top card, which is at position 0, to any desired position m by performing a sequence of
in-shuffles and out-shuffles, as follows. You write m in binary. Beginning with the leftmost 1 and proceeding to
the right, you perform an in-shuffle for each 1 encountered and an out-shuffle for each 0. For example, if m is 8,
we have 1000 for its binary equivalent. We would perform one in-shuffle followed by three out-shuffles to
move the original top card to position 8, that is, so it is the ninth card from the top of the deck. Define a method
to perform this card trick.
A NSWERS TO S ELF -T EST Q UESTIONS
1.
Jill is at the front, Jess is at the back.
2.
a. 11.
b. 4.
3.
Jill is at the front, Jane is at the back.
4.
Jim is at the front, Jane is at the back.
Search WWH ::




Custom Search