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 // 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...
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().
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.
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
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...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT