Write a static method named mostBowlsFull, to be added to the Bowl class, which
ID: 3565584 • Letter: W
Question
Write a static method named mostBowlsFull, to be added to the Bowl class, which is passed an array of Bowl objects, and returns true if a strict majority of the Bowls in the array are not empty. Thus if the array consists of 11 bowls and 6 are not empty, your method should return true, but if the array consists of 12 bowls and 6 (or fewer) are not empty, your method should return false.
public static boolean mostBowlsFull(Bowl[] array){
int len = array.length();
int count = 0;
for(int i=0; i if(!array[i].isEmpty()){
count++;
}
}
return (count > len/2));
}
What is wrong with that ^
Class:
Explanation / Answer
public static boolean mostBowlsFull(Bowl[] array){
int len = array.length;
int count = 0;
for(int i=0; i < len; i++)
if(!array[i].getEmpty()){
count++;
}
return (count > len/2);
}
/*
You should return (count > len / 2), instead of (count > len), because you have to check whether the number of non-empty bowls is more than 'half' the total number of bowls or not!!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.