In: Computer Science
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); } }
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