// Program to test solutions to problem #6 on the cse143 midterm, spring 2008 // Fill in your solution to removeOddPositions, then compile and run the // program import java.util.*; class ArrayIntList { public void removeOddPositions() { // fill in your solution here } private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // post: constructs an empty list of default capacity public ArrayIntList() { this(DEFAULT_CAPACITY); } // this is the sample solution public void removeOddPositions2() { size = (size + 1) / 2; for (int i = 0; i < size; i++) elementData[i] = elementData[2 * i]; } // post: list is empty public void clear() { this.size = 0; } // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { this.elementData = new int[capacity]; this.size = 0; } // post: returns the current number of elements in the list public int size() { return this.size; } // pre : 0 <= index < size() // post: returns the integer at the given index in the list public int get(int index) { return this.elementData[index]; } // post: creates a comma-separated, bracketed version of the list public String toString() { String result = "["; if (this.size > 0) { result += this.elementData[0]; for (int i = 1; i < this.size; i++) result += ", " + this.elementData[i]; } result += "]"; return result; } // post : returns the position of the first occurence of the given // value (-1 if not found) public int indexOf(int value) { for(int i = 0; i < this.size; i++) if (this.elementData[i] == value) return i; return -1; } // pre : size() < capacity (elementData.length) // post: appends the given value to the end of the list public void add(int value) { this.elementData[this.size] = value; this.size++; } // pre: size() < capacity (elementData.length) && 0 <= index <= size() // post: inserts the given value at the given index, shifting subsequent // values right public void add(int index, int value) { for (int i = this.size; i > index; i--) this.elementData[i] = this.elementData[i - 1]; this.elementData[index] = value; this.size++; } // pre : 0 <= index < size() // post: removes value at the given index, shifting subsequent values left public void remove(int index) { for (int i = index; i < this.size - 1; i++) this.elementData[i] = this.elementData[i + 1]; this.size--; } } public class Test6 { public static void main(String[] args) { test(new int[] {1, 2, 3, 4, 5, 6}); test(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}); test(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); test(new int[] {8, 13, 17, 4, 9, 12, 98}); test(new int[] {}); test(new int[] {1}); test(new int[] {1, 2}); test(new int[] {1, 2, 3}); } public static void test(int[] data) { ArrayIntList list1 = new ArrayIntList(data.length); ArrayIntList list2 = new ArrayIntList(data.length); for (int n : data) { list1.add(n); list2.add(n); } boolean fail = false; do { System.out.println("testing removeOddPositions for " + list1); list1.removeOddPositions2(); System.out.println(" next result should = " + list1); try { list2.removeOddPositions(); } catch (RuntimeException e) { System.out.println(" threw " + e); fail = true; } if (!fail && !list1.toString().equals(list2.toString())) { System.out.println(" actual result = " + list2); fail = true; } if (!fail) System.out.println(" success"); } while (!fail && list1.size() > 1); if (fail) System.out.println("failed"); else System.out.println("passed"); System.out.println(); } }