In: Computer Science
JAVA CODE
Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
After reading the question, I could see two different possible solutions for this method. One is printing each character on separate lines forward and backward (one line for one character), and the other is printing characters forward in one line, and printing characters reverse in another line. When reading the first portion of your question, I’m thinking about the first type definition, and when it comes to the last portion of the question (“these two lines of characters”) I’m feeling like second version is what you are looking for. So I’m attaching code for both versions that I can think of. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change (if this is not what you are expecting). If you are satisfied with the solution, please rate the answer. Thanks
//required method (first version)
public static char[] showArray(char[] array) {
// creating a char array twice the capacity to store the contents of
// array in forward and reverse
char[] combined = new char[array.length * 2];
int index = 0;
// looping from first character to the last
for (int i = 0; i < array.length; i++) {
// printing current character on a separate line
System.out.println(array[i]);
// adding current character to combined array at index 'index'
combined[index] = array[i];
// updating index
index++;
}
// looping from last character to the first
for (int i = array.length - 1; i >= 0; i--) {
// printing current character on a separate line
System.out.println(array[i]);
// adding current character to combined array at index 'index'
combined[index] = array[i];
// updating index
index++;
}
// returning combined array
return combined;
}
// required method (second version)
public static char[] showArray(char[] array) {
// creating a char array twice the capacity to store the contents of
// array in forward and reverse
char[] combined = new char[array.length * 2];
int index = 0;
// looping from first character to the last
for (int i = 0; i < array.length; i++) {
// printing current character on the same line
System.out.print(array[i]); // add space if you want
// adding current character to combined array at index 'index'
combined[index] = array[i];
// updating index
index++;
}
// printing line break
System.out.println();
// looping from last character to the first
for (int i = array.length - 1; i >= 0; i--) {
// printing current character on the same line
System.out.print(array[i]); // add space if you want
// adding current character to combined array at index 'index'
combined[index] = array[i];
// updating index
index++;
}
// printing line break
System.out.println();
// returning combined array
return combined;
}