Java TOPIC 27 – DATA STRUCTURES ASSIGNMENT LESSON WORK
QUESTION 1
– QUEUE (1 point) Write
the one-line statement that will move the first element in a queue named q to
the last element in the q. In other
words, move an element from the beginning of the line to the end. QUESTION 2
– QUEUE (2 points) Write
the statements required to move the last element in a queue to the first
element in the queue. So the element
that was previously first is now second.
And the order is the same for the rest. QUESTION 3
– QUEUE (3 points) Write
the statements required to flip the order of the queue. Note that you will need to use another data
structure to temporarily hold the contents of the queue. QUESTION 4A
– STACK (3 points) You
have a stack. Unfortunately, you need
to get the value of the element at the very bottom of the stack. Fortunately, you no longer need the stack
afterwards. QUESTION 4B
– STACK (5 points) You
have a stack. Unfortunately, you need
to get the value of the element at the very bottom and then you still need
the rest of the stack to be intact (without the bottom element). QUESTION 5
– SET (5 points) Write
the program that will continuously simulate two die being rolled. The goal is to continue rolling die until
every number but one has been rolled.
That number is the winner. SAMPLE OUTPUT Remaining:
2 3 4 5 6 7 8 9 10 11 12 Roll:
2+4=6 Remaining:
2 3 4 5 7 8 9 10 11 12 Roll:
1+3=4 Remaining:
2 3 5 7 8 9 10 11 12 Roll:
6+4=10 Remaining:
2 3 4 5 7 8 9 11 12 Roll:
3+3=6 Remaining:
2 3 4 5 7 8 9 11 12 …and
so on… Remaining:
2 11 Roll:
1+1=2 Remaining:
11 Winner:
11 QUESTION 6
– ARRAYLIST (4 points) You
have an arraylist with elements in it. It contains doubles (and possibly triples,
quads, …) of some of the data. We need
to remove all multiple occurences (or simply
recreate an ArrayList without multi-occurences). SAMPLE
OUTPUT Original
data: [2,4,18,10,7,2,0,7,4,9] Final
data: [2,4,18,10,7,0,9] QUESTION 7
– QUEUE (5 points) Guests
are all lined up at a theatre waiting to be seated. Each guest has a unique number that
associates them with their seat.
Guests with a number less than 200 are seated at the ground level
while guests with a number greater than 200 are seated up on the
balcony. Those guests have to take an
elevator. Unfortunately,
the elevator is broken. Your boss
wants you to seat all the guests on the main floor first (in the same order
that they arrived) and then address the balcony guests once the elevator is
fixed (again in the same order they arrived). Write
the program that re-orders the lineup. SAMPLE OUTPUT Original
lineup: [88,3,118,242,415,13,201,18,391,7] Reorganized
lineup: [88,3,118,13,18,7,242,415,201,391] QUESTION 8
– MAP (4 points) You
have been hired to create an RPG. For
now, the spellcasters in the game have a choice of
5 spells: Fireball, Lightning,
Earthquake, Fly and EnergyBeam. Their respective damage is 5,6,2,0,7 and
their mana cost is 4,4,6,5,5. Make use
of a map data structure to have an easy way to look up spell damage and mana
cost. QUESTION 9
– MAP (6 points) Write
a program that will continuously encrypt or decrypt a word using the letter
to letter mapping below. You can
assume the word will always be in lowercase.
The first letter that the user enters is either a E or D representing
Encrypt or Decrypt.
SAMPLE OUTPUT (loops
forever) How
can I serve you my master? Dzkzkyqd …and
so on… Note:
The orange text is the user input. QUESTION
10 – MAP (4 points) Consider
the following code that stores all of the Scrabble letter values inside of a
map. Write the program that will ask
the user for a word and output the value of that word (the total of the value
of all letters in that word).
|
||||||||||
|