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.
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:
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative...
Please Write in Java 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...
Assume the availability of a method named makeStars that can be passed a non-negative integer n and that returns a String of n asterisks. Write a method named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 asterisks, and then a line of n-2 asterisks, and so on. For example, if the method received 5 it would print: * * * * * * * * *...
Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...
3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...
Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...
****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...
in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...
Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...