Lab 1: Compiling and Running Java Code, with Printing and Arithmetic


Due Thursday, September 1 at 11:59 PM

Goals for This Lab

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

Provided files:

Note on the use of jGrasp

All instruction in this class is based around jGrasp. This ensures that all instruction will be the same no matter what operating system is used. That said, jGrasp is not strictly required for this class; if you are already comfortable with your own setup, feel free to use it. Be advised, however, that it is your responsibility to make sure that your own setup works correctly if you choose not to use jGrasp.

Step-by-Step Instructions

Step 1: Familiarize Yourself with a jGrasp Tutorial

Read and familiarize yourself with the short jGrasp tutorial available here. This tutorial discusses how to open, edit, compile, and run Java code using jGrasp. These steps will be heavily utilized in this class.

Step 2: Download all Required Files

Download all the files listed under Provided files above. Ideally, these should all be placed in the same folder/directory, and it should be someplace that you can easily access later. For example, these could go on either a personal laptop, the Z drive, or a personal USB drive.

Step 3: Open HelloWorld.java

Start jGrasp. Exactly how you do this is dependent on your particular system. Once jGrasp is open, go to File -> Open, and navigate to the copy of HelloWorld.java you saved in the previous step. The images below illustrate exactly how this can be done.







Step 4: Compile and Run HelloWorld.java

Once HelloWorld.java has been opened, you can compile it according to these instructions from the tutorial in step 1. Specifically, follow steps 5 (Compile Your Code) and 6 (Run Your Code) in the tutorial.

Once HelloWorld.java has been compiled, a new file will appear in the left pane of jGrasp, namely HelloWorld.class. You will submit this file later as part of your solution.

Step 5: Open, Compile, and Run SingleArithmetic.java

Using the same steps above (specifically steps 3 and 4), open, compile, and run SingleArithmetic.java. This process will similarly produce SingleArithmetic.class, which you will submit later as part of your solution.

Step 6: Open MultiPrint.java

Using the same process as described in step 3, open MultiPrint.java.

Step 7: Edit MultiPrint.java

In this step, you will write the first bit of code for this class. The Java program in MultiPrint.java is supposed to have the following output when run:

first
second
third
fourth
fifth

However, if you compile and run MultiPrint.java as-is, it will only produce:

first
second

You must add three lines of code (specifically statements) which will make MultiPrint.java print out the three missing items. You should write your code immediately after the comment which reads ---WRITE YOUR CODE HERE---. You will need to use System.out.println as part of this process; your code should look very similar to the code that is already in the file.

Step 8: Compile and Run MultiPrint.java

Once you think your edits are complete, go ahead and compile and run the code using the same steps as before (specifically step 4). Check the output of your program to make sure it matches the expected output shown in the previous step. If the output doesn't match, you will need to further edit your program and try again. Once the output matches, you may proceed to the next step.

Step 9: Open MultiArithmetic.java

Using the same process as described in step 3, open MultiArithmetic.java.

Step 10: Edit MultiArithmetic.java

This step requires you to write some more code. Specifically, you will need to write four System.out.println statements which show the computed values of various arithmetic expressions. The comments starting with TODO describe exactly what these expressions should be. You should write your code on the line following the ---WRITE YOUR CODE HERE--- markers. Your code should look very similar to the code which has already been provided in the file.

Step 11: Compile and Run MultiArithmetic.java

Once you think your edits are complete, go ahead and compile and run the code using the same steps as before (specifically step 4). Check the output to make sure that it matches what you'd expect. In particular, the last four items output should be, in order:

70
3
-28
114

Step 12: 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 1”. From here, you can upload the two .class files you produced, along with the code you wrote. Specifically, you must turn in the following four files:

Note that you must turn in the .java files for MultiPrint.java and MultiArithmetic.java, NOT the .class 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.