Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Homework3 {
- /**
- * smoosh() takes an array of ints. On completion the array contains
- * the same numbers, but wherever the array had two or more consecutive
- * duplicate numbers, they are replaced by one copy of the number. Hence,
- * after smoosh() is done, no two consecutive numbers in the array are the
- * same.
- *
- * Any unused elements at the end of the array 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 ],
- * it reads [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ] after smoosh()
- * completes.
- *
- * @param ints the input array.
- **/
- public static void smoosh(int[] a) {
- // Fill in your solution here. (Ours is fourteen lines long, not counting
- // blank lines or lines already present in this file.)
- int cP = 1;
- int i, j = 0;
- for(i =0; i < a.length; i++){
- int flag = 0;
- for(j = cP; j < a.length; j++){
- if(a[j] != a[i])
- {
- a[i+1] = a[j];
- cP = ++j;
- flag = 1;
- break;
- }
- }
- if(j == a.length){
- if(flag == 1)
- i+=2;
- else
- i += 1;
- for(int k = i; k < a.length; k++)
- a[k] = -1;
- break;
- }
- }
- }
- /**
- * 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) + ":");
- smoosh(test1);
- result = stringInts(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) + ":");
- smoosh(test2);
- result = stringInts(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) + ":");
- smoosh(test3);
- result = stringInts(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) + ":");
- smoosh(test4);
- result = stringInts(test4);
- System.out.println(result);
- TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"),
- "BAD SMOOSH!!! No cookie.");
- } /* end main*/
- }/* end Homework3 class */
- public class TestHelper {
- /**
- * verify() checks an invariant and prints an error message if it fails.
- * If invariant is true, this method does nothing. If invariant is false,
- * the message is printed, followed by a dump of the program call stack.
- *
- * @param invariant the condition to be verified
- * @param message the error message to be printed if the invariant fails to
- * hold true.
- **/
- static void verify(boolean invariant, String message) {
- if (!invariant) {
- System.out.println("*** ERROR: " + message);
- Thread.dumpStack();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement