/* Homework3.java */ public class Homework3 { /** * smoosh() takes an array of integers and returns an array containing the * same numbers, but wherever the input array has two or more consecutive * duplicate numbers, they are replaced in the output array by one copy of * the number. Hence, no two consecutive numbers in the output array are * the same. * * The output array is the same length as the input array. Any unused * elements at the end are set to -1. * * For example, if the input array is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ], the * output array is [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ]. * * DO NOT CHANGE THE INPUT ARRAY! * DO NOT CHANGE THE INPUT ARRAY! * DO NOT CHANGE THE INPUT ARRAY! * YOU MUST RETURN A _NEW_ ARRAY! * * @param ints the input array. * @return the compressed array with no two consecutive duplicate numbers. **/ private static int[] smoosh(int[] ints) { // Fill in your solution here. (Ours is sixteen lines long.) return new int[0]; } /** * stringInts() converts an array of ints to a String. * @return a String representation of the array. **/ private static String stringInts(int[] ints) { String s = "[ "; for (int i = 0; i < ints.length; i++) { s = s + Integer.toString(ints[i]) + " "; } return s + "]"; } /** * main() runs test cases on your smoosh and squish methods. Prints summary * information on basic operations and halts with an error (and a stack * trace) if any of the tests fail. **/ public static void main(String[] args) { String result; int i; System.out.println("Let's smoosh arrays!\n"); int[] test1 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5}; System.out.println("smooshing " + stringInts(test1) + ":"); result = stringInts(smoosh(test1)); System.out.println(result); TestHelper.verify(result.equals( "[ 3 7 4 5 2 0 8 5 -1 -1 -1 -1 -1 -1 ]"), "BAD SMOOSH!!! No cookie."); int[] test2 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3}; System.out.println("smooshing " + stringInts(test2) + ":"); result = stringInts(smoosh(test2)); System.out.println(result); TestHelper.verify(result.equals( "[ 6 3 6 3 6 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 ]"), "BAD SMOOSH!!! No cookie."); int[] test3 = {4, 4, 4, 4, 4}; System.out.println("smooshing " + stringInts(test3) + ":"); result = stringInts(smoosh(test3)); System.out.println(result); TestHelper.verify(result.equals("[ 4 -1 -1 -1 -1 ]"), "BAD SMOOSH!!! No cookie."); int[] test4 = {0, 1, 2, 3, 4, 5, 6}; System.out.println("smooshing " + stringInts(test4) + ":"); result = stringInts(smoosh(test4)); System.out.println(result); TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"), "BAD SMOOSH!!! No cookie."); System.out.println("\nLet's squish linked lists!\n"); SList list1 = new SList(); for (i = 0; i < test1.length; i++) { list1.insertEnd(new Integer(test1[i])); } System.out.println("squishing " + list1.toString() + ":"); list1.squish(); result = list1.toString(); System.out.println(result); TestHelper.verify(result.equals("[ 3 7 4 5 2 0 8 5 ]"), "BAD SQUISH!!! No biscuit."); SList list2 = new SList(); for (i = 0; i < test2.length; i++) { list2.insertEnd(new Integer(test2[i])); } System.out.println("squishing " + list2.toString() + ":"); list2.squish(); result = list2.toString(); System.out.println(result); TestHelper.verify(result.equals("[ 6 3 6 3 6 3 ]"), "BAD SQUISH!!! No biscuit."); SList list3 = new SList(); for (i = 0; i < test3.length; i++) { list3.insertEnd(new Integer(test3[i])); } System.out.println("squishing " + list3.toString() + ":"); list3.squish(); result = list3.toString(); System.out.println(result); TestHelper.verify(result.equals("[ 4 ]"), "BAD SQUISH!!! No biscuit."); SList list4 = new SList(); for (i = 0; i < test4.length; i++) { list4.insertEnd(new Integer(test4[i])); } System.out.println("squishing " + list4.toString() + ":"); list4.squish(); result = list4.toString(); System.out.println(result); TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"), "BAD SQUISH!!! No biscuit."); SList list5 = new SList(); System.out.println("squishing " + list5.toString() + ":"); list5.squish(); result = list5.toString(); System.out.println(result); TestHelper.verify(result.equals("[ ]"), "BAD SQUISH!!! No biscuit."); System.out.println("\nLet's twin linked lists!\n"); System.out.println("twinning " + list2.toString() + ":"); list2.twin(); result = list2.toString(); System.out.println(result); TestHelper.verify(result.equals( "[ 6 6 3 3 6 6 3 3 6 6 3 3 ]"), "BAD TWIN!!! No gravy."); System.out.println("twinning " + list3.toString() + ":"); list3.twin(); result = list3.toString(); System.out.println(result); TestHelper.verify(result.equals("[ 4 4 ]"), "BAD TWIN!!! No gravy."); System.out.println("twinning " + list5.toString() + ":"); list5.twin(); result = list5.toString(); System.out.println(result); TestHelper.verify(result.equals("[ ]"), "BAD TWIN!!! No gravy."); } }