i have a couple of questions on java for my homework 20864 A \' array palindrome
ID: 3567452 • Letter: I
Question
i have a couple of questions on java for my homework
20864
A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward) Write a recursive, boolean -valued method , isPalindrome, that accepts an integer -valued array , and a pair ofintegers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome. The function returnswhether that portion of the array is a palindrome.
An array is a palindrome if:
20867
Write a recursive, int -valued method named productOfOdds that accepts an integer array , and the number of elements in the array and returns the product of the odd-valued elements of the array . You may assume the array has at least one odd-valued element . The product of the odd-valued elements of an integer -valued array recursively may be calculated as follows:
20868
Write a recursive, boolean -valued method named search that accepts an integer array , the number of elements in the array , and an integer (in that order), and returns whether the integer is present as an element in the array . Searching for a particular value in an array can be performed in the following recursive manner:
Explanation / Answer
A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward) Write a recursive, boolean -valued method , isPalindrome, that accepts an integer -valued array , and a pair ofintegers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome. The function returnswhether that portion of the array is a palindrome.
An array is a palindrome if:
public static boolean isPalindrome(String str, int start, int end){
// the array is empty (0 elements ) or contains only one element (which therefore is the same when reversed), or
if(str.isEmpty() || str.length()==1 || start > end)
return true;
// the first and last elements of the array are the same, and the rest of the array (i.e., the second through next-to-last elements ) form a palindrome.
else
return (str.charAt(start)==str.charAt(end) && isPalindrome(str, start+1,end-1));
}
Write a recursive, int -valued method named productOfOdds that accepts an integer array , and the number of elements in the array and returns the product of the odd-valued elements of the array . You may assume the array has at least one odd-valued element . The product of the odd-valued elements of an integer -valued array recursively may be calculated as follows:
public static int productOfOdds(int array[],int elements){
// If the array has a single element and it is odd, return the value of that element ; otherwise return 1.
if(elements == 1 && array[array.length-1]%2!=0)
return array[array.length-1];
else if(elements == 1 && array[array.length-1]%2==0)
return 1;
// Otherwise, if the first element of the array is odd, return the product of that element and the result of finding the product of the odd elements of the rest ofthe array ; if the first element is NOT odd, simply return the result of finding the product of the odd elements of the rest of thearray?
else if(array[array.length-elements]%2!=0)
return array[array.length-elements] * productOfOdds(array, elements-1);
return productOfOdds(array, elements-1);
}
Write a recursive, boolean -valued method named search that accepts an integer array , the number of elements in the array , and an integer (in that order), and returns whether the integer is present as an element in the array . Searching for a particular value in an array can be performed in the following recursive manner:
public static boolean search(int array[],int elements,int value){
// If the array has no elements , the value is not there.
if(elements ==0) return false;
// Compare the first element of the array to the value , if they're equal , the value is there; other search for the value in the rest of the array .
if(array[array.length-elements] == value)
return true;
return search(array,elements-1, value);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.