Advertisement
Crenox

ArrayUtility Java Program

Oct 30th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. // Sammy Samkough
  2. // ArrayUtility
  3. // Spec: In this project you will be given the template for an ArrayUtility file. You will also be given a simple client to
  4. // use for testing purposes. Take a look at the methods and the comments for each method in ArrayUtility. Your task in this
  5. // project is to implement each of the methods shown.
  6.  
  7. import java.util.Arrays;
  8.  
  9. // This class is meant to be Procedural, that's why it's called Utility
  10. public class ArrayUtility
  11. {
  12. /** Calculates and returns the sum of the nums array
  13. * @param nums the int array to be added
  14. * @return the sum of the nums array */
  15. // Note: We have two versions of getSum - this is an example of Method Overloading
  16. // They have different signatures - the type and / or sequence of parameters
  17. public static int getSum(int[] nums)
  18. {
  19. int sum = 0;
  20.  
  21. for (int i: nums)
  22. {
  23. sum += i;
  24. }
  25.  
  26. return sum;
  27. }
  28.  
  29. /** Calculates and returns the sum of the nums array
  30. * @param nums the double array to be added
  31. * @return the sum of the nums array*/
  32. public static double getSum(double[] nums)
  33. {
  34. double sum = 0;
  35.  
  36. for (double i: nums)
  37. {
  38. sum += i;
  39. }
  40.  
  41. return sum;
  42. }
  43.  
  44. /** Calculates and returns the product of the nums array
  45. * @param nums the int array to create a product from
  46. * @return the product of each element in the nums array */
  47. public static long getProduct(int[] nums)
  48. {
  49. long product = 1;
  50.  
  51. for (long i: nums)
  52. {
  53. product *= i;
  54. }
  55. return product;
  56. }
  57.  
  58. /** Builds and returns a reverse order version of the nums array
  59. * @param nums the int array to be added
  60. * @return a reverse order version of the nums array */
  61. public static int[] reverseArray(int[] nums)
  62. {
  63. int reverse;
  64.  
  65. for (int i = 0; i < nums.length / 2; i++)
  66. {
  67. reverse = nums[i];
  68. nums[i] = nums[nums.length - 1 - i];
  69. nums[nums.length - 1 - i] = reverse;
  70. }
  71.  
  72. return nums;
  73. }
  74.  
  75. /** Searches the nums array for the maximum value
  76. * @param nums the array to be searched
  77. * @return the maximum value in the array */
  78. public static int findMax(int[] nums)
  79. {
  80. int max = nums[0];
  81.  
  82. for (int i = 0; i < nums.length; i++)
  83. {
  84. if (nums[i] > max)
  85. {
  86. max = nums[i];
  87. }
  88. }
  89.  
  90. return max;
  91. }
  92.  
  93. /** Searches the nums array for the minimum value
  94. * @param nums the array to be searched
  95. * @return the minimum value in the array */
  96. public static int findMin(int[] nums)
  97. {
  98. int min = nums[0];
  99.  
  100. for (int i = 0; i < nums.length; i++)
  101. {
  102. if (nums[i] < min)
  103. {
  104. min = nums[i];
  105. }
  106. }
  107.  
  108. return min;
  109. }
  110.  
  111. /** Searches the String word for an occurence of the target char
  112. * @param word the String to be searched
  113. * @param target the char to search for
  114. * @return true if the target char is found, false otherwise */
  115. public static boolean contains(String word, char target)
  116. {
  117. boolean found = false;
  118.  
  119. int count = 0;
  120. for(int i =0; i < word.length(); i++)
  121. {
  122. if(target == word.charAt(i))
  123. {
  124. found = true;
  125. }
  126. }
  127.  
  128. return found;
  129. }
  130.  
  131. /** Searches the nums array for an occurence of the int target
  132. * @param nums the array to be searched
  133. * @param target the int to search for
  134. * @return true if the target int is found, false otherwise */
  135. public static boolean contains(int[] nums, int target)
  136. {
  137. boolean found = false;
  138.  
  139. for (int i: nums)
  140. {
  141. if (i == target)
  142. {
  143. found = true;
  144. }
  145. }
  146.  
  147. return found;
  148. }
  149.  
  150. /** "poor-mans Encryption"... add 44 to the ascii value of each char in a String
  151. * @param word the String to be encrypted
  152. * @return an 'encrypted' version of the original String */
  153. public static String pmEncrypt(String word)
  154. {
  155. String result = "";
  156. char[] arr = new char[word.length()];
  157.  
  158. for (int i = 0; i < word.length(); i++)
  159. {
  160. arr[i] = (char)((int)word.charAt(i) + 44);
  161. }
  162.  
  163. for (int i = 0; i < arr.length; i++)
  164. {
  165. result += arr[i];
  166. }
  167.  
  168. return result;
  169. }
  170.  
  171. /** Reverses the pmEncrypt process... builds a new String by subtacting 44 from each char value in an encrypted String
  172. * @param word a pmEncrypted String
  173. * @return a 'decrypted' version of the pmEncrypted String */
  174. public static String pmDecrypt(String word)
  175. {
  176. String result = "";
  177. char[] arr = new char[word.length()];
  178.  
  179. for (int i = 0; i < word.length(); i++)
  180. {
  181. arr[i] = (char)((int)word.charAt(i) - 44);
  182. }
  183.  
  184. for (int i = 0; i < arr.length; i++)
  185. {
  186. result += arr[i];
  187. }
  188.  
  189. return result;
  190. }
  191.  
  192. /** Formats an array of Strings for console output with tabs (/t) in-between each value*/
  193. // Note: Arrays have a built-in 'toString() method that does a decent job of this already
  194. public static String arrayToString(int[] nums)
  195. {
  196. String result = "";
  197.  
  198. for (int i: nums)
  199. {
  200. result += (i + "\t");
  201.  
  202. }
  203. System.out.println(result);
  204.  
  205. return result;
  206. }
  207.  
  208. /** Formats an array of Strings for console output with tabs (/t) in-between each value*/
  209. // Note: Arrays have a built-in 'toString() method that does a decent job of this already
  210. public static String arrayToString(double[] nums)
  211. {
  212. String result = "";
  213.  
  214. for (double i: nums)
  215. {
  216. result += (i + "\t");
  217.  
  218. }
  219. System.out.println(result);
  220.  
  221. return result;
  222. }
  223. }
  224. ------------------------------------------------------------------------------------------------------------------------------
  225. // Sammy Samkough
  226. // ArrayUtility
  227. // Spec: In this project you will be given the template for an ArrayUtility file. You will also be given a simple client to
  228. // use for testing purposes. Take a look at the methods and the comments for each method in ArrayUtility. Your task in this
  229. // project is to implement each of the methods shown.
  230.  
  231. public class ArrayClient
  232. {
  233. public static void main(String[] args)
  234. {
  235. int[] intArr = {5,8,3,22,64,81};
  236. double[] dblArr = {3.4, 6.2, 9.8};
  237. int intResult;
  238. double dblResult;
  239. long lngResult;
  240. int[] intArrBackwards;
  241. String word = "APCS";
  242.  
  243. intResult = ArrayUtility.getSum(intArr);
  244. System.out.println("\nSum of integers: " + intResult);
  245.  
  246. dblResult = ArrayUtility.getSum(dblArr);
  247. System.out.println("\nSum of doubles: " + dblResult);
  248.  
  249. lngResult = ArrayUtility.getProduct(intArr);
  250. System.out.println("\nProduct of integers: " + lngResult);
  251.  
  252. System.out.println("\nInteger array forwards: ");
  253. ArrayUtility.arrayToString(intArr);
  254.  
  255. intArrBackwards = ArrayUtility.reverseArray(intArr);
  256. System.out.println("\nInteger array backwards: ");
  257. ArrayUtility.arrayToString(intArrBackwards);
  258.  
  259. intResult = ArrayUtility.findMax(intArr);
  260. System.out.println("\nMax integer: " + intResult);
  261.  
  262. intResult = ArrayUtility.findMin(intArr);
  263. System.out.println("\nMin integer: " + intResult);
  264.  
  265. System.out.println("\nfoo contains \'e\': " + ArrayUtility.contains("foo",'e'));
  266.  
  267. System.out.println("\nInteger array contains 6: " + ArrayUtility.contains(intArr, 6));
  268.  
  269. System.out.print("\n" + word);
  270. word = ArrayUtility.pmEncrypt(word);
  271. System.out.println(" encrypted is: " + word);
  272.  
  273. word = ArrayUtility.pmDecrypt(word);
  274. System.out.println("\ndecrypted it's: " + word + "\n");
  275. }
  276. }
  277. /*
  278. Sum of integers: 183
  279.  
  280. Sum of doubles: 19.4
  281.  
  282. Product of integers: 13685760
  283.  
  284. Integer array forwards:
  285. 5 8 3 22 64 81
  286.  
  287. Integer array backwards:
  288. 81 64 22 3 8 5
  289.  
  290. Max integer: 81
  291.  
  292. Min integer: 3
  293.  
  294. foo contains 'e': false
  295.  
  296. Integer array contains 6: false
  297.  
  298. APCS encrypted is: m|o¦
  299.  
  300. decrypted it's: APCS
  301.  
  302. Press any key to continue . . .
  303. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement