(1) locate the error in the following method and show how to fix it: private sta
ID: 3635487 • Letter: #
Question
(1) locate the error in the following method and show how to fix it:
private static boolean isEven (int n) {
if (n % 2 == 0)
return true;
}
(2) Simplify the following method as much as possible:
private static int cube (int n) {
int result ;
result = n * n * n;
return result;
}
(3) Write a class method name capitalizeWords that takes one parameter of type String.
The method should print its parameter, but with the first letter of each word capatilized.
For example, the call
capitalizeWords ( "abstract window toolkit") ;
will produce the following output:
Abstract Window Toolkit
// please provide explanations for your answers
Explanation / Answer
1. ans: public static boolean isEven (int n) { if (n % 2 == 0) return true; } 2. ans public static int cube (int n) { return(Math.pow(n,3)); } 3.ans public static String capitalizeWords (String string) { char[] chars = string.toLowerCase().toCharArray(); boolean found = false; for (int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.