/*********************** * * LabQuiz5Srv.java Write-a-method problems for lab quiz 5. * This is the service class. * Run LabQuiz5Test.java to test these methods. * * Stubs by: Shawn Brenneman * * Methods by: YOURNAME! * * Date: 2015-Nov-5 * ***********************/ public class LabQuiz5Srv { //---------------------------------------------------------------------------- // allCaps - answers if all the letters in the given char array are uppercase. // Non letters are not considered to be any case. If there are no // letters in the array, allCaps returns false. // // allCaps({'Y','A','H','O','O','!'}) --> true // allCaps({'I',' ','R','A','N','.'}) --> true // allCaps({'I',' ','r','a','n','.'}) --> false // allCaps({'*','$,','@'}) --> false // allCaps({}) --> false // public boolean allCaps(char[] phrase){ boolean containsLowerCase = false; boolean containsUpperCase = false; int ii = 0; while (!containsLowerCase && ii < phrase.length) { if (phrase[ii] >= 'a' && phrase[ii] <= 'z') { containsLowerCase = true; } else if (phrase[ii] >= 'A' && phrase[ii] <= 'Z') { containsUpperCase = true; } ii++; } return (containsUpperCase && !containsLowerCase); } //--------------------------------------------------------------------------- // multString - returns a String that is str concatenated num times // // multString(5,"*-") --> "*-*-*-*-*-" // multString(2,"ha.") --> "ha.ha." // multString(1,"Yay!") --> "Yay!" // multString(0,"Oink") --> "" // multString(-3,"Hey") --> "" // public String multString(int num, String str){ String returnString = ""; if (num > 0) { for (int ii = 0; ii < num; ii++) { returnString += str; } } return returnString; } //---------------------------------------------------------------------------- // odds - gets an array of ints as a parameter, and returns an array of // just the odd ints in the given array. // The returned array is exactly the size needed to hold the odd numbers. // // odds({7,3,2,9,6,15}) --> {7,3,5,9,15} // odds({3,4,5,6,6}) --> {3,5} // odds({8,4,8,16}) --> {} // odds({3}) --> {3} // odds({}) --> {} // public int[] odds(int[] nums){ int cntOdds = 0; for (int ii = 0; ii < nums.length; ii++) { if (nums[ii]%2 != 0) { cntOdds++; } } int[] returnInts = new int[cntOdds]; int returnIndex = 0; for (int ii = 0; ii < nums.length; ii++) { if (nums[ii]%2 != 0) { returnInts[returnIndex] = nums[ii]; returnIndex++; } } return returnInts; } //---------------------------------------------------------------------------- // containsDigit - answers if the given String contains a Digit // // containsDigit("867-5309") --> true // containsDigit("Not a number") --> false // containsDigit("") --> false // containsDigit("jsmith5@gmail.com") --> true // public boolean containsDigit(String str){ boolean hasDigit = false; int ii = 0; while (!hasDigit && ii < str.length()) { if (str.charAt(ii) >= '0' && str.charAt(ii) <= '9') { hasDigit = true; } ii++; } return hasDigit; } //---------------------------------------------------------------------------- // percentAbove - takes an array of ints and a threshold (int) as parameters, // and returns the percentage of values in the array that // are above or equal to the threshold. Do not bother to round // the (double) result, messy long decimals are fine. // // // percentAbove({4,6,5,8,9}, 8) --> 40.0 // percentAbove({4,6,5,8,9}, 22) --> 0.0 // percentAbove({4,6,5,8,9}, 2) --> 100.0 // percentAbove({99, 70, 69, 82}, 75) --> 50.0 // percentAbove({},5) --> 0.0 // public double percentAbove(int[] values, int threshold) { if (values.length == 0) return 0.0; int countAbove = 0; for (int ii = 0; ii < values.length; ii++) { if (values[ii] >= threshold) { countAbove++; } } return 100.0 * countAbove / values.length; } //---------------------------------------------------------------------------- // starBox - takes two ints as parameters and returns // a rectangle of asterisks as a String // /* starBox(3,8) --> "******** ******** ********" starBox(4,2) --> "** ** ** **" starBox(1,1) --> "*" starBox(0,1) --> "" starBox(1,0) --> "" starBox(0,0) --> "" starBox(-3,4) --> "" starBox(2,-5) --> "" starBox(-2,-4) --> "" */ public String starBox(int row, int col){ String returnString = ""; for (int ii = 0; ii < row; ii++) { for (int jj = 0; jj < col; jj++) { returnString += "*"; } if (ii < row - 1) { returnString += "\n"; } } return returnString; } //---------------------------------------------------------------------------- // countStr - takes a long String and a short String as parameters and returns // the number of times the short String occurs in the long String. // Returns 0 if the short string is longer than the long string. // The method is case-insensitive. // // countStr("banana", "an") —> 2 // countStr("Pupu Platter", "pu") —> 2 // countStr("Mississippi Mud", "Mo") —> 0 // countStr("Mississippi", "issi") —> 2 // countStr("Apple", "apple") —> 1 // countStr("Apple", "Apple Pie") —> 0 // countStr("", "") —> 0 public int countStr(String longString, String shortString) { if (longString.length() < shortString.length() || "".equals(shortString)) { return 0; } longString = longString.toUpperCase(); shortString = shortString.toUpperCase(); int countMatches = 0; boolean isMatch = false; int idxShort = 0; for (int idxLong = 0; idxLong <= longString.length() - shortString.length(); idxLong++) { isMatch = true; idxShort = 0; while (isMatch && idxShort < shortString.length()) { if (longString.charAt(idxLong + idxShort) != shortString.charAt(idxShort)) { isMatch = false; } idxShort++; } if (isMatch) { countMatches++; } } return countMatches; } }