Guest User

Untitled

a guest
Jan 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. /* Homework3.java */
  2.  
  3. public class Homework3 {
  4.  
  5.   /**
  6.    *  smoosh() takes an array of ints.  On completion the array contains
  7.    *  the same numbers, but wherever the array had two or more consecutive
  8.    *  duplicate numbers, they are replaced by one copy of the number.  Hence,
  9.    *  after smoosh() is done, no two consecutive numbers in the array are the
  10.    *  same.
  11.    *
  12.    *  Any unused elements at the end of the array are set to -1.
  13.    *
  14.    *  For example, if the input array is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ],
  15.    *  it reads [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ] after smoosh()
  16.    *  completes.
  17.    *
  18.    *  @param ints the input array.
  19.    **/
  20.   public static void smoosh(int[] ints) {
  21.       // Create an array for the new ints
  22.       int[] smooshedInts = new int[ints.length];
  23.       // First number will always be equal:
  24.       smooshedInts[0] = ints[0];
  25.      
  26.       int index = 0;
  27.       for(int i=0; i<ints.length; i++) {
  28.           for(int j=i+1; j<ints.length; j++) {
  29.              
  30.               if (smooshedInts[index] == ints[j]) {
  31.                   // Keep moving
  32.               } else {
  33.                   // Record the new number
  34.                   index++;
  35.                   smooshedInts[index] = ints[j];
  36.                   // Start at that new number
  37.                   i = j-1;
  38.                   break;
  39.               }
  40.           }
  41.       }
  42.      
  43.       // Check the very last number:
  44.       if((smooshedInts[index] == ints[ints.length-1]) && (index != ints.length-1)) {
  45.           index++;
  46.           smooshedInts[index] = ints[ints.length-1];
  47.          
  48.           // Fill in the rest with -1's
  49.           for(int i=index; i<smooshedInts.length; i++) {
  50.               smooshedInts[i] = -1;
  51.           }
  52.       }
  53.      
  54.       // Doesn't do anything
  55.       ints = smooshedInts;
  56.       // And this doesn't do anything either
  57.       for(int i=0; i<ints.length; i++) {
  58.           ints[i] = smooshedInts[i];
  59.       }
  60.   }
  61.  
  62.   /**
  63.    *  stringInts() converts an array of ints to a String.
  64.    *  @return a String representation of the array.
  65.    **/
  66.  
  67.   private static String stringInts(int[] ints) {
  68.     String s = "[  ";
  69.     for (int i = 0; i < ints.length; i++) {
  70.       s = s + Integer.toString(ints[i]) + "  ";
  71.     }
  72.     return s + "]";
  73.   }
  74.  
  75.   /**
  76.    *  main() runs test cases on your smoosh and squish methods.  Prints summary
  77.    *  information on basic operations and halts with an error (and a stack
  78.    *  trace) if any of the tests fail.
  79.    **/
  80.  
  81.   public static void main(String[] args) {
  82.     String result;
  83.     int i;
  84.  
  85.  
  86.     System.out.println("Let's smoosh arrays!\n");
  87.  
  88.     int[] test1 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
  89.     System.out.println("smooshing " + stringInts(test1) + ":");
  90.     smoosh(test1);
  91.     result = stringInts(test1);
  92.     System.out.println(result);
  93.     TestHelper.verify(result.equals(
  94.             "[  3  7  4  5  2  0  8  5  -1  -1  -1  -1  -1  -1  ]"),
  95.                       "BAD SMOOSH!!!  No cookie.");
  96.  
  97.     int[] test2 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
  98.     System.out.println("smooshing " + stringInts(test2) + ":");
  99.     smoosh(test2);
  100.     result = stringInts(test2);
  101.     System.out.println(result);
  102.     TestHelper.verify(result.equals(
  103.             "[  6  3  6  3  6  3  -1  -1  -1  -1  -1  -1  -1  -1  -1  ]"),
  104.                       "BAD SMOOSH!!!  No cookie.");
  105.  
  106.     int[] test3 = {4, 4, 4, 4, 4};
  107.     System.out.println("smooshing " + stringInts(test3) + ":");
  108.     smoosh(test3);
  109.     result = stringInts(test3);
  110.     System.out.println(result);
  111.     TestHelper.verify(result.equals("[  4  -1  -1  -1  -1  ]"),
  112.                       "BAD SMOOSH!!!  No cookie.");
  113.  
  114.     int[] test4 = {0, 1, 2, 3, 4, 5, 6};
  115.     System.out.println("smooshing " + stringInts(test4) + ":");
  116.     smoosh(test4);
  117.     result = stringInts(test4);
  118.     System.out.println(result);
  119.     TestHelper.verify(result.equals("[  0  1  2  3  4  5  6  ]"),
  120.                       "BAD SMOOSH!!!  No cookie.");
  121.  
  122.  
  123.     System.out.println("\nLet's squish linked lists!\n");
  124.  
  125.     int[] test5 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
  126.     SList list5 = new SList();
  127.     for (i = 0; i < test5.length; i++) {
  128.       list5.insertEnd(new Integer(test5[i]));
  129.     }
  130.     System.out.println("squishing " + list5.toString() + ":");
  131.     list5.squish();
  132.     result = list5.toString();
  133.     System.out.println(result);
  134.     TestHelper.verify(result.equals("[  3  7  4  5  2  0  8  5  ]"),
  135.                       "BAD SQUISH!!!  No biscuit.");
  136.  
  137.     int[] test6 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
  138.     SList list6 = new SList();
  139.     for (i = 0; i < test6.length; i++) {
  140.       list6.insertEnd(new Integer(test6[i]));
  141.     }
  142.     System.out.println("squishing " + list6.toString() + ":");
  143.     list6.squish();
  144.     result = list6.toString();
  145.     System.out.println(result);
  146.     TestHelper.verify(result.equals("[  6  3  6  3  6  3  ]"),
  147.                       "BAD SQUISH!!!  No biscuit.");
  148.  
  149.     int[] test7 = {4, 4, 4, 4, 4};
  150.     SList list7 = new SList();
  151.     for (i = 0; i < test7.length; i++) {
  152.       list7.insertEnd(new Integer(test7[i]));
  153.     }
  154.     System.out.println("squishing " + list7.toString() + ":");
  155.     list7.squish();
  156.     result = list7.toString();
  157.     System.out.println(result);
  158.     TestHelper.verify(result.equals("[  4  ]"),
  159.                       "BAD SQUISH!!!  No biscuit.");
  160.  
  161.     int[] test8 = {0, 1, 2, 3, 4, 5, 6};
  162.     SList list8 = new SList();
  163.     for (i = 0; i < test8.length; i++) {
  164.       list8.insertEnd(new Integer(test8[i]));
  165.     }
  166.     System.out.println("squishing " + list8.toString() + ":");
  167.     list8.squish();
  168.     result = list8.toString();
  169.     System.out.println(result);
  170.     TestHelper.verify(result.equals("[  0  1  2  3  4  5  6  ]"),
  171.                       "BAD SQUISH!!!  No biscuit.");
  172.  
  173.     SList list9 = new SList();
  174.     System.out.println("squishing " + list9.toString() + ":");
  175.     list9.squish();
  176.     result = list9.toString();
  177.     System.out.println(result);
  178.     TestHelper.verify(result.equals("[  ]"),
  179.                       "BAD SQUISH!!!  No biscuit.");
  180.  
  181.  
  182.     System.out.println("\nLet's twin linked lists!\n");
  183.  
  184.     System.out.println("twinning " + list6.toString() + ":");
  185.     list6.twin();
  186.     result = list6.toString();
  187.     System.out.println(result);
  188.     TestHelper.verify(result.equals(
  189.                       "[  6  6  3  3  6  6  3  3  6  6  3  3  ]"),
  190.                       "BAD TWIN!!!  No gravy.");
  191.  
  192.     System.out.println("twinning " + list7.toString() + ":");
  193.     list7.twin();
  194.     result = list7.toString();
  195.     System.out.println(result);
  196.     TestHelper.verify(result.equals("[  4  4  ]"),
  197.                       "BAD TWIN!!!  No gravy.");
  198.  
  199.     System.out.println("twinning " + list9.toString() + ":");
  200.     list9.twin();
  201.     result = list9.toString();
  202.     System.out.println(result);
  203.     TestHelper.verify(result.equals("[  ]"),
  204.                       "BAD TWIN!!!  No gravy.");
  205.   }
  206.  
  207. }
Add Comment
Please, Sign In to add comment