midterm-sample-5 - CSE 143 Section Handout #12 Practice...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Section Handout #12 Practice Midterm #5 1. ArrayList Mystery . Consider the following method: public static void mystery5(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i++) { int element = list.get(i); list.remove(i); list.add(0, element + 1); } System.out.println(list); } Write the output produced by the method when passed each of the following ArrayList s: List Output (a) [10, 20, 30] ____________________________________ (b) [8, 2, 9, 7, 4] ____________________________________ (c) [-1, 3, 28, 17, 9, 33] ____________________________________
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 143 Section Handout #12 2. ArrayList Programming . Write a method filterRange that accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max (inclusive) from the list. For example, if a variable called list stores the values: [4, 7 , 9, 2, 7, 7, 5 , 3, 5 , 1, 7 , 8, 6, 7 ] The call of filterRange(list, 5, 7); should remove all values between 5 and 7, therefore it should change the list to store [4, 9, 2, 3, 1, 8] . If no elements in range min - max are found in the list, the list's contents are unchanged. If an empty list is passed, the list remains empty. You may assume that the list is not null .
Background image of page 2
CSE 143 Section Handout #12 3. Stack and Queue Programming . Write a method removeMin that accepts a stack of integers as a parameter and removes and returns the smallest value from the stack. For example, if a variable s stores: bottom [2, 8, 3, 19, 7, 3, 2, 42, 9, 3, 2, 7, 12, -8, 4] top And we make the following call: int n = removeMin(s); The method removes and returns -8 , so n will store -8 after the call and s will store the following values: bottom [2, 8, 3, 19, 7, 3, 2, 42, 9, 3, 2, 7, 12, 4] top If the minimum value appears more than once, all occurrences of it should be removed. For example, given the stack above, if we again call removeMin(s); , it would return 2 and would leave the stack as follows: bottom [8, 3, 19, 7, 3, 42, 9, 3, 7, 12, 4] top You may use one queue as auxiliary storage. You may not use any other structures to solve this problem, although you can have as many primitive variables as you like. You may not solve the problem recursively. You may assume that the stack is not empty. For full credit, your solution must run in O( n ) time. You have access to the following two methods and may call them as needed to help you solve the problem: public static void s2q (Stack<Integer> s, Queue<Integer> q) { while (!s.isEmpty()) { q.add(s.pop()); // Transfers the entire contents } // of stack s to queue q } public static void
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

midterm-sample-5 - CSE 143 Section Handout #12 Practice...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online