CSci 2101 Takehome exam 1.

Due Tuesday, September 27th at 11:59pm

You may not spend more than 6 hours on this test.

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).

30 points

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.

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 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.

Problem 1 (10 points):

Write a method containsAll that takes two arrays and returns true if all elements of the first array appear in the second one and false otherwise. For instance, if arr1 is [1, 2, 3] and arr2 is [4, 3, 1, 5, 2, 2] then containsAll(arr1, arr2) returns true and containsAll(arr2, arr1) returns false.

If the first array has repeated elements then that element is required to appear in the second array at least once, but it's not required to appear the same number of times as in the first array. For instance, if arr1 is [1, 1] and arr2 is [1] then containsAll(arr2, arr1) returns true. None of the arrays are assumed to be non-empty.

You may write helper methods if you prefer.

Problem 2 (8 points): adding an element to a stack.

Write a method that takes a stack of integers and an int element and returns another stack that is the same as the original one, but with the element placed between every two elements of the original stack. For instance, if you are given a stack [1, 2, 3] and an element 5, the returned stack will be [1, 5, 2, 5, 3]. The original stack must remain unchanged after the method call.

If you are given an empty stack or a stack of one element, the returned stack should just be a copy of the original stack.

Problem 3 (12 points): removing the smallest item of from a stack of integers.

Write and test a void method that takes a stack of integers and removes its smallest element. The stack must contain all of the other original elements in the original order after the method returns. If there are several occurrences of the smallest element then all of them have to be removed. You may assume that the given stack is non-empty.

How to submit

Please submit it by e-mail to me with the subject 2101 Midterm I.


CSci 2101 course web site.