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...
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
Create two lists; one contains 5 elements and the other one is empty. Write a python...
Create two lists; one contains 5 elements and the other one is empty. Write a python program that iterates through the first one, pops the elements from the end of it, then it pushes them one by one to the empty list. As your program iterates through the first list, clean it before processing the data, meaning if any element is like a special character ( , . ; : ) it must be discarded and not gets pushed to...
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  
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.
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.
To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed...
To be Completed in C# Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception. Design the program such that it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT