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) 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..770264b 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,21 @@ public static String removeFirstAndLastCharacter(String str) { * @return string with identical characters, each with opposite casing */ public static String invertCasing(String str) { - return null; + + //StringBuilder to start + 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; } } 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; } } 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; } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java b/src/main/java/com/zipcodewilmington/assessment1/part1/RockPaperSissorsEvaluator.java index 9495445..a21703b 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,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) { - return null; + + //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 (winningMove.equals(handSignOfPlayer2)) { + return handSignOfPlayer2; + } else if (handSignOfPlayer1.equals(handSignOfPlayer2)) { + return "Tie!"; + } else { + return handSignOfPlayer1; + } + + + +// 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..a16eca9 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 + ArrayList 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; } } 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]); } } 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); } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java b/src/main/java/com/zipcodewilmington/assessment1/part3/Animal.java index 658bb25..5029f06 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..dd2e65a 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,18 @@ public Dog(String name) { * age is 0 */ public Dog() { + super(); + } + + public static void add(Pet pet) { + } /** * @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..f53e1ca 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part3/Pet.java @@ -4,58 +4,85 @@ * 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 = "Dog 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 this.name; } /** * @return age of this pet */ public Integer getAge() { - return null; + + 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) { + } /** * @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..1d15189 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,35 +11,56 @@ 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 int age; + public PetOwner owner; + 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); } /** * @param pet pet to be removed from the composite collection Pets */ public void removePet(Pet pet) { - + pets.remove(pet); } + /** * @param pet pet to evaluate ownership of * @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; + float minimumAge = Integer.MAX_VALUE; + for (Pet pet : pets) { + if (pet.getAge() < minimumAge) { + minimumAge = pet.getAge(); + } + } + return (int) minimumAge; } @@ -46,7 +70,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 +84,35 @@ 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; + if (pets.isEmpty()) { + return 0.0F; + } + 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]); } } diff --git a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java index f881e9c..f6a5bb6 100644 --- a/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java +++ b/src/main/java/com/zipcodewilmington/assessment1/part4/Jumper.java @@ -6,6 +6,26 @@ public class Jumper { * Complete the function below. */ public int jumps(int k, int j) { - return -1; + + // Calculate full jumps of j units Tariq can make + int fullJumps = k / 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; + } 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; } }