To become familiar with stacks and queues. Directions: You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate operation: If the tile is a 1, you remove that tile plus one more from the top of the stack. If the tile is a 2. you remove that tile plus two more from the top of the stack.
To become familiar with stacks and queues. Directions: You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate operation: If the tile is a 1, you remove that tile plus one more from the top of the stack. If the tile is a 2. you remove that tile plus two more from the top of the stack.
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
in java pls and thank you!

Transcribed Image Text:To become familiar with stacks and queues.
Directions:
You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool
of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate
operation:
●
●
If the tile is a 1, you remove that tile plus one more from the top of the stack.
If the tile is a 2, you remove that tile plus two more from the top of the stack.
If the tile is a 3, you remove that tile but take the three next tiles from the queue and add them
to the top of the stack.
When the player's stack has become empty, the game is over. If this happens in the middle of a turn (for
example, if the player is supposed to remove three tiles from the stack but there are only two tiles left),
that still counts as a turn. You can assume that the queue will always contain enough tiles to reach the
end of the game.
Write a class called TileGame that contains a method that takes the stack and queue of integers and
returns the number of turns it takes for the player's stack of tiles to become empty. Your method must
have the following signature:
public static int tileGame (ArrayDeque<Integer> stack, Queue<Integer> q)
Your program must be able to handle all test cases without causing an exception in order to receive full
credit, so be sure to try to think of tricky cases and check them.
Note that this problem does not require recursion to solve (though you can use that if you wish).
Example output:
If the queue contains the items 1, 2, 2, 1, 3, 1, 2, 1, 2, 3
and the stack contains (from top to bottom) the items 3, 2, 1, 2
then the return value of your method should be 3 because
Turn 1: discard the 3 and add 1, 2, 2 so the stack is now 2, 2, 1, 2, 1, 2
Turn 2: discard the 2 as well as the following 2, 1 so the stack is now 2, 1, 2
Turn 3: discard the 2 and the following 1, 2, so the stack is now empty
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images

Recommended textbooks for you

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY