Advertisement
ruthiek

Untitled

Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2. public class ArrayManip
  3. {
  4. public static void main(String [] args)
  5. {
  6. testSearchNum();
  7. testHasDuplicates();
  8. testRemoveDuplicates();
  9. testContainsAll();
  10. testCommonElements();
  11. }
  12. public static boolean searchNum(int [] myArray, int num)
  13. {
  14. boolean containsNum = false;
  15. for(int i = 0 ; i < myArray.length ; i++)
  16. {
  17. if(myArray[i] == num)
  18. containsNum = true;
  19. }
  20. return containsNum;
  21. }
  22. public static void testSearchNum()
  23. {
  24. String result = "";
  25. int x;
  26. String msg1 = "Please specify the size of the array";
  27. x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
  28. int[] myArray = new int[x];
  29. String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
  30. String input = JOptionPane.showInputDialog(null, msg2);
  31. String[] temp = new String[x];
  32. temp = input.split(" ");
  33. for(int i = 0 ; i < x ; i++)
  34. myArray[i] = Integer.parseInt(temp[i]);
  35. String msg3 = "Please enter the number you would like to search for";
  36. int num = Integer.parseInt(JOptionPane.showInputDialog(null, msg3));
  37. boolean contains = searchNum(myArray,num);
  38. if(contains)
  39. result += "Array contains number";
  40. else
  41. result += "Array does not contain number";
  42. JOptionPane.showMessageDialog(null, result);
  43. }
  44. public static boolean hasDuplicates(int[] listA)
  45. {
  46. boolean duplicates = false;
  47. for(int i = 0 ; i < listA.length ; i++)
  48. {
  49. for(int j = i+1 ; j < listA.length ; j++)
  50. {
  51. if(listA[i] == listA[j])
  52. duplicates = true;
  53. }
  54. }
  55. return duplicates;
  56. }
  57. public static void testHasDuplicates()
  58. {
  59. String result = "";
  60. int x;
  61. String msg1 = "Please specify the size of the array";
  62. x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
  63. int[] listA = new int[x];
  64. String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
  65. String input = JOptionPane.showInputDialog(null, msg2);
  66. String[] temp = new String[x];
  67. temp = input.split(" ");
  68. for(int i = 0 ; i < x ; i++)
  69. listA[i] = Integer.parseInt(temp[i]);
  70. boolean duplicates = hasDuplicates(listA);
  71. if(duplicates)
  72. result += "Duplicates found";
  73. else
  74. result += "No duplicates found";
  75. JOptionPane.showMessageDialog(null, result);
  76. }
  77. public static int[] removeDuplicates(int[] listA)
  78. {
  79. int k = listA.length;
  80. for(int i = 0 ; i < k - 1 ; i++)
  81. {
  82. for(int j = i+1 ; j < k ; )
  83. {
  84. if(listA[i] == listA[j] && j < k)
  85. {
  86. listA[j] = listA[k-1];
  87. k--;
  88. }
  89. else
  90. j++;
  91. }
  92. }
  93. int[] noDuplicates = new int[k];
  94. for(int x = 0 ; x < k ; x++)
  95. noDuplicates[x] = listA[x];
  96.  
  97. return noDuplicates;
  98. }
  99. public static void testRemoveDuplicates()
  100. {
  101. String result = "";
  102. int x;
  103. String msg1 = "Please specify the size of the array";
  104. x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
  105. int[] listA = new int[x];
  106. String msg2 = "Please enter " + x + " integers(allow a space between each integer)";
  107. String input = JOptionPane.showInputDialog(null, msg2);
  108. String[] temp = new String[x];
  109. temp = input.split(" ");
  110. for(int i = 0 ; i < x ; i++)
  111. listA[i] = Integer.parseInt(temp[i]);
  112.  
  113. int[] newArray = removeDuplicates(listA);
  114.  
  115. String[] myArray = new String[newArray.length];
  116.  
  117. for(int j = 0 ; j < newArray.length ; j++)
  118. myArray[j] = Integer.toString(newArray[j]);
  119. for(int y = 0 ; y < myArray.length ; y++)
  120. result += myArray[y] + " ";
  121.  
  122. JOptionPane.showMessageDialog(null, result);
  123. }
  124. public static boolean containsAll(int [] listA, int[] listB)
  125. {
  126. boolean contains = false;
  127. for(int i = 0; i < listA.length ; i++)
  128. {
  129. contains = false;
  130. for(int j = 0; j < listB.length && !contains ; j++)
  131. if(listA[i] == listB[j])
  132. contains = true;
  133. }
  134. return contains;
  135. }
  136. public static void testContainsAll()
  137. {
  138. String result = "";
  139. int x, y;
  140. String msg1 = "Please specify the size of listA";
  141. x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
  142. String msg2 = "Please specify the size of listB";
  143. y = Integer.parseInt(JOptionPane.showInputDialog(null, msg2));
  144. String msg3 = "Please enter " + x + " integers(allow a space between each integer)";
  145. String input1 = JOptionPane.showInputDialog(null, msg3);
  146. String msg4 = "Please enter " + y + " integers(allow a space between each integer)";
  147. String input2 = JOptionPane.showInputDialog(null, msg4);
  148. String[] temp1 = new String[x];
  149. temp1 = input1.split(" ");
  150. String[] temp2 = new String[y];
  151. temp2 = input2.split(" ");
  152. int[] listA = new int[x];
  153. int[] listB = new int[y];
  154. for(int i = 0 ; i < x ; i++)
  155. listA[i] = Integer.parseInt(temp1[i]);
  156. for(int j = 0 ; j < y ; j++)
  157. listB[j] = Integer.parseInt(temp2[j]);
  158. boolean subset = containsAll(listA, listB);
  159. if(subset)
  160. result += "All the elements in listA are in listB";
  161. else
  162. result += "All the elements in listA are not in listB";
  163. JOptionPane.showMessageDialog(null, result);
  164. }
  165. public static int[] commonElements(int[] listA, int[] listB)
  166. {
  167. int[] temp = new int[listA.length];
  168. int count = 0;
  169. boolean found = false;
  170. for(int i = 0; i < listA.length; i++)
  171. {
  172. found = false;
  173. for(int j = 0 ; j < listB.length && !found ; j ++)
  174. {
  175. if(listA[i]==listB[j])
  176. {
  177. found = true;
  178. temp[count] = listA[i];
  179. count++;
  180. }
  181. }
  182. }
  183. int[] commonList = new int[count];
  184. for(int k = 0 ; k < count ; k++)
  185. commonList[k] = temp[k];
  186. return commonList;
  187.  
  188. }
  189. public static void testCommonElements()
  190. {
  191.  
  192. String result = "Common Elements from listA and listB are:\n";
  193. int x, y;
  194. String msg1 = "Please specify the size of listA";
  195. x = Integer.parseInt(JOptionPane.showInputDialog(null, msg1));
  196. String msg2 = "Please specify the size of listB";
  197. y = Integer.parseInt(JOptionPane.showInputDialog(null, msg2));
  198. String msg3 = "Please enter " + x + " integers(allow a space between each integer)";
  199. String input1 = JOptionPane.showInputDialog(null, msg3);
  200. String msg4 = "Please enter " + y + " integers(allow a space between each integer)";
  201. String input2 = JOptionPane.showInputDialog(null, msg4);
  202. String[] temp1 = new String[x];
  203. temp1 = input1.split(" ");
  204. String[] temp2 = new String[y];
  205. temp2 = input2.split(" ");
  206. int[] listA = new int[x];
  207. int[] listB = new int[y];
  208. for(int i = 0 ; i < x ; i++)
  209. listA[i] = Integer.parseInt(temp1[i]);
  210. for(int j = 0 ; j < y ; j++)
  211. listB[j] = Integer.parseInt(temp2[j]);
  212. int[] common = commonElements(listA, listB);
  213. int[] common2 = removeDuplicates(common);
  214. for(int k = 0 ; k < common2.length; k ++)
  215. result += common2[k] + "\t";
  216. JOptionPane.showMessageDialog(null, result);
  217. }
  218. public static int[] joinArrays(int[] listA, int[] listB)
  219. {
  220. int length = listA.length + listB.length;
  221. int[] joined = new int[length]
  222. int j=0;
  223. for(int i = 0; i < listA.length ; i++)
  224. {
  225. joined[i] = listA[i];
  226. j = j+1;
  227. }
  228. for(int k = j ; k < listB.length ; k++)
  229. joined[k] = listB[k];
  230. return joined;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement