Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // ArrayUtility
- // Spec: In this project you will be given the template for an ArrayUtility file. You will also be given a simple client to
- // use for testing purposes. Take a look at the methods and the comments for each method in ArrayUtility. Your task in this
- // project is to implement each of the methods shown.
- import java.util.Arrays;
- // This class is meant to be Procedural, that's why it's called Utility
- public class ArrayUtility
- {
- /** Calculates and returns the sum of the nums array
- * @param nums the int array to be added
- * @return the sum of the nums array */
- // Note: We have two versions of getSum - this is an example of Method Overloading
- // They have different signatures - the type and / or sequence of parameters
- public static int getSum(int[] nums)
- {
- int sum = 0;
- for (int i: nums)
- {
- sum += i;
- }
- return sum;
- }
- /** Calculates and returns the sum of the nums array
- * @param nums the double array to be added
- * @return the sum of the nums array*/
- public static double getSum(double[] nums)
- {
- double sum = 0;
- for (double i: nums)
- {
- sum += i;
- }
- return sum;
- }
- /** Calculates and returns the product of the nums array
- * @param nums the int array to create a product from
- * @return the product of each element in the nums array */
- public static long getProduct(int[] nums)
- {
- long product = 1;
- for (long i: nums)
- {
- product *= i;
- }
- return product;
- }
- /** Builds and returns a reverse order version of the nums array
- * @param nums the int array to be added
- * @return a reverse order version of the nums array */
- public static int[] reverseArray(int[] nums)
- {
- int reverse;
- for (int i = 0; i < nums.length / 2; i++)
- {
- reverse = nums[i];
- nums[i] = nums[nums.length - 1 - i];
- nums[nums.length - 1 - i] = reverse;
- }
- return nums;
- }
- /** Searches the nums array for the maximum value
- * @param nums the array to be searched
- * @return the maximum value in the array */
- public static int findMax(int[] nums)
- {
- int max = nums[0];
- for (int i = 0; i < nums.length; i++)
- {
- if (nums[i] > max)
- {
- max = nums[i];
- }
- }
- return max;
- }
- /** Searches the nums array for the minimum value
- * @param nums the array to be searched
- * @return the minimum value in the array */
- public static int findMin(int[] nums)
- {
- int min = nums[0];
- for (int i = 0; i < nums.length; i++)
- {
- if (nums[i] < min)
- {
- min = nums[i];
- }
- }
- return min;
- }
- /** Searches the String word for an occurence of the target char
- * @param word the String to be searched
- * @param target the char to search for
- * @return true if the target char is found, false otherwise */
- public static boolean contains(String word, char target)
- {
- boolean found = false;
- int count = 0;
- for(int i =0; i < word.length(); i++)
- {
- if(target == word.charAt(i))
- {
- found = true;
- }
- }
- return found;
- }
- /** Searches the nums array for an occurence of the int target
- * @param nums the array to be searched
- * @param target the int to search for
- * @return true if the target int is found, false otherwise */
- public static boolean contains(int[] nums, int target)
- {
- boolean found = false;
- for (int i: nums)
- {
- if (i == target)
- {
- found = true;
- }
- }
- return found;
- }
- /** "poor-mans Encryption"... add 44 to the ascii value of each char in a String
- * @param word the String to be encrypted
- * @return an 'encrypted' version of the original String */
- public static String pmEncrypt(String word)
- {
- String result = "";
- char[] arr = new char[word.length()];
- for (int i = 0; i < word.length(); i++)
- {
- arr[i] = (char)((int)word.charAt(i) + 44);
- }
- for (int i = 0; i < arr.length; i++)
- {
- result += arr[i];
- }
- return result;
- }
- /** Reverses the pmEncrypt process... builds a new String by subtacting 44 from each char value in an encrypted String
- * @param word a pmEncrypted String
- * @return a 'decrypted' version of the pmEncrypted String */
- public static String pmDecrypt(String word)
- {
- String result = "";
- char[] arr = new char[word.length()];
- for (int i = 0; i < word.length(); i++)
- {
- arr[i] = (char)((int)word.charAt(i) - 44);
- }
- for (int i = 0; i < arr.length; i++)
- {
- result += arr[i];
- }
- return result;
- }
- /** Formats an array of Strings for console output with tabs (/t) in-between each value*/
- // Note: Arrays have a built-in 'toString() method that does a decent job of this already
- public static String arrayToString(int[] nums)
- {
- String result = "";
- for (int i: nums)
- {
- result += (i + "\t");
- }
- System.out.println(result);
- return result;
- }
- /** Formats an array of Strings for console output with tabs (/t) in-between each value*/
- // Note: Arrays have a built-in 'toString() method that does a decent job of this already
- public static String arrayToString(double[] nums)
- {
- String result = "";
- for (double i: nums)
- {
- result += (i + "\t");
- }
- System.out.println(result);
- return result;
- }
- }
- ------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // ArrayUtility
- // Spec: In this project you will be given the template for an ArrayUtility file. You will also be given a simple client to
- // use for testing purposes. Take a look at the methods and the comments for each method in ArrayUtility. Your task in this
- // project is to implement each of the methods shown.
- public class ArrayClient
- {
- public static void main(String[] args)
- {
- int[] intArr = {5,8,3,22,64,81};
- double[] dblArr = {3.4, 6.2, 9.8};
- int intResult;
- double dblResult;
- long lngResult;
- int[] intArrBackwards;
- String word = "APCS";
- intResult = ArrayUtility.getSum(intArr);
- System.out.println("\nSum of integers: " + intResult);
- dblResult = ArrayUtility.getSum(dblArr);
- System.out.println("\nSum of doubles: " + dblResult);
- lngResult = ArrayUtility.getProduct(intArr);
- System.out.println("\nProduct of integers: " + lngResult);
- System.out.println("\nInteger array forwards: ");
- ArrayUtility.arrayToString(intArr);
- intArrBackwards = ArrayUtility.reverseArray(intArr);
- System.out.println("\nInteger array backwards: ");
- ArrayUtility.arrayToString(intArrBackwards);
- intResult = ArrayUtility.findMax(intArr);
- System.out.println("\nMax integer: " + intResult);
- intResult = ArrayUtility.findMin(intArr);
- System.out.println("\nMin integer: " + intResult);
- System.out.println("\nfoo contains \'e\': " + ArrayUtility.contains("foo",'e'));
- System.out.println("\nInteger array contains 6: " + ArrayUtility.contains(intArr, 6));
- System.out.print("\n" + word);
- word = ArrayUtility.pmEncrypt(word);
- System.out.println(" encrypted is: " + word);
- word = ArrayUtility.pmDecrypt(word);
- System.out.println("\ndecrypted it's: " + word + "\n");
- }
- }
- /*
- Sum of integers: 183
- Sum of doubles: 19.4
- Product of integers: 13685760
- Integer array forwards:
- 5 8 3 22 64 81
- Integer array backwards:
- 81 64 22 3 8 5
- Max integer: 81
- Min integer: 3
- foo contains 'e': false
- Integer array contains 6: false
- APCS encrypted is: m|o¦
- decrypted it's: APCS
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement