From 7e0774009825aa1113a545637b8b85c8524b2e5a Mon Sep 17 00:00:00 2001 From: "T.Lai" Date: Fri, 30 Oct 2020 12:32:04 -0400 Subject: [PATCH] commit message --- README.TXT | 0 StringArrayUtils.java | 91 +++++++++++++++++++++++++++++++++++++------ package.bluej | 46 +++++++++++----------- 3 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 README.TXT diff --git a/README.TXT b/README.TXT new file mode 100644 index 0000000..e69de29 diff --git a/StringArrayUtils.java b/StringArrayUtils.java index e86810b..136e353 100644 --- a/StringArrayUtils.java +++ b/StringArrayUtils.java @@ -9,7 +9,14 @@ public class StringArrayUtils { * @return first element of specified array */ // TODO public static String getFirstElement(String[] array) { - return null; + if (array.length > 0){ + return array[0]; + } + else{ + return null; + } + + } /** @@ -17,7 +24,12 @@ public static String getFirstElement(String[] array) { * @return second element in specified array */ public static String getSecondElement(String[] array) { - return null; + if (array.length > 0){ + return array[1]; + } + else{ + return null; + } } /** @@ -25,7 +37,12 @@ public static String getSecondElement(String[] array) { * @return last element in specified array */ // TODO public static String getLastElement(String[] array) { - return null; + if (array.length > 0){ + return array[array.length-1]; + } + else{ + return null; + } } /** @@ -33,7 +50,12 @@ public static String getLastElement(String[] array) { * @return second to last element in specified array */ // TODO public static String getSecondToLastElement(String[] array) { - return null; + if (array.length > 0){ + return array[array.length-2]; + } + else{ + return null; + } } /** @@ -42,6 +64,9 @@ public static String getSecondToLastElement(String[] array) { * @return true if the array contains the specified `value` */ // TODO public static boolean contains(String[] array, String value) { + for(String item : array){ + if (value == item){return true;} + } return false; } @@ -50,7 +75,11 @@ public static boolean contains(String[] array, String value) { * @return an array with identical contents in reverse order */ // TODO public static String[] reverse(String[] array) { - return null; + String[] res = new String[array.length]; + for(int i = 0; i < array.length; i++){ + res[array.length-1-i] = array[i]; + } + return res; } /** @@ -58,7 +87,16 @@ public static String[] reverse(String[] array) { * @return true if the order of the array is the same backwards and forwards */ // TODO public static boolean isPalindromic(String[] array) { - return false; + int i = 0; + int j = array.length-1; + while( i < j){ + if (array[i] != array[j]){ + return false; + } + i++; + j--; + } + return true; } /** @@ -66,7 +104,26 @@ public static boolean isPalindromic(String[] array) { * @return true if each letter in the alphabet has been used in the array */ // TODO public static boolean isPangramic(String[] array) { - return false; + boolean[] checked = new boolean[26]; + for(String item : array){ + int strLen = item.length(); + for(int i = 0; i < strLen; i++){ + char a = item.charAt(i); + if(a >= 'A' && a <= 'Z'){ + checked[a-'A'] = true; + } + else if (a >= 'a' && a <= 'z'){ + checked[a-'a'] = true; + } + } + } + + for(int i = 0; i < 26; i++){ + if(checked[i] == false){ + return false; + } + } + return true; } /** @@ -75,7 +132,13 @@ public static boolean isPangramic(String[] array) { * @return number of occurrences the specified `value` has occurred */ // TODO public static int getNumberOfOccurrences(String[] array, String value) { - return 0; + int res = 0; + for(String str : array){ + if(str == value){ + res++; + } + } + return res; } /** @@ -83,8 +146,16 @@ public static int getNumberOfOccurrences(String[] array, String value) { * @param valueToRemove value to remove from array * @return array with identical contents excluding values of `value` */ // TODO + public static String[] removeValue(String[] array, String valueToRemove) { - return null; + String[] res = new String[array.length-1]; + int i = 0; + for(String str : array){ + if(str != valueToRemove){ + res[i++] = str; + } + } + return res; } /** @@ -102,6 +173,4 @@ public static String[] removeConsecutiveDuplicates(String[] array) { public static String[] packConsecutiveDuplicates(String[] array) { return null; } - - } diff --git a/package.bluej b/package.bluej index e451ca6..6f76b28 100644 --- a/package.bluej +++ b/package.bluej @@ -1,23 +1,23 @@ #BlueJ package file -dependency1.from=GetFirstElementTest +dependency1.from=ReverseTest dependency1.to=StringArrayUtils dependency1.type=UsesDependency -dependency10.from=RemoveConsecutiveDuplicatesTest +dependency10.from=IsPalindromicTest dependency10.to=StringArrayUtils dependency10.type=UsesDependency -dependency11.from=IsPalindromicTest +dependency11.from=GetSecondElementTest dependency11.to=StringArrayUtils dependency11.type=UsesDependency -dependency12.from=GetSecondElementTest +dependency12.from=ContainsTest dependency12.to=StringArrayUtils dependency12.type=UsesDependency -dependency13.from=ContainsTest +dependency13.from=RemoveValueTest dependency13.to=StringArrayUtils dependency13.type=UsesDependency -dependency2.from=ReverseTest +dependency2.from=StringArrayUtilsTest dependency2.to=StringArrayUtils dependency2.type=UsesDependency -dependency3.from=StringArrayUtilsTest +dependency3.from=GetFirstElementTest dependency3.to=StringArrayUtils dependency3.type=UsesDependency dependency4.from=GetNumberOfOccurrencesTest @@ -32,26 +32,26 @@ dependency6.type=UsesDependency dependency7.from=RemovePackDuplicatesTest dependency7.to=StringArrayUtils dependency7.type=UsesDependency -dependency8.from=RemoveValueTest +dependency8.from=IsPangramicTest dependency8.to=StringArrayUtils dependency8.type=UsesDependency -dependency9.from=IsPangramicTest +dependency9.from=RemoveConsecutiveDuplicatesTest dependency9.to=StringArrayUtils dependency9.type=UsesDependency editor.fx.0.height=704 -editor.fx.0.width=800 -editor.fx.0.x=224 -editor.fx.0.y=155 -objectbench.height=107 -objectbench.width=837 +editor.fx.0.width=771 +editor.fx.0.x=-1336 +editor.fx.0.y=24 +objectbench.height=221 +objectbench.width=976 package.divider.horizontal=0.6397146254458977 -package.divider.vertical=0.8235294117647058 -package.editor.height=509 -package.editor.width=727 -package.editor.x=77 -package.editor.y=158 +package.divider.vertical=0.6470588235294118 +package.editor.height=411 +package.editor.width=866 +package.editor.x=82 +package.editor.y=31 package.frame.height=704 -package.frame.width=861 +package.frame.width=1000 package.numDependencies=13 package.numTargets=14 package.showExtends=true @@ -117,7 +117,7 @@ target3.showInterface=false target3.type=UnitTestTargetJunit4 target3.width=140 target3.x=10 -target3.y=180 +target3.y=590 target4.height=50 target4.name=GetNumberOfOccurrencesTest target4.showInterface=false @@ -130,8 +130,8 @@ target5.name=GetSecondToLastElement target5.showInterface=false target5.type=UnitTestTargetJunit4 target5.width=170 -target5.x=20 -target5.y=450 +target5.x=10 +target5.y=170 target6.height=50 target6.name=GetLastElementTest target6.showInterface=false