Lab 15: More Iteration over Arrays with Loops


Due Monday, April 2 at 11:59 PM

Goals for This Lab

By the time you have completed this work, you should be able to:

Provided files:

Step-by-Step Instructions

Step 1: Edit AllEqual.java

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 no arguments or only one argument, it should consider all the elements equal to each other.

Step 2: Open AllEqualTest.java as a Test File

Download the AllEqualTest.java file, being sure to put it in the same folder/directory as your AllEqual.java file. This file contains tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You do not need to submit these tests, and you are not required to write any of your own tests for this portion. However, for full credit, all the tests must pass.

Step 3: Edit AddValueToArray.java

Download the AddValueToArray.java file, and open it in jGrasp (or a text editor of your choice). This program will add a given value (passed as a command-line argument) to a given array. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output for the program is shown below, with the command-line argument 5:

8
13
11
9

Each line of the above output corresponds to a single element of the array, starting from index 0.

Step 4: Open AddValueToArrayTest.java as a Test File

Download the AddValueToArrayTest.java file, being sure to put it in the same folder/directory as your AddValueToArray.java file. This file contains tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You do not need to submit these tests, and you are not required to write any of your own tests for this portion. However, for full credit, all the tests must pass.

Step 5: Edit AddValueNewArray.java

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
8
6
4
New array:
6
11
9
7

Note that the output shows the original, unchanged array, along with the new array.

Step 6: Open AddValueNewArrayTest.java as a Test File

Download the AddValueNewArrayTest.java file, being sure to put it in the same folder/directory as your AddValueNewArray.java file. This file contains tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You do not need to submit these tests, and you are not required to write any of your own tests for this portion. However, for full credit, all the tests must pass.

Step 7: Edit Take.java

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.

Step 8: Open TakeTest.java as a Test File

Download the TakeTest.java file, being sure to put it in the same folder/directory as your Take.java file. This file contains tests for the method you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You do not need to submit these tests, and you are not required to write any of your own tests for this portion. However, for full credit, all the tests must pass.

Step 9: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110L class. Click “Assignments” on the left pane, then click “Lab 15”. From here, you can upload your code. Specifically, you must turn in the following four files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other four files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.