write in java 1. Assume the availability of a method named makeLine that can be
ID: 644389 • Letter: W
Question
write in java
1.
Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints a SYMMETRIC triangle of O's (the capital letter O) as follows: first a line of nO, followed by a line of n-2 O's (indented by one space), and then a line of n-4 O's (indented by two spaces), and so on. For example, if the method received 5,0 (or 4,0) it would print:
OOOOO
OOO
O
Note: in the above output , the first line contains 0 spaces before the first O, the next line 1 space, and so on.
Note: These instructions state what the method does when k is zero, but it is up to you, the programmer, to determine what it does when k is not zero and use it for your advantage.
The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeLine to accomplish the task of creating Strings of varying lengths.
2.
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 of integers representing the starting and ending indexes of the portion of the array to be tested for being a palindrome. The function returns whether that portion of the array is a palindrome.
An array is a palindrome if:
the array is empty (0 elements ) or contains only one element (which therefore is the same when reversed), or
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.
3.
The maximum-valued element of an integer -valued array can be recursively calculated as follows:
If the array has a single element , that is its maximum (note that a zero-sized array has no maximum)
Otherwise, compare the first element with the maximum of the rest of the array -- whichever is larger is the maximum value .
Write an int method named max that accepts an integer array , and the number of elements in the array and returns the largest value in the array . Assume the array has at least oneelement .
4.
The elements of an integer -valued array can be initialized so that a[i] == i in a recursive fashion as follows:
An array of size 0 is already initialized ;
Otherwise
set the last element of the array to n-1 (where n is the number of elements in the array , for example, an array of size 3 will have its last element -- index 2-- set to 2; and
initialize the portion of the array consisting of the first n-1 elements (i.e., the other elements of the array )
Write a void method named init that accepts an integer array , and the number of elements in the array and recursively initializes the array so that a[i] == i.
5.
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:
If the array has a single element and it is odd, return the value of that element ; otherwise 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 of the array ; if the first element is NOT odd, simply return the result of finding the product of the odd elements of the rest of the array
6.
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 theinteger is present as an element in the array . Searching for a particular value in an array can be performed in the following recursive manner:
If the array has no elements , the value is not there.
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 .
7.
Write a recursive, void method , reverse, that accepts an integer array , a starting index and an ending index, and reverses the array . Reversing an array involves:
Nothing if the array has 0 or 1 elements .
swapping the first and last elements of the array and then reversing the remainder of the array (2nd through next-to-last elements ).
8.
An array is sorted (in ascending order) if each element of the array is less than or equal to the next element .
An array of size 0 or 1 is sorted
Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted.
Write a boolean -valued method named isSorted that accepts an integer array , and the number of elements in the array and returns whether the array is sorted.
Explanation / Answer
Answer
This program uses recursion to make repeated calls to the same function to achieve our required output. This function adds a character to the beginning and end of every repeated call to the function makeLine.
private static String makeLine(int count, char character)
{
if (count <= 0)
{
return( "");
}
//use recursion to add a character before each line
return (character + makeLine(count - 1, character));
}
This is the program which tests the function makeLine:
public class test2
{
private static String makeLine(int count, char character)
{
if (count <= 0)
{
return( "");
}
//use recursion to add a character before each line
return (character + makeLine(count - 1, character));
}
public static void main(String args[])
{
makeLine(9,'0');
}
}
Output:
Now, recursively calling the makeLine function in the printTriangle Function:
public static void printTriangle(int n, int k)
{
if (n <= 0)
{
return;
}
System.out.println(makeLine(k, ' ') + makeLine(n * 2 - 1, '0') + makeLine(k, ' '));
printTriangle(n - 1, k + 1);
}
The complete program:
public class test2
{
public static String makeLine(int count, char t)
{
if (count <= 0)
{
return("");
}
//use recursion to add a character before each line
return (t + makeLine(count - 1, t));
}
public static void printTriangle(int n, int k)
{
if (n <= 0)
{
return;
}
System.out.println(makeLine(k, ' ') + makeLine(n * 2 - 1, '0') + makeLine(k, ' '));
printTriangle(n - 1, k + 1);
}
public static void main(String args[])
{
printTriangle(8,2);
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.