2004 ICS4M FINAL EXAM-Java

Wednesday, June 9th. 9:00 a.m.

INSTRUCTIONS TO STUDENTS.
  1. Students will complete ONE question from the following THREE alternatives.
  2. It is imperative that you name your files exactly as requested. Attach all (and only) the requested files to your email to handin at the end of the exam.
  3. You are allowed to use your textbooks and your Z: drive.
  4. You are NOT allowed to access the internet at any time during the exam.
  5. Good luck.
Question 1. RECURSIVE ALGORITHM. (Degree of Difficulty:1.2)

Permutations

Preamble. The ability to generate the set of all possible permutations of a string of characters is a useful utility to have. A permutation is simply a rearrangement of letters. For example, the string "stop" has 24 permutations.

Here's an idea of how to generate the permutations recursively. Consider the string "stop" and let's simplify the problem. First, we'll generate all permutations that start with the letter 's', then those that start with 't', and so on. How do we generate the permutations that start with 's'? We need to know the permutations of the string "top". But that's the same problem - to generate all permutations-with a simpler input, namely the shorter string "top". Thus, we can use recursion. For each permutation of the substring "top", we can prepend the letter 's' to get the permutations of "top" that start with 's', namely,

stop
stpo
sotp
sopt
spto
spot

Now let's turn our attention to the permutations of "stop" that start with 't'. We need to create the permutations of the remaining letters "sop". That process will yield

sop
spo
osp
ops
pso
pos

We add the letter 't' to the front of the strings and obtain,

tsop
tspo
tosp
tops
tpso
tpos

We generate all the permutations that start with 'o' and 'p' in the same way.

Task. Write the Java application, Permutations.java, that will produce output similar to the screen capture above. You might consider generating all permutations of the original string and storing them in a TreeSet or some such structure for easy lookup of the possible permutation.

Question 2. DATA STRUCTURE. (Degree of Difficulty:1.0)

CircularList

Preamble. In the children's game hot potato, a group of n children sit in a circle passing an object, called the "potato", around the circle. The potato begins with a starting child in the circle, and the children continue passing the potato until a leader rings a bell, at which point the child holding the potato must leave the game after handing the potato to the next child in the circle. After the selected child leaves, the other children close up the circle. This process is then continued until there is only one child remaining, who is declared the winner.

Task. Develop the Java application, HotPotato.java, that simulates this game. In support of your application, you will incoporate the class, CircularList.java, that encapsulates a LinkedList object to model the circular list (ring). Your CircularList class should contain overloaded constructors of the form,

public class CircularList( Object[] L, int k)

so that it could handle arrays of differnt types. To confirm that your code is working, CircularList({1,2,3,4,5},3) is 1, and CircularList({1,2,3},2) is 2. Assume the game begins with the the hot potato being at the first object in L.

Submit the two classes, with HotPotato.java including the following tests:

L=(Alice,Bob,Charles,Darwin,Ed,Fred,George,Holly,Irene), k=5
L=(Canada,USA,Iraq,Syria,Iran,France,Germany,England), k=8
L=(Mike), k=2
L=(23,34,104,1025,2,15,27), k=4
L=(2.3,3.4,10.4,10.25,2.2,1.5,2.7), k=6

Note that the first three of the above are arrays of are Strings, whereas the fourth is an array of integers, and the last one is an array of doubles.

Questions 3. Java3D. (Degree of Difficulty:1.1)

Java3D

Develop a Java3D scene graph for any one of the following objects. Include Mouse Rotate and Mouse Zoom capabilities into the scene. Click on any thumbnail image for a larger view.