Lab 3: Introduction to Loops


Due Friday, December 8 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: Get OperationsBetween.java Compiling

Download the OperationsBetween.java file, and open it in jGrasp (or a text editor of your choice). This program takes two command-line arguments representing the minimum and maximum number in a numeric range. Currently, the code does not compile. As a first step, you need to get this code to compile. To this end, you'll need to implement a “stub” for the sum method, which will allow the call to sum in the main method to compile. A stub has already been written for the product method; you should write something similar for sum.

Step 2: Implement the sum Method

In the previous step, you wrote a stub for sum to get the code compiling. Now replace the code in the stub with a loop that will compute the sum between min and max. You will need to use a loop for this purpose, and a for loop is likely the best fit. You should start from min, and then iterate up to max, using a temporary variable that you increment along the way. You will also need another variable to store the result. Comments in the file contain further details.

Once sum (and only sum) is correctly implemented, you should see the following output if run with the command-line arguments 3 4:

Sum: 7
Product: 1

Step 3: Implement the product Method

Replace the code in the stub for product with a loop that will compute the product between min and max. This code should look very similar to the code you wrote for sum. Further details are in the file.

Once both product and sum are correctly implemented, you should see the following output if run with the command-line arguments 5 8:

Sum: 26
Product: 1680

Further example output is shown below for the command-line arguments 3 2 (note that the minimum end of the range is greater than the maximum end of the range):

Sum: 0
Product: 1

Further example output is shown below for the command-line arguments 3 3 (the minimum end of the range is equal to the maximum end of the range):

Sum: 3
Product: 3

Step 4: Run the Tests in OperationsBetweenTest.java

Download the OperationsBetweenTest.java file, being sure to put it in the same folder/directory as your OperationsBetween.java file. This file contains tests for the code you wrote in the previous steps. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. This time around, you do not need to write any of your own tests, though you are free to do so if you desire. All of the provided tests should pass.

Step 5: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110 class. Click “Assignments” on the left pane, scroll down to “Weekly Activities”, then click “Lab 3”. From here, you can upload your answers and your code. Specifically, you must turn in the following file:

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 file.

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