From edcb1a2ce05aef1d0f2002ab9bf111a9fcf6d43f Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 8 Jul 2024 12:59:05 +0000 Subject: [PATCH 01/16] Setting up GitHub Classroom Feedback From 2d878819d251ba00f36ddc78a245f91ee91df88f Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 09:19:13 -0400 Subject: [PATCH 02/16] Test - first commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4f1803a..8940042 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # ZipCodeWilmington Assessment 1 +//Jon testing first commit ## **Getting Started** * Complete each of the asks in each of the `README.md` files found in the [instructions directory](./instructions) From 778f9f02b189299cc178861657f3d1dcb97dac43 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 09:36:15 -0400 Subject: [PATCH 03/16] Finished BasicArrayUtils, working on BasicStringUtils --- .../assessment1/part1/BasicArrayUtils.java | 12 ++++++++---- .../assessment1/part1/BasicStringUtils.java | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicArrayUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicArrayUtils.java index ef714b5..4b5d2c6 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicArrayUtils.java @@ -9,7 +9,8 @@ public class BasicArrayUtils { * @return the first element in the array */ public static String getFirstElement(String[] stringArray) { - return null; + + return stringArray[0]; } /** @@ -17,7 +18,8 @@ public static String getFirstElement(String[] stringArray) { * @return the second element in the array */ public static String getSecondElement(String[] stringArray) { - return null; + + return stringArray[1]; } /** @@ -25,7 +27,8 @@ public static String getSecondElement(String[] stringArray) { * @return the last element in the array */ public static String getLastElement(String[] stringArray) { - return null; + + return stringArray[stringArray.length -1]; } /** @@ -33,6 +36,7 @@ public static String getLastElement(String[] stringArray) { * @return the second to last element in the array */ public static String getSecondToLastElement(String[] stringArray) { - return null; + + return stringArray[stringArray.length - 2]; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java index ca13f2d..fca2355 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java @@ -9,7 +9,8 @@ public class BasicStringUtils { * @return string with identical content, and the first character capitalized */ public static String camelCase(String str) { - return null; + + return Character.toUpperCase(str.charAt(0)) + str.substring(1); } /** @@ -17,7 +18,8 @@ public static String camelCase(String str) { * @return string with identical contents, in the reverse order */ public static String reverse(String str) { - return null; + StringBuilder reverseSb = new StringBuilder(str); + return reverseSb.reverse().toString(); } /** @@ -25,7 +27,8 @@ public static String reverse(String str) { * @return string with identical contents, in reverse order, with first character capitalized */ public static String reverseThenCamelCase(String str) { - return null; + String reversedString = reverse(str); + return camelCase(reversedString); } @@ -34,7 +37,8 @@ public static String reverseThenCamelCase(String str) { * @return string with identical contents excluding first and last character */ public static String removeFirstAndLastCharacter(String str) { - return null; + + return str.substring(1, str.length() - 1); } /** @@ -42,6 +46,10 @@ public static String removeFirstAndLastCharacter(String str) { * @return string with identical characters, each with opposite casing */ public static String invertCasing(String str) { + + //StringBuilder to start + //For loop + return null; } } From a30c65f5b3947e003f4b01fd7403002a12eb6799 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 09:48:20 -0400 Subject: [PATCH 04/16] Completed IntegerArrayUtils, on to the next --- .../assessment1/part1/BasicStringUtils.java | 15 ++++++++-- .../assessment1/part1/IntegerArrayUtils.java | 28 +++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java index fca2355..770264b 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/BasicStringUtils.java @@ -48,8 +48,19 @@ public static String removeFirstAndLastCharacter(String str) { public static String invertCasing(String str) { //StringBuilder to start - //For loop + StringBuilder result = new StringBuilder(str.length()); + //For loop - with if statement to see if character is upper or lower case + for (char c : str.toCharArray()) { + if (Character.isUpperCase(c)) { + result.append(Character.toLowerCase(c)); + } else if (Character.isLowerCase(c)) { + result.append(Character.toUpperCase(c)); + } else { + result.append(c); + } + } + return result.toString(); - return null; + //return null; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerArrayUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerArrayUtils.java index 68d82ec..0ba3384 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerArrayUtils.java @@ -9,7 +9,15 @@ public class IntegerArrayUtils { * @return the sum of `intArray` */ public static Integer getSum(Integer[] intArray) { - return null; + //Need to write a method to getSum(Integer[] intArray) + + //Initialize first + int sumOfIntegers = 0; + for (Integer num : intArray) { + sumOfIntegers += num; + } + + return sumOfIntegers; } /** @@ -17,7 +25,15 @@ public static Integer getSum(Integer[] intArray) { * @return the product of `intArray` */ public static Integer getProduct(Integer[] intArray) { - return null; + //Need to write a method to getProduct(Integer[] intArray) + + //Initialize + int productOfIntegers = 1; + for (Integer num : intArray) { + productOfIntegers *= num; + } + + return productOfIntegers; } /** @@ -25,6 +41,12 @@ public static Integer getProduct(Integer[] intArray) { * @return the sum of `intArray` divided by number of elements in `intArray` */ public static Double getAverage(Integer[] intArray) { - return null; + //Need to write a method to getAverage(Integer[] intArray) + + //Initialize to get sum + int sumOfNum = getSum(intArray); + + //Average = sum / count + return (double) sumOfNum / intArray.length; } } From b8016e02df88f4e64346e5b8ab4f61f315034256 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 10:04:17 -0400 Subject: [PATCH 05/16] Completed IntegerUtils --- .../assessment1/part1/IntegerUtils.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerUtils.java index eccbb6c..1cfdcb6 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/IntegerUtils.java @@ -11,7 +11,17 @@ public class IntegerUtils { * @return the sum of all integers between 0 and not including `n` */ public static Integer getSumOfN(Integer n) { - return null; + //Write method to getSumOfN(int n) + + //Initialize a sum var + int sumOfN = 0; + + //for loop to iterate through n + for (int i = 0; i <= n; i++) { + sumOfN += i; + } + + return sumOfN; } /** @@ -19,7 +29,21 @@ public static Integer getSumOfN(Integer n) { * @return the product of all integers between 0 and not including `n` */ public static Integer getProductOfN(Integer n) { - return null; + // Write method to getProductOfN(int n) + + if (n <= 0) { + return 0; + } + + //initialize variable for product of n + int productThroughN = 1; + + //iterate through + for (int i = 1; i <= n; i++) { + productThroughN *= i; + } + + return productThroughN; } /** @@ -27,6 +51,22 @@ public static Integer getProductOfN(Integer n) { * @return integer with identical digits in the reverse order */ public static Integer reverseDigits(Integer val) { - return null; + //Write method to reverseDigits(int val) + + //Initialize a reverse var + int reversed = 0; + + //Loop + while (val !=0) { + //get last digit of val + int digit = val % 10; + //Add new value to reversed number + reversed = reversed * 10 + digit; + + //remove last dig (value / 10) + val /= 10; + } + + return reversed; } } From 2d38661825dcf27211cf30e036f42b3891a3eb46 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 10:26:04 -0400 Subject: [PATCH 06/16] Almost finished with part 1, need to figure out getWinner code in Rock Paper Siccosrs --- .../part1/RockPaperSissorsEvaluator.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java index 9495445..1742e85 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java @@ -13,6 +13,17 @@ public class RockPaperSissorsEvaluator { * @return the respective winning move */ public String getWinningMove(String handSign) { + //Need to write a method to get the winning move against a given hand sign + + //Utilizing a switch + switch (handSign.toLowerCase()) { + case "rock": + return "paper"; + case "paper": + return "scissor"; + case "scissor": + return "rock"; + } return null; } @@ -21,6 +32,17 @@ public String getWinningMove(String handSign) { * @return the respective losing move */ public String getLosingMove(String handSign) { + //Need to write a method to get the losing move against a given hand sign + + //Utilizing a switch for scenario building + switch (handSign.toLowerCase()) { + case "rock": + return "scissor"; + case "paper": + return "rock"; + case "scissor": + return "paper"; + } return null; } @@ -30,6 +52,17 @@ public String getLosingMove(String handSign) { * @return a string representative of the winning hand sign between the two players */ public String getWinner(String handSignOfPlayer1, String handSignOfPlayer2) { - return null; + //Need to write a method to figure out the winner between two players based on their respective hand signs (rock, paper, scissor) + + String winningMove = getWinningMove(handSignOfPlayer1); + + //If statement to check if player 1's move equals player 2's move + if (winningMove.equals(handSignOfPlayer2)) { + return handSignOfPlayer1; //player1 wins! + } else if (handSignOfPlayer1.equals(handSignOfPlayer2)) { + return "Tie"; // Tie - the same hand-sign for both players 1 and 2 + } else { + return handSignOfPlayer2; //player2 wins! + } } } From 3663651dd707c97931c6122b94a288d98bc469ac Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 11:22:40 -0400 Subject: [PATCH 07/16] Moving on to Multiples Deleter --- .../part1/RockPaperSissorsEvaluator.java | 24 ++++- .../assessment1/part2/ArrayUtils.java | 96 ++++++++++++++++++- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java index 1742e85..fe5a95d 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java @@ -52,17 +52,33 @@ public String getLosingMove(String handSign) { * @return a string representative of the winning hand sign between the two players */ public String getWinner(String handSignOfPlayer1, String handSignOfPlayer2) { + + //Given two Strings, named handSignOfPlayer1, and handSignOfPlayer2, representative of the hand signs of two Rock Paper Scissor players, + // return the String representation of the hand sign which would be the victor. + //Need to write a method to figure out the winner between two players based on their respective hand signs (rock, paper, scissor) String winningMove = getWinningMove(handSignOfPlayer1); - //If statement to check if player 1's move equals player 2's move if (winningMove.equals(handSignOfPlayer2)) { - return handSignOfPlayer1; //player1 wins! + return handSignOfPlayer1; } else if (handSignOfPlayer1.equals(handSignOfPlayer2)) { - return "Tie"; // Tie - the same hand-sign for both players 1 and 2 + return "Tie!"; } else { - return handSignOfPlayer2; //player2 wins! + return handSignOfPlayer2; } + + + +// String winningMove = getWinningMove(handSignOfPlayer1); +// +// //If statement to check if player 1's move equals player 2's move +// if (winningMove.equals(handSignOfPlayer2)) { +// return handSignOfPlayer1; //player1 wins! +// } else if (handSignOfPlayer1.equals(handSignOfPlayer2)) { +// return "Tie"; // Tie - the same hand-sign for both players 1 and 2 +// } else { +// return handSignOfPlayer2; //player2 wins! +// } } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java index bb9995a..fe62b16 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java @@ -1,5 +1,11 @@ package com.zipcodewilmington.assessment1.part2; +import java.sql.Array; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * Created by leon on 2/16/18. */ @@ -11,7 +17,16 @@ public class ArrayUtils { * Given an array of objects, named `objectArray`, and an object `objectToCount`, return the number of times the `objectToCount` appears in the `objectArray` */ public static Integer getNumberOfOccurrences(Object[] objectArray, Object objectToCount) { - return null; + //Need to write method to count occurrences of an object in an array + + //initialize count + int count = 0; + for (int i = 0; i < objectArray.length; i++) { + if (objectArray[i].equals(objectToCount)) { + count++; + } + } + return count; } /** @@ -21,7 +36,19 @@ public static Integer getNumberOfOccurrences(Object[] objectArray, Object object * Given an array of objects, name `objectArray`, and an object `objectToRemove`, return an array of objects with identical contents excluding `objectToRemove` */ public static Object[] removeValue(Object[] objectArray, Object objectToRemove) { - return null; + //Need to write method to remove all occurrences of an object from an array + + //Array list + List resultList = new ArrayList<>(); + + //Iterate through the array + for (int i = 0; i < objectArray.length; i++) { + if (!objectArray[i].equals(objectToRemove)) { + resultList.add(objectArray[i]); + } + } + + return resultList.toArray(new Object[resultList.size()]); } /** @@ -30,7 +57,29 @@ public static Object[] removeValue(Object[] objectArray, Object objectToRemove) * given an array of objects, named `objectArray` return the most frequently occuring object in the array */ public static Object getMostCommon(Object[] objectArray) { - return null; + + Map frequencyMap = new HashMap<>(); + + for (int i = 0; i < objectArray.length; i++) { + Object current = objectArray[i]; + if (frequencyMap.containsKey(current)) { + frequencyMap.put(current, frequencyMap.get(current) + 1); + } else { + frequencyMap.put(current,1); + } + } + + Object mostCommon = null; + int maxFrequency = 0; + + for (Map.Entry entry : frequencyMap.entrySet()) { + if (entry.getValue() > maxFrequency) { + maxFrequency = entry.getValue(); + mostCommon = entry.getKey(); + } + } + + return mostCommon; } @@ -40,7 +89,28 @@ public static Object getMostCommon(Object[] objectArray) { * given an array of objects, named `objectArray` return the least frequently occuring object in the array */ public static Object getLeastCommon(Object[] objectArray) { - return null; + Map frequencyMap = new HashMap<>(); + + for (int i = 0; i < objectArray.length; i++) { + Object current = objectArray[i]; + if (frequencyMap.containsKey(current)) { + frequencyMap.put(current, frequencyMap.get(current) + 1); + } else { + frequencyMap.put(current, 1); + } + } + + Object leastCommon = null; + int minFrequency = Integer.MAX_VALUE; + + for (Map.Entry entry : frequencyMap.entrySet()) { + if (entry.getValue() < minFrequency) { + minFrequency = entry.getValue(); + leastCommon = entry.getKey(); + } + } + + return leastCommon; } /** @@ -50,6 +120,22 @@ public static Object getLeastCommon(Object[] objectArray) { * given two arrays `objectArray` and `objectArrayToAdd`, return an array containing all elements in `objectArray` and `objectArrayToAdd` */ public static Object[] mergeArrays(Object[] objectArray, Object[] objectArrayToAdd) { - return null; + //Merge two arrays into one + + Object[] mergeArrays = new Object[objectArray.length + objectArrayToAdd.length]; + + //Copying elements from objectArray to mergedArray + //Iterate through the array + for (int i = 0; i < objectArray.length; i++) { + mergeArrays[i] = objectArray[i]; + } + + //Need to copy elements from objectArrayToAdd to mergedArray + for (int i = 0; i < objectArray.length; i++) { + mergeArrays[objectArray.length + i] = objectArrayToAdd[i]; + } + + + return mergeArrays; } } From b50bb539c376694dbb383f694ff01d9675118f64 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 11:33:36 -0400 Subject: [PATCH 08/16] finished multiples deleter --- .../assessment1/part2/MultiplesDeleter.java | 58 +++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part2/MultiplesDeleter.java b/src/main/java/com/zipcodewilmington/assessment1/part2/MultiplesDeleter.java index a348b0d..d3b5d43 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part2/MultiplesDeleter.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part2/MultiplesDeleter.java @@ -1,5 +1,8 @@ package com.zipcodewilmington.assessment1.part2; +import java.util.ArrayList; +import java.util.List; + /** * Created by leon on 2/16/18. */ @@ -10,7 +13,20 @@ public class MultiplesDeleter { * given an array of integers, named `ints` return an identical array with evens removed */ public Integer[] deleteEvens(Integer[] ints) { - return null; + + List result = new ArrayList<>(); + + //Iterate through each number in the array + for (Integer num : ints) { + //Check if the number is odd (num % 2 !=0) + if (num % 2 != 0) { + //If its odd, add it to the result list + result.add(num); + } + } + + //Convert the list back to an array and return + return result.toArray(new Integer[0]); } /** @@ -19,7 +35,19 @@ public Integer[] deleteEvens(Integer[] ints) { * given an array of integers, named `ints` return an identical array with odds removed */ public Integer[] deleteOdds(Integer[] ints) { - return null; + List result = new ArrayList<>(); + + //Iterate through each number in the array + for (Integer num : ints) { + //Check if the number is even (num % 2 ==0) *Opposite of above* + if (num % 2 == 0) { + //If its even, add it to the result list + result.add(num); + } + } + + //Convert the list back to an array and return + return result.toArray(new Integer[0]); } /** @@ -28,7 +56,18 @@ public Integer[] deleteOdds(Integer[] ints) { * given an array of integers, named `ints` return an identical array with numbers indivisible by 3 removed */ public Integer[] deleteMultiplesOf3(Integer[] ints) { - return null; + List result = new ArrayList<>(); + + //Need to iterate through each number in array + for (Integer num : ints) { + //check if number is not divisible by 3 + if (num % 3 != 0) { + //if its not divisible by 3, add it to the result list + result.add(num); + } + } + + return result.toArray(new Integer[0]); } /** @@ -38,6 +77,17 @@ public Integer[] deleteMultiplesOf3(Integer[] ints) { * given an array of integers, named `ints` return an identical array with numbers indivisible by `multiple` removed */ public Integer[] deleteMultiplesOfN(Integer[] ints, int multiple) { - return null; + List result = new ArrayList<>(); + + //Iterate through each number in the array + for (Integer num : ints) { + //Check if number is not divisible by multiple (per parameters above) + if (num % multiple != 0) { + // if it is not divisible, add it to the result list + result.add(num); + } + } + + return result.toArray(new Integer[0]); } } From 05648cd51f215a496e168e193de2332dd3998a97 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 11:56:25 -0400 Subject: [PATCH 09/16] Finished part 2, on to part 3 --- .../assessment1/part2/StringUtils.java | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part2/StringUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part2/StringUtils.java index fc403e5..3d83d36 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part2/StringUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part2/StringUtils.java @@ -1,5 +1,7 @@ package com.zipcodewilmington.assessment1.part2; +import static com.zipcodewilmington.assessment1.part1.BasicStringUtils.reverse; + /** * Created by leon on 2/16/18. */ @@ -11,7 +13,8 @@ public class StringUtils { * given a string containing words delimited by spaces, representative of a sentence, return an array of strings, each element representative of a respective word in the sentence */ public static String[] getWords(String sentence) { - return null; + + return sentence.split("\\s+"); } @@ -21,7 +24,12 @@ public static String[] getWords(String sentence) { * given a string containing words delimited by spaces, representative of a sentence, return the first word of the sentence */ public static String getFirstWord(String sentence) { - return null; + String[] wordsIn = sentence.split("\\s+"); + if (wordsIn.length > 0) { + return wordsIn[0]; + } else { + return null; + } } /** @@ -30,27 +38,48 @@ public static String getFirstWord(String sentence) { * given a string containing words delimited by spaces, representative of a sentence, return the first word with identical contents in reverse order */ public static String reverseFirstWord(String sentence) { - return null; + String[] wordsIn = sentence.split("\\s+"); + if (wordsIn.length > 0) { + //Call to the first word with index 0 + String firstWord = wordsIn[0]; + } else { + return null; + } + + //Create new string with string builder -- use index to return first word + StringBuilder reversedString = new StringBuilder(wordsIn[0]); + return reversedString.reverse().toString(); } + /** * @param sentence a string containing words delimited by spaces, representative of a sentence * @return the first word in the specified sentence, with identical contents in reverse order and the first character capitalized * given a string containing words delimited by spaces, representative of a sentence, return the first word with identical contents in reverse order with the first character capitalized */ public static String reverseFirstWordThenCamelCase(String sentence) { - return null; - } + String[] wordsIn = sentence.split("\\s+"); + //Isolate first word and then reverse + if (wordsIn.length > 0) { + String firstWord = wordsIn[0]; + String reversed = reverse(firstWord); + //Capitalize first word + return reversed.substring(0, 1).toUpperCase() + reversed.substring(1); + } else { + return null; + } + } /** - * @param str string input from client + * @param str string input from client * @param index the index of the character to be removed from `str` * @return string with identical contents, excluding the character at the specified index * given a string and index, return an identical string excluding the character at the specified index */ public static String removeCharacterAtIndex(String str, int index) { - return null; + + return str.substring(0, index) + str.substring(index + 1); } } From 24e073eb44423d7207ed4bb540d771601541a20b Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 13:01:00 -0400 Subject: [PATCH 10/16] Animals is a work in progress --- .../assessment1/part3/Animal.java | 3 ++ .../assessment1/part3/Cat.java | 10 ++-- .../assessment1/part3/Dog.java | 8 ++- .../assessment1/part3/Pet.java | 37 ++++++++++++-- .../assessment1/part3/PetOwner.java | 50 ++++++++++++++++--- 5 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java index 658bb25..e1cbb9f 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java @@ -4,5 +4,8 @@ * Created by leon on 2/16/18. */ public interface Animal { + String name = ""; + Integer age = 0; + String petOwner = ""; String speak(); } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java index e731b77..0898eda 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java @@ -8,21 +8,23 @@ public class Cat extends Pet { * @param name name of this Cat * @param age age of this Cat */ - public Cat(String name, Integer age) { + public Cat(String name, Integer age) { + super(name, age); } /** * @param age age of this Cat */ public Cat(Integer age) { + super(age); } /** * @param name name of this Cat */ public Cat(String name) { - + super(name); } /** @@ -32,12 +34,14 @@ public Cat(String name) { * age is 0 */ public Cat() { + super(); } /** * @return meow as a string */ public String speak() { - return null; + + return "Meow"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java index 0c775fd..7fcbe0c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java @@ -9,19 +9,21 @@ public class Dog extends Pet { * @param age age of this dog */ public Dog(String name, Integer age) { - + super(name, age); } /** * @param age age of this dog */ public Dog(Integer age) { + super(age); } /** * @param name name of this dog */ public Dog(String name) { + super(name); } @@ -32,12 +34,14 @@ public Dog(String name) { * age is 0 */ public Dog() { + super(); } /** * @return bark as a string */ public String speak() { - return null; + + return "Bark"; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java index 3c925da..f370e3c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java @@ -4,45 +4,62 @@ * Created by leon on 2/16/18. */ public abstract class Pet implements Animal { + /** * nullary constructor * by default, pet has age of 0; name of ""; */ + + //Define instance variables ---> access modifier: + private String name; + private int age; + private PetOwner owner; + public Pet() { + this.name = ""; + this.age = 0; } /** * @param name name of this pet */ public Pet(String name) { + this.name = name; + this.age = 0; } /** * @param age age of this pet */ - public Pet(int age) { + public Pet(Integer age) { + this.name = ""; + this.age = age; } /** * @param name name of this pet * @param age age of this pet */ - public Pet(String name, int age) { + public Pet(String name, Integer age) { + this.name = name; + this.age = age; } /** * @return name of this pet */ public String getName() { - return null; + + return name; } /** * @return age of this pet */ public Integer getAge() { - return null; + + return age; } /** @@ -50,12 +67,22 @@ public Integer getAge() { * ensure this instance of `Pet` is added to the owner's composite `pets` list */ public void setOwner(PetOwner newPetOwner) { + this.owner = owner; } /** * @return PetOwner object whose composite `pets` collection contains this Pet instance */ public PetOwner getOwner() { - return null; + + return owner; + } + + public void setAge(Integer age) { + this.age = age; + } + + public void setName(String name) { + this.name = name; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java index 7bbf2ab..e28382f 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java @@ -1,5 +1,8 @@ package com.zipcodewilmington.assessment1.part3; +import java.util.ArrayList; +import java.util.List; + /** * Created by leon on 2/16/18. */ @@ -8,20 +11,33 @@ public class PetOwner { * @param name name of the owner of the Pet * @param pets array of Pet object */ + + public String name; + public List pets; + public PetOwner(String name, Pet... pets) { + this.name = name; + this.pets = new ArrayList<>(); + for (Pet pet : pets) { + this.pets.add(pet); + pet.setOwner(this); + } } /** * @param pet pet to be added to the composite collection of Pets */ public void addPet(Pet pet) { + pets.add(pet); + pet.setOwner(this); } /** * @param pet pet to be removed from the composite collection Pets */ public void removePet(Pet pet) { - + pets.remove(pet); + pet.setOwner(null); } /** @@ -29,14 +45,21 @@ public void removePet(Pet pet) { * @return true if I own this pet */ public Boolean isOwnerOf(Pet pet) { - return null; + + return pets.contains(pet); } /** * @return the age of the Pet object whose age field is the lowest amongst all Pets in this class */ public Integer getYoungetPetAge() { - return null; + int minimumAge = Integer.MAX_VALUE; + for (Pet pet : pets) { + if (pet.getAge() < minimumAge) { + minimumAge = pet.getAge(); + } + } + return minimumAge; } @@ -46,7 +69,13 @@ public Integer getYoungetPetAge() { * @return the age of the Pet object whose age field is the highest amongst all Pets in this class */ public Integer getOldestPetAge() { - return null; + int maxAge = Integer.MIN_VALUE; + for (Pet pet : pets) { + if (pet.getAge() > maxAge) { + maxAge = pet.getAge(); + } + } + return maxAge; } @@ -54,27 +83,32 @@ public Integer getOldestPetAge() { * @return the sum of ages of Pet objects stored in this class divided by the number of Pet object */ public Float getAveragePetAge() { - return null; + int sum = 0; + for (Pet pet : pets) { + sum += pet.getAge(); + } + return (float) sum / pets.size(); } /** * @return the number of Pet objects stored in this class */ public Integer getNumberOfPets() { - return null; + + return pets.size(); } /** * @return the name property of the Pet */ public String getName() { - return null; + return name; } /** * @return array representation of animals owned by this PetOwner */ public Pet[] getPets() { - return null; + return pets.toArray(new Pet[0]); } } From 809b32df7a12d491c801c6ff1db1965571e30745 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 13:43:13 -0400 Subject: [PATCH 11/16] Finished Palindrome, went through all parts - need to go back and fix other sections --- .../assessment1/part3/Animal.java | 6 ++--- .../assessment1/part3/Cat.java | 1 - .../assessment1/part3/Dog.java | 1 - .../assessment1/part3/PetOwner.java | 7 ++++-- .../assessment1/part4/Jumper.java | 20 +++++++++++++++- .../assessment1/part5/Palindrome.java | 23 +++++++++++++++++-- 6 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java index e1cbb9f..5029f06 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java @@ -4,8 +4,8 @@ * Created by leon on 2/16/18. */ public interface Animal { - String name = ""; - Integer age = 0; - String petOwner = ""; +// String name = ""; +// Integer age = 0; +// String petOwner = ""; String speak(); } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java index 0898eda..a64c4f3 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java @@ -34,7 +34,6 @@ public Cat(String name) { * age is 0 */ public Cat() { - super(); } /** diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java index 7fcbe0c..0db7f6c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java @@ -34,7 +34,6 @@ public Dog(String name) { * age is 0 */ public Dog() { - super(); } /** diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java index e28382f..e397a61 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java @@ -14,6 +14,8 @@ public class PetOwner { public String name; public List pets; + public int age; + public PetOwner owner; public PetOwner(String name, Pet... pets) { this.name = name; @@ -40,6 +42,7 @@ public void removePet(Pet pet) { pet.setOwner(null); } + /** * @param pet pet to evaluate ownership of * @return true if I own this pet @@ -53,13 +56,13 @@ public Boolean isOwnerOf(Pet pet) { * @return the age of the Pet object whose age field is the lowest amongst all Pets in this class */ public Integer getYoungetPetAge() { - int minimumAge = Integer.MAX_VALUE; + float minimumAge = Integer.MAX_VALUE; for (Pet pet : pets) { if (pet.getAge() < minimumAge) { minimumAge = pet.getAge(); } } - return minimumAge; + return (int) minimumAge; } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java index f881e9c..988221a 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java @@ -6,6 +6,24 @@ public class Jumper { * Complete the function below. */ public int jumps(int k, int j) { - return -1; + // If j is greater than or equal to k, Bob can reach the flag in one jump + if (j >= k) { + return 1; + } + + // Calculate full jumps of j units Bob can make + int fullJumps = k / j; + + //Calculate remaining height after full jumps of j units + int remainingHeight = k % j; + + //Calculate min jumps required + if (remainingHeight == 0) { + return fullJumps; + } else { + return fullJumps + 1; + } + +// return -1; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part5/Palindrome.java b/src/main/java/com/zipcodewilmington/assessment1/part5/Palindrome.java index 89e2016..100478d 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part5/Palindrome.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part5/Palindrome.java @@ -2,7 +2,26 @@ public class Palindrome { - public Integer countPalindromes(String input){ - return null; + public Integer countPalindromes(String input) { + //Function to count palindromic substrings in a given string + //Palindromic = the same if we start reading it from left to right or right to left. + + int count = 0; + int n = input.length(); + + //Check all possible centers of palindromes + for (int center = 0; center < 2 * n - 1; center++) { + int left = center / 2; + int right = left + center % 2; + + //Check for palindromes + while (left >= 0 && right < n && input.charAt(left) == input.charAt(right)) { + count++; + left--; + right++; + } + } + + return count; } } From bc4fa60bb823c233cdcaa10751160ab095a74771 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 14:32:00 -0400 Subject: [PATCH 12/16] Fixed Rock Paper Scissor --- .../assessment1/part1/RockPaperSissorsEvaluator.java | 4 ++-- .../java/com/zipcodewilmington/assessment1/part3/Pet.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java index fe5a95d..a21703b 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java @@ -61,11 +61,11 @@ public String getWinner(String handSignOfPlayer1, String handSignOfPlayer2) { String winningMove = getWinningMove(handSignOfPlayer1); if (winningMove.equals(handSignOfPlayer2)) { - return handSignOfPlayer1; + return handSignOfPlayer2; } else if (handSignOfPlayer1.equals(handSignOfPlayer2)) { return "Tie!"; } else { - return handSignOfPlayer2; + return handSignOfPlayer1; } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java index f370e3c..1ae403c 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java @@ -16,7 +16,7 @@ public abstract class Pet implements Animal { private PetOwner owner; public Pet() { - this.name = ""; + this.name = "Cat name"; this.age = 0; } From 4430dcbcfb074efb0e7bece220b94f43f0f57df7 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 15:28:07 -0400 Subject: [PATCH 13/16] update as of 3:28 --- .../java/com/zipcodewilmington/assessment1/part3/Cat.java | 1 + .../java/com/zipcodewilmington/assessment1/part3/Dog.java | 5 +++++ .../java/com/zipcodewilmington/assessment1/part3/Pet.java | 8 ++++---- .../com/zipcodewilmington/assessment1/part3/PetOwner.java | 5 +++-- .../com/zipcodewilmington/assessment1/part4/Jumper.java | 4 ++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java index a64c4f3..0898eda 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Cat.java @@ -34,6 +34,7 @@ public Cat(String name) { * age is 0 */ public Cat() { + super(); } /** diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java index 0db7f6c..dd2e65a 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Dog.java @@ -34,6 +34,11 @@ public Dog(String name) { * age is 0 */ public Dog() { + super(); + } + + public static void add(Pet pet) { + } /** diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java index 1ae403c..536ef25 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java @@ -51,7 +51,7 @@ public Pet(String name, Integer age) { */ public String getName() { - return name; + return this.name; } /** @@ -59,15 +59,15 @@ public String getName() { */ public Integer getAge() { - return age; + return this.age; } /** * @param newPetOwner the new owner of this pet - * ensure this instance of `Pet` is added to the owner's composite `pets` list + * ensure this instance of `Pet` is added to the owner's composite `pets` list */ public void setOwner(PetOwner newPetOwner) { - this.owner = owner; + } /** diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java index e397a61..1d15189 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/PetOwner.java @@ -31,7 +31,6 @@ public PetOwner(String name, Pet... pets) { */ public void addPet(Pet pet) { pets.add(pet); - pet.setOwner(this); } /** @@ -39,7 +38,6 @@ public void addPet(Pet pet) { */ public void removePet(Pet pet) { pets.remove(pet); - pet.setOwner(null); } @@ -86,6 +84,9 @@ public Integer getOldestPetAge() { * @return the sum of ages of Pet objects stored in this class divided by the number of Pet object */ public Float getAveragePetAge() { + if (pets.isEmpty()) { + return 0.0F; + } int sum = 0; for (Pet pet : pets) { sum += pet.getAge(); diff --git a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java index 988221a..6372e86 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java @@ -6,12 +6,12 @@ public class Jumper { * Complete the function below. */ public int jumps(int k, int j) { - // If j is greater than or equal to k, Bob can reach the flag in one jump + // If j is greater than or equal to k, Tariq can reach the flag in one jump if (j >= k) { return 1; } - // Calculate full jumps of j units Bob can make + // Calculate full jumps of j units Tariq can make int fullJumps = k / j; //Calculate remaining height after full jumps of j units From 41a863727b5efd42e0fbccb83186b92e92085a0d Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 15:54:12 -0400 Subject: [PATCH 14/16] update as of 3:54 --- .../zipcodewilmington/assessment1/part4/Jumper.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java index 6372e86..f6a5bb6 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java @@ -6,10 +6,6 @@ public class Jumper { * Complete the function below. */ public int jumps(int k, int j) { - // If j is greater than or equal to k, Tariq can reach the flag in one jump - if (j >= k) { - return 1; - } // Calculate full jumps of j units Tariq can make int fullJumps = k / j; @@ -17,6 +13,12 @@ public int jumps(int k, int j) { //Calculate remaining height after full jumps of j units int remainingHeight = k % j; + // If j is greater than or equal to k + // Tariq can reach the flag in one jump + if (j >= k) { + return 1; + } + //Calculate min jumps required if (remainingHeight == 0) { return fullJumps; From 1c249a80433ca322335c3be6f5476405da086861 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 16:25:02 -0400 Subject: [PATCH 15/16] Update as of 4:24 --- .../com/zipcodewilmington/assessment1/part2/ArrayUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java b/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java index fe62b16..a16eca9 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part2/ArrayUtils.java @@ -39,7 +39,7 @@ public static Object[] removeValue(Object[] objectArray, Object objectToRemove) //Need to write method to remove all occurrences of an object from an array //Array list - List resultList = new ArrayList<>(); + ArrayList resultList = new ArrayList<>(); //Iterate through the array for (int i = 0; i < objectArray.length; i++) { From af5f7797a51ee1c69643c97c15dcb56ffbebe2a7 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 8 Jul 2024 16:50:22 -0400 Subject: [PATCH 16/16] Final Commit --- src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java index 536ef25..f53e1ca 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java @@ -16,7 +16,7 @@ public abstract class Pet implements Animal { private PetOwner owner; public Pet() { - this.name = "Cat name"; + this.name = "Dog name"; this.age = 0; }