CmSc150 Fundamentals of Computing I
Homework 10 due 11/19
Problem 1 (Java): In the ArrayMethods class, write a Java method
public static void insert(int[ ] array, int pos)
that takes an array sorted up to position pos-1 (inclusive) and rearranges the elements so that the array is sorted up to position pos (inclusive), in other words - inserts a number in a sorted array.
Example1: given array: 1, 5, 7, 4, 15, 20, pos = 3
result: 1, 4, 5, 7, 15, 20,
Example2: given array: 1, 5, 7, 30, 15, 20, pos = 3
result: 1, 5, 7, 30, 15, 20, note that here there is no change in the array, because 30 is greater than all preceding elements and the array is sorted from position 0 to position 3
Example3: given array: 2, 5, 7, 4, 1, 20, pos = 4
result: 1, 2, 4, 5, 7, 20
The idea of the algorithm is to shift all elements greater than the element in position pos one position to the right and then insert the element
Algorithm outline:
store array[pos] in a variable temp
set k = pos -1
while k >=0 and array[k] > temp
copy array[k] into array[k+1]
decrement k
store temp into array[k+1]
How to do it:
Implement the algorithm in a method
public static void insert(int [] array, int pos)
where array is the given array, and pos is the position of the element to be inserted
In the main method
a) create an array of random integers (size and upper limit specified by the user),
b) sort the array with selection sort.
c) ask the user to specify a position and a number to be stored in that position, and store the number in the specified position. Print the array so that you can see what is the input to the insert method
d) call insert method with the array and the specified position
e) print the resulting array
f) allow for multiple runs
Sample program run:
This program tests the insert method in ArrayMethods class.
Please specify the size of the array to be created: 10
Please specify the upper limit of the elements to be generated: 20
Generated and sorted array: 1 2 2 5 5 6 6 7 11 12
Please specify the position to be changed: 8
Please specify the new content of that position: 3
Array before insertion: 1 2 2 5 5 6 6 7 3 12
Array after insertion: 1 2 2 3 5 5 6 6 7 12
run again? (y/n)
y
Please specify the size of the array to be created: 5
Please specify the upper limit of the elements to be generated: 100
Generated and sorted array: 15 40 43 51 67
Please specify the position to be changed: 3
Please specify the new content of that position: 7
Array before insertion: 15 40 43 7 67
Array after insertion: 7 15 40 43 67
run again? (y/n)
y
Please specify the size of the array to be created: 5
Please specify the upper limit of the elements to be generated: 100
Generated and sorted array: 19 29 31 60 67
Please specify the position to be changed: 4
Please specify the new content of that position: 65
Array before insertion: 19 29 31 60 65
Array after insertion: 19 29 31 60 65
run again? (y/n)
n
end of program
Problem 2: Modify the class Critter in the critter_caretaker.py program so that the critter will ask for food if it is hungry (hunger >=3) and will ask for play if it is bored. Use your imagination and add a new activity for the critter and or other responses depending on its internal state (for example the critter may refuse to eat if hunger <= 1)
How to submit: In a folder your_nameHW10 copy/move the python program, 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
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.
1