Answer:
// Method's name: has_more_zs
// Parameters are text1 and text2 to hold the two phrases to be tested
public static String has_more_zs(String text1, String text2) {
// Create and initialize z1 to zero
// Use z1 to count the number of zs in text1
int z1 = 0;
// Create and initialize z2 to zero
// Use z2 to count the number of zs in text2
int z2 = 0;
//Create a loop to cycle through the characters in text1
//Increment z1 by one if the current character is a 'z'
int i = 0;
while (i < text1.length()) {
if (text1.charAt(i) == 'z' || text1.charAt(i) == 'Z') {
z1 += 1;
}
i++;
}
//Create a loop to cycle through the characters in text2
//Increment z2 by one if the current character is a 'z'
i = 0; //Re-initialize i to zero
while (i < text2.length()) {
if (text2.charAt(i) == 'z' || text2.charAt(i) == 'Z') {
z2 += 1;
}
i++;
}
//Using the values of z1 and z2, return the necessary statements
if (z1 > z2) {
return "The phrase '" + text1 + "'" + " has more occurences of z than the phrase " + "'" + text2 + "'";
}
else if (z1 < z1) {
return "The phrase '" + text2 + "'" + " has more occurences of z than the phrase " + "'" + text1 + "'";
}
else if (z1 == z2) {
return "The strings have the same number of z";
}
else {
return "Neither string contains the the letter z";
}
}
Explanation:
Explanation to answer has been given in the code as comments. Please refer to the comments for the details of the code.
The source code file has been attached to this response and saved as "NumberOfZs.java"
Hope this helps!