Question

In: Computer Science

Write a class called CombineTwoArraysAlternating that combines two lists by alternatingly taking elements, e.g. [a,b,c], [1,2,3]...

Write a class called CombineTwoArraysAlternating that combines two
  lists by alternatingly taking elements, e.g.

     [a,b,c], [1,2,3] -> [a,1,b,2,c,3].
  
  You must read the elements of the array from user input by reading
  them in a single line, separated by spaces, as shown in the examples
  below.

  Despite the name of the class you don't need to implement this class
  using arrays.  If you find any other way of doing it, as long as it
  passes the test it is ok.

  HINTS:

  You don't need to use any of my hints.  As long as you pass the test
  you can write the code any way you want.

  I used:

        String [] aa = line.split("\\s+");        

  to split the String contained in the variable called 'line' into
  strings separated by at least one white space character.  For
  example if line is "hello world how are you?" the previous statement
  populates array aa in such a way that

     aa[0] is: "hello" 
     aa[1] is: "world" 
     aa[2] is: "how"
     aa[3] is: "are" 
     aa[4] is: "you?"

  I used the method Arrays.toString from java.util.Arrays to print
  arrays.  For example the following lines of code:

        String line = "hello world how are you?";
        String [] aa = line.split("\\s+");
        //Arrays.toString(aa) takes array aa and returns a nicely formatted String
        System.out.println(Arrays.toString(aa));

  produce the following output:

        [hello, world, how, are, you?]
        

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Scanner;

public class CombineTwoArraysAlternating {

    public static String[] combineAlternating(String[] aa, String[] bb) {
        String[] result = new String[aa.length+bb.length];
        int index = 0;
        for (int i = 0; i < aa.length || i < bb.length; i++) {
            if (i < aa.length) {
                result[index++] = aa[i];
            }
            if (i < bb.length) {
                result[index++] = bb[i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line1 = in.nextLine();
        String[] aa = line1.split("\\s+");
        String line2 = in.nextLine();
        String[] bb = line2.split("\\s+");
        System.out.println(Arrays.toString(combineAlternating(aa, bb)));
    }
}


Related Solutions

Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
Write a C++ function which will search for TWO elements. The two elements must be right...
Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x /...
Hello, this question relates to a class I am taking called introduction to C++. I have...
Hello, this question relates to a class I am taking called introduction to C++. I have no experience writing programs and outside of learning out of a textbook, and studying on my own, have little understanding of logic. I have been assigned a problem that requires me to write a program for a Grocery Bill, where the consumer inputs the price for 5 items, that the program calculates the total with a 6% sales tax. I really am not sure...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Collectively the elements of group 15 are called – (a) pnictogens (b) pnicopens (c) nicopen (d) None of these
Collectively the elements of group 15 are called as _____________.(a) pnictogens(b) pnicopens(c) nicopen(d) None of these
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT