Question

In: Computer Science

Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....

Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below:

foo
bar
baz

In the above example, three elements were taken. However, it's possible that we may request to take more elements than we have. In such a case, we should take as many as possible. For example, consider the program output below when given the command-line arguments foo bar:

foo
bar

In this case, we were only given two arguments, so we cannot take the full three requested. As such, all the arguments we could take are shown.

As a third example, we might have more arguments than what we want to take, in which case we only take as many arguments as requested. Example output illustrating such a case is shown below, with the command-line arguments alpha beta gamma delta epsilon:

alpha
beta
gamma

In the above example, even though we were given more than three arguments, main is hard-coded to take only three. As such, only three are taken, specifically the first three.

public class Take {
    // You will need to write a method that "takes" a given number
    // of elements from a given array, producing a new array
    // holding the elements "taken".  The original array is
    // unchanged.
    //
    // For example, if we call:
    //
    // take(new String[]{"first", "second", "third"}, 1)
    //
    // ...the result should be the String array:
    //
    // {"first"}
    //
    // ...because 1 element was "taken" (always from the front).
    //
    // If the number of elements to take exceeds the number
    // of elements in the array, then take as many as can be taken.
    // For example, if we call:
    //
    // take(new String[]{"hi", "bye"}, 25)
    //
    // ...the result should be the String array:
    //
    // {"hi", "bye"}
    //
    // Even though 25 elements were requested in the above example,
    // since the array only held 2 elements, the same 2 elements
    // are returned.
    //
    // If no elements are to be taken, then an empty array is
    // returned.  For example, with the call:
    //
    // take(new String[]{"one", "two", "three"}, 0)
    //
    // ...the result should be an empty String array.  This behavior
    // should also occur if a negative number of elements are
    // requested.
    //
    // Two hints:
    // 1.) Math.min may come in handy.
    // 2.) It is possible to implement this without any if statements.
    //
    // TODO - write your code below this comment.
    //
    
    // DO NOT MODIFY printArray!
    public static void printArray(String[] array) {
        for (int index = 0; index < array.length; index++) {
            System.out.println(array[index]);
        }
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) {
        String[] taken = take(args, 3);
        printArray(taken);
    }
}

Solutions

Expert Solution

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 you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//Take.java


public class Take {
        // You will need to write a method that "takes" a given number
        // of elements from a given array, producing a new array
        // holding the elements "taken". The original array is
        // unchanged.
        //
        // For example, if we call:
        //
        // take(new String[]{"first", "second", "third"}, 1)
        //
        // ...the result should be the String array:
        //
        // {"first"}
        //
        // ...because 1 element was "taken" (always from the front).
        //
        // If the number of elements to take exceeds the number
        // of elements in the array, then take as many as can be taken.
        // For example, if we call:
        //
        // take(new String[]{"hi", "bye"}, 25)
        //
        // ...the result should be the String array:
        //
        // {"hi", "bye"}
        //
        // Even though 25 elements were requested in the above example,
        // since the array only held 2 elements, the same 2 elements
        // are returned.
        //
        // If no elements are to be taken, then an empty array is
        // returned. For example, with the call:
        //
        // take(new String[]{"one", "two", "three"}, 0)
        //
        // ...the result should be an empty String array. This behavior
        // should also occur if a negative number of elements are
        // requested.
        //
        // Two hints:
        // 1.) Math.min may come in handy.
        // 2.) It is possible to implement this without any if statements.
        //
        // TODO - write your code below this comment.
        //

        public static String[] take(String[] array, int count) {
                // below statement will store the max value among 0 and count to count.
                // So if count is negative, 0 will be stored in count, if count is
                // positive, then count is unchanged.
                count = Math.max(0, count);
                // finding minimum value among array size and count, using this value as
                // resultant array size, so if count is bigger than array size, it will
                // be set to array size
                int size = Math.min(array.length, count);
                // creating array
                String[] result = new String[size];
                // copying first size number of elements from array to result
                for (int i = 0; i < size; i++) {
                        result[i] = array[i];
                }
                // returning result
                return result;
        }

        // DO NOT MODIFY printArray!
        public static void printArray(String[] array) {
                for (int index = 0; index < array.length; index++) {
                        System.out.println(array[index]);
                }
        }

        // DO NOT MODIFY main!
        public static void main(String[] args) {
                String[] taken = take(args, 3);
                printArray(taken);
        }
}

/*OUTPUT when "alpha beta gamma delta" are passed as arguments*/

alpha
beta
gamma

Related Solutions

Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text...
Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, parses them as ints, and then displays: The arithmetic sum of the arguments The smallest argument The largest argument If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line arguments 1 2 3 4 5 is shown...
Step 3: Edit MultiSplit.java Download the MultiSplit.java file, and open it in jGrasp (or a text...
Step 3: Edit MultiSplit.java Download the MultiSplit.java file, and open it in jGrasp (or a text editor of your choice). You will need to write a method that will take an array of Strings (String[]) along with a regular expression, and will call split() on each one of those Strings with the given regular expression. The result of this method should be a two-dimensional array of String (String[][]). You will also need to write a method to print out this...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT