CIS 67: Homework 10
Handed out: 03/27/07
Due: by 10:00pm on 04/02/07
Email program to TA
Your program consists of a class, Lab10.
The main program will in a loop
- Prompt the user to enter a line, say "roses are red, violets are blue"
- Enter the words (i.e. no delimiters, such as ',', '.' etc.) found in that line into an ArrayList
(call it 'words'), so the ArrayList words contains
{"roses", "are", "red", "violets", "are", "blue"}.
- Print out, one per line, the content of the ArrayList words.
- For each string in words, transform it into an array of characters (i.e. a string like
"roses" becomes the character array with content {'r', 'o', 's', 'e', 's'}). Put these character
arrays into an array, say wordArray (this is an array of arrays of characters).
- Now in each element of wordArray, keep the first and last position fixed, and randomly permute
the other characters. So the array {'r', 'o', 's', 'e', 's'} may be changed into
{'r', 's', 'e', 'o', 's'}
and the array {'v', 'i', 'o', 'l', 'e', 't', 's'} may be changed into
{'v', 'e', 'l', 't', 'i', 'o', 's'}.
- Now make an array of strings, stringArray, obtained by transforming each element of wordArray
into a string. So the array
{'r', 's', 'e', 'o', 's'} will become the string "rseos" and the array
{'v', 'e', 'l', 't', 'i', 'o', 's'}. will become the string "veltios".
- Print out in order on a line, separated by spaces, the content of stringArray.
So in our case you may print out
rseos are red veltios are bule
- Sort in increasing lexicographic order the stringArray and print out its content one per line.
The conceptual difficulty of this assignment is in coming up with an algorithm for randomly permuting
the specified elements of an array of characters. Try to think about it and come up with a way in 15
minutes. Then look at the following
program. You should be able to modify the method permute that you
find there into something that works in our homework.
If you can, put in your code as comments the loop invariants.
Send to the TA a case analysis for this problem: problem statement,
analysis, design, implementation, and testing.