Java

TOPIC 27 – DATA STRUCTURES ASSIGNMENT

 

LESSON WORK

 

 


INSTRUCTIONS

Your teacher will tell you how many points you need to accumulate.  You will decide which of the questions below are worth solving to get their associated points.

 

ABOUT QUEUES

 

To create a Queue, make sure to use the following which will limit the functionality to only Queue methods:

 

    Queue<Integer> q = new LinkedList<Integer>();

 

ABOUT STACKS

 

Unfortunately, the Stack class in Java provides extra functionality than traditional stacks.  You should limit yourself to the methods: pop, push, isEmpty and size.

 

 

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).

Note: You cannot get marks for both 4A and 4B.

 

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.

 

Letter

Encrypted

Letter

Letter

Encrypted

Letter

a

b

c

d

e

f

g

h

i

j

k

l

m

j

b

z

e

f

r

n

h

s

g

u

i

x

n

o

p

q

r

s

t

u

v

w

x

y

z

y

k

a

o

c

p

d

q

l

t

v

m

w

 

SAMPLE OUTPUT (loops forever)

How can I serve you my master? Ecoconut

Word: coconut
Encrypted: zkzkyqd

 

How can I serve you my master? Dzkzkyqd

Encrypted: zkzkkyqd
Word: coconut

 

…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).

 

 

        TreeMap<String, Integer> map = new TreeMap<String, Integer>();

        map.put("A", 1);

        map.put("B", 3);

        map.put("C", 3);

        map.put("D", 2);

        map.put("E", 1);

        map.put("F", 4);

        map.put("G", 2);

        map.put("H", 4);

        map.put("I", 1);

        map.put("J", 8);

        map.put("K", 5);

        map.put("L", 1);

        map.put("M", 3);

        map.put("N", 1);

        map.put("O", 1);

        map.put("P", 3);

        map.put("Q", 10);

        map.put("R", 1);

        map.put("S", 1);

        map.put("T", 1);

        map.put("U", 1);

        map.put("V", 4);

        map.put("W", 4);

        map.put("X", 8);

        map.put("Y", 4);

        map.put("Z", 10);