Question

In: Computer Science

JAVA CODE Give the definition of a static method called showArray that has an array of...

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.

Solutions

Expert Solution

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;

}


Related Solutions

JAVA CODE Give the definition of a static method called showArray that has an array of...
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.
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT