The time does not have to be all in one stretch. However, it must include, in addition to all your programming time, the time for all reading that you did after you have first seen the problems and all of your non-programming time spent on the test (drawing pictures, writing sketches of code, etc).
You may use the textbook, your lecture notes, in-class examples, and other materials for this class. Other Java textbooks and online Java resources may be used with preapproval only (send me an e-mail if you plan to use any). You may not google for any problem-specific keywords.
The work is strictly individual. You may ask me questions in person or by e-mail, you may not ask anyone else any questions about any material on the test.
You may submit a file per problem, or combine files, within reason. Please include your name in comments in each file and comment the program appropriately.
Please state your assumptions about the data (the program is allowed to break if the assumptions do not hold). Your assumptions must be reasonable.
Submit all your test data and conclusions (it may be in comments or in a separate file). The conclusions may be as simple as "in all of these cases the program ran as expected". If your tests are not passing, include them in your program (you may comments them out) and indicate why you think they may not be passing.
If any of the required functionality is missing, please include that in comments. Adding notes on how you would proceed if you had more time may gain you partial credit.
Write and test a method pick
that takes a string and an
array of integers
and returns a string that contains characters of the original string at the
positions specified in the array. The elements of the array are assumed
to be in increasing order, all different, and correspond to valid indices in
the string.
The array may be empty, in which case an empty string is returned.
For instance,
pick
called with the string "structure" and the
array [1, 2, 3, 8] returns the string "true"pick
called with the the string "structure" and an empty array [] returns ""
Write and test a void method removeElements
that takes an
array list
of strings and an array of integers and removes from the given array list
all strings that are at the indices given in the array.
For instance,
removeElements
is called with
the array list containing
["lion", "panda", "panther", "tiger", "koala"]
and the array
[0, 2, 3], the array list will contain ["panda", "koala"]
removeElements
is called with an empty array, the array list remains unchanged.
Write a method copyInReverse
that takes a stack of integers and
returns a new stack of integers that has all of the same elements,
but in reverse order. The given stack is unchanged after the method call.
The given stack may be empty, in which case an empty stack is returned.
You may use any data structure as intermediate storage. You may only use
pop, push, peek,
and empty
as stack methods.
Write a method relativelyPrime
that takes an array of
non-zero integers and returns true>
if all of the elements are relatively prime to each other, and
false
otherwise.
Two integers are relatively prime if their greatest common divisor is 1.
There is a recursive algorithm for finding the greatest common divisor,
known as Euclidean algorithm. Feel free to use it (and you can google
for it if you would like to), although there are other approaches
to this problem.
Examples of the method results:
true
since none of the
numbers have
common divisors other than 1false
since 22 and 18 are
both divisible by 2.true
since there is no pair of numbers
that have a common divisor.
Likewise the method returns true for an empty array. Your solution will be graded on:
Please submit it by e-mail to me with the subject 2101 Midterm 1.