Download HW09 due 11/12

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CmSc150 Fundamentals of Computing I
Homework 09 due 11/12
In this homework you have to write two programs – one in Python and one in Java. The Java program
implements two problems
Python Program: Create a plain text file (using Notepad) with 20 words, one word per line. Write a
program your_nameHW09.py that does the following:
a. reads the file into a list
b. sorts the list (recall that Python has a sort method for lists)
c. Write to another file the words without repetitions.
Sample transcript:
This program reads a file containing words (one word per line), sorts the
words, and then creates another file with the words in sorted order
without repetition.
Please give the name of the input file: wordsUnsorted.txt
Please give the name of the output file: uniqueWordsSorted.txt
Reading the input file....
Writing to the output file....
End of program
Java Program:
Task 1: Write a program that merges two sorted arrays in a third array so that the third array is sorted.
Create two arrays of random integers with different size. The user specifies the size of the two
arrays and the upper limit (use the same upper limit for both arrays).
Sort the arrays using the method selectionSort in the class ArrayMethods
Declare a third array with appropriate size (you have to figure out what the size should be)
Merge the two sorted arrays into the third array, using the following algorithm:
Sorted arrays array1, array2. Resulting array mergeArray
set indexes i, j, k to 0
while there are elements in both array1 and array2
if array1[i] < array2[j]
store array1[i] into mergeArray[k]
increment i
else
store array2[j] into mergeArray[k]
increment j
increment k
After the loop, one of the arrays will have remaining elements to be copied into mergeArray. Since it is
not known in advance which array will have remaining elements, the algorithm proceeds in this way:
1
while there are elements in array1
store array1[i] into mergeArray[k]
increment i
increment k
while there are elements in array2
store array2[j] into mergeArray[k]
increment j
increment k
Since only one array will have remaining elements, only one loop will run, the other will be skipped.
Finally,
print mergeArray
Task 2: Write a method in the ArrayMethods class that reverses an array:
public static void (int [] array)
Example:
original array: 1, 3, 2, 5
reversed array: 5, 2, 3, 1
Call the method with mergeArray (the result from Task 1) and then print it to see if your method
works properly.
Allow for multiple runs.
Sample transcript:
This program generates two arrays with random integers, sorts them and then
merges them in a third array. The third array is printed, then reversed and
printed again.
Please specify how many numbers to be generated in the first array? 4
Please specify how many numbers to be generated in the second array? 7
Please specify the upper bound of the generated numbers: 50
Generated array1: 41 16 26 34
Generated array2: 15 8 6 44 20 11 32
Sorted array1: 16 26 34 41
Sorted array2: 6 8 11 15 20 32 44
Merged array:
6 8 11 15 16 20 26 32 34 41 44
Reversed array: 44 41 34 32 26 20 16 15 11 8 6
Another run? (y/n)
How to submit: In a folder your_nameHW09 copy/move the python program, the text file, and the folder that
contains the java project. The Java project should start with your name. In the project you should have your
program (make the class start with your name) and the ArrayMethods class. Zip the folder and upload it in
Scholar.
Each program will be evaluated with respect to:

meeting the problem requirements
2

presence of author's name, general comment explaining what the program does, welcome
message, meaningful and relevant comments inside the code, meaningful names of variables.

The presence of unnecessary statements, irrelevant code and/or comments decreases the
value of the program.
Run the programs several times with different input values to make sure they run as expected.
3