Examples of smaller Java programs that do various interesting things.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //Mike Scott
  2. //examples of array manipulations
  3. public class ArrayExamples
  4. { public static void main(String[] args)
  5. { int[] list = {1, 2, 3, 4, 1, 2, 3};
  6. findAndPrintPairs(list, 5);
  7. bubblesort(list);
  8. showList(list);
  9. list = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  10. bubblesort(list);
  11. showList(list);
  12. list = new int[]{11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
  13. bubblesort(list);
  14. showList(list);
  15. list = new int[]{1};
  16. bubblesort(list);
  17. showList(list);
  18. }
  19. // pre: list != null, list.length > 0
  20. // post: return index of minimum element of array
  21. public static int findMin(int[] list)
  22. { assert list != null && list.length > 0 : "failed precondition";
  23. int indexOfMin = 0;
  24. for(int i = 1; i < list.length; i++)
  25. { if(list[i] < list[indexOfMin])
  26. { indexOfMin = i;
  27. }
  28. }
  29. return indexOfMin;
  30. }
  31. /*
  32. *pre: list != null, newSize >= 0
  33. *post: nothing. the method does not succeed it resizing the
  34. * argument
  35. */
  36. public static void badResize(int[] list, int newSize)
  37. { assert list != null && newSize >= 0 : "failed precondition";
  38. int[] temp = new int[newSize];
  39. int limit = Math.min(list.length, newSize);
  40. for(int i = 0; i < limit; i++)
  41. { temp[i] = list[i];
  42. }
  43. // uh oh!! Changing pointer, not pointee. This breaks the
  44. // relationship between the parameter and argument
  45. list = temp;
  46. }
  47. /*
  48. *pre: list != null, newSize >= 0
  49. *post: returns an array of size newSize. Elements from 0 to newSize - 1
  50. * will be copied into the new array
  51. */
  52. public static int[] goodResize(int[] list, int newSize)
  53. { assert list != null && newSize >= 0 : "failed precondition";
  54. int[] result = new int[newSize];
  55. int limit = Math.min(list.length, newSize);
  56. for(int i = 0; i < limit; i++)
  57. { result[i] = list[i];
  58. }
  59. return result;
  60. }
  61. /*
  62. *pre: list != null
  63. *post: prints out the indices and values of all pairs of numbers
  64. *in list such that list[a] + list[b] = target
  65. */
  66. public static void findAndPrintPairs(int[] list, int target)
  67. { assert list != null : "failed precondition";
  68. for(int i = 0; i < list.length; i++)
  69. { for(int j = i + 1; j < list.length; j++)
  70. { if(list[i] + list[j] == target)
  71. { System.out.println("The two elements at indices " + i + " and " + j
  72. + " are " + list[i] + " and " + list[j] + " add up to " + target);
  73. }
  74. }
  75. }
  76. }
  77. /*
  78. *pre: list != null;
  79. *post: sort the elements of list so that they are in ascending order
  80. */
  81. public static void bubblesort(int[] list)
  82. {
  83. assert list != null : "failed precondition";
  84. int temp;
  85. boolean changed = true;
  86. for(int i = 0; i < list.length && changed; i++)
  87. { changed = false;
  88. for(int j = 0; j < list.length - i - 1; j++)
  89. { assert (j > 0) && (j + 1 < list.length) : "loop counter j " + j +
  90. "is out of bounds.";
  91. if(list[j] > list[j+1])
  92. { changed = true;
  93. temp = list[j + 1];
  94. list[j + 1] = list[j];
  95. list[j] = temp;
  96. }
  97. }
  98. }
  99. assert isAscending( list );
  100. }
  101. public static void showList(int[] list)
  102. { for(int i = 0; i < list.length; i++)
  103. System.out.print( list[i] + " " );
  104. System.out.println();
  105. }
  106. /* pre: list != null
  107. post: return true if list is sorted in ascedning order, false otherwise
  108. */
  109. public static boolean isAscending( int[] list )
  110. { boolean ascending = true;
  111. int index = 1;
  112. while( ascending && index < list.length )
  113. { assert index >= 0 && index < list.length;
  114. ascending = (list[index - 1] <= list[index]);
  115. index++;
  116. }
  117. return ascending;
  118. }
  119. }