Write a static method named allPlural that accepts an array of strings as a para
ID: 3860049 • Letter: W
Question
Write a static method named allPlural that accepts an array of strings as a parameter and returns true only if every string in the array is a plural word, and false otherwise. For this problem,a plural word is defined as any string that ends with the letter S, case-insensitively. The empty string "" is not considered a plural word, but the single-letter string "s" or "S" is. Your method should return true if passed an empty array (one with 0 elements). The table below shows calls to your method and the expected values returned: Array Call and Value Returned String[] a1 = {"snails", "DOGS", "Cats"}; allPlural(a1) returns true String[] a2 = {"builds", "Is", "S", "THRILLs", "CS"}; allPlural(a2) returns true String[] a3 = {}; allPlural(a3) returns true String[] a4 = {"She", "sells", "sea", "SHELLS"}; allPlural(a4) returns false String[] a5 = {"HANDS", "feet", "toes", "OxEn"}; allPlural(a5) returns false String[] a6 = {"shoes", "", "socks"}; allPlural(a6) returns falseExplanation / Answer
AllPluralTest.java
public class AllPluralTest {
public static void main(String[] args) {
String[] a1 = {"snails", "DOGS", "Cats"};
System.out.println(allPlural(a1));
String[] a2 = {"builds", "Is", "S", "THRILLs", "CS"};
System.out.println(allPlural(a2));
String[] a3 = {};
System.out.println(allPlural(a3));
String[] a4 = {"She", "sells", "sea", "SHELLS"};
System.out.println(allPlural(a4));
String[] a5 = {"HANDS", "feet", "toes", "OxEn"};
System.out.println(allPlural(a5));
String[] a6 = {"shoes", "", "socks"};
System.out.println(allPlural(a6));
}
public static boolean allPlural(String words[]) {
if(words.length == 0){
return true;
}
else{
for(int i=0; i<words.length; i++){
if(!(words[i].endsWith("s") || words[i].endsWith("S"))){
return false;
}
}
return true;
}
}
}
Output:
true
true
true
false
false
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.