Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. public class Homework3 {
  2.  
  3. /**
  4. * smoosh() takes an array of ints. On completion the array contains
  5. * the same numbers, but wherever the array had two or more consecutive
  6. * duplicate numbers, they are replaced by one copy of the number. Hence,
  7. * after smoosh() is done, no two consecutive numbers in the array are the
  8. * same.
  9. *
  10. * Any unused elements at the end of the array are set to -1.
  11. *
  12. * For example, if the input array is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ],
  13. * it reads [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ] after smoosh()
  14. * completes.
  15. *
  16. * @param ints the input array.
  17. **/
  18.  
  19. public static void smoosh(int[] a) {
  20. // Fill in your solution here. (Ours is fourteen lines long, not counting
  21. // blank lines or lines already present in this file.)
  22. int cP = 1;
  23. int i, j = 0;
  24. for(i =0; i < a.length; i++){
  25. int flag = 0;
  26. for(j = cP; j < a.length; j++){
  27. if(a[j] != a[i])
  28. {
  29. a[i+1] = a[j];
  30. cP = ++j;
  31. flag = 1;
  32. break;
  33. }
  34. }
  35.  
  36. if(j == a.length){
  37. if(flag == 1)
  38. i+=2;
  39. else
  40. i += 1;
  41. for(int k = i; k < a.length; k++)
  42. a[k] = -1;
  43. break;
  44. }
  45. }
  46. }
  47.  
  48. /**
  49. * stringInts() converts an array of ints to a String.
  50. * @return a String representation of the array.
  51. **/
  52.  
  53. private static String stringInts(int[] ints) {
  54. String s = "[ ";
  55. for (int i = 0; i < ints.length; i++) {
  56. s = s + Integer.toString(ints[i]) + " ";
  57. }
  58. return s + "]";
  59. }
  60.  
  61. /**
  62. * main() runs test cases on your smoosh and squish methods. Prints summary
  63. * information on basic operations and halts with an error (and a stack
  64. * trace) if any of the tests fail.
  65. **/
  66.  
  67. public static void main(String[] args) {
  68. String result;
  69. int i;
  70.  
  71.  
  72. System.out.println("Let's smoosh arrays!n");
  73.  
  74. int[] test1 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
  75. System.out.println("smooshing " + stringInts(test1) + ":");
  76. smoosh(test1);
  77. result = stringInts(test1);
  78. System.out.println(result);
  79. TestHelper.verify(result.equals(
  80. "[ 3 7 4 5 2 0 8 5 -1 -1 -1 -1 -1 -1 ]"),
  81. "BAD SMOOSH!!! No cookie.");
  82.  
  83. int[] test2 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
  84. System.out.println("smooshing " + stringInts(test2) + ":");
  85. smoosh(test2);
  86. result = stringInts(test2);
  87. System.out.println(result);
  88. TestHelper.verify(result.equals(
  89. "[ 6 3 6 3 6 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 ]"),
  90. "BAD SMOOSH!!! No cookie.");
  91.  
  92. int[] test3 = {4, 4, 4, 4, 4};
  93. System.out.println("smooshing " + stringInts(test3) + ":");
  94. smoosh(test3);
  95. result = stringInts(test3);
  96. System.out.println(result);
  97. TestHelper.verify(result.equals("[ 4 -1 -1 -1 -1 ]"),
  98. "BAD SMOOSH!!! No cookie.");
  99.  
  100. int[] test4 = {0, 1, 2, 3, 4, 5, 6};
  101. System.out.println("smooshing " + stringInts(test4) + ":");
  102. smoosh(test4);
  103. result = stringInts(test4);
  104. System.out.println(result);
  105. TestHelper.verify(result.equals("[ 0 1 2 3 4 5 6 ]"),
  106. "BAD SMOOSH!!! No cookie.");
  107.  
  108. } /* end main*/
  109. }/* end Homework3 class */
  110.  
  111. public class TestHelper {
  112.  
  113. /**
  114. * verify() checks an invariant and prints an error message if it fails.
  115. * If invariant is true, this method does nothing. If invariant is false,
  116. * the message is printed, followed by a dump of the program call stack.
  117. *
  118. * @param invariant the condition to be verified
  119. * @param message the error message to be printed if the invariant fails to
  120. * hold true.
  121. **/
  122.  
  123. static void verify(boolean invariant, String message) {
  124. if (!invariant) {
  125. System.out.println("*** ERROR: " + message);
  126. Thread.dumpStack();
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement