Lab 12: Command-line Arguments, charAt(), Integer.parseInt, and Arrays


Due Wednesday, November 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 FirstChar.java

Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. 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 argument foo is shown below:

First char: 'f'

Note that the output character is surrounded by single quotes ('); you'll need to match this output exactly. As a hint, you can do this with the help of string concatenation.

Step 2: Edit Sum2.java

Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4:

Sum: 7

Further example output for the command-line arguments 28 10 is shown below:

Sum: 38

Step 3: Edit ProductUpTo3.java

Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold:

Enter first integer: 3
Enter second integer: 4
Enter third integer: 5
Product: 60

More details about the method you need to write are in the comments of ProductUpTo3.java. Note that the method you need to write must handle input arrays holding fewer than three elements! The next step will check that additional behavior.

Step 4: Open ProductUpTo3Test.java as a Test File, and Edit It

Download the ProductUpTo3Test.java file, being sure to put it in the same folder/directory as your ProductUpTo3.java file. This file contains tests for the methods 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 need to write a number of tests in this file, and all of them must pass. The comments in the file provide more details.

Step 5: Turn in Your Solution Using Canvas

Log into Canvas, and go to the COMP 110 class. Click “Assignments” on the left pane, then click “Lab 12”. From here, you can upload your answers and 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.