Lab 5: More Methods and an Introduction to Testing with JUnit


Due Monday, September 18 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 AreaCalculationMethod.java

Download the AreaCalculationMethod.java file, and open it in jGrasp (or a text editor of your choice). You'll need to implement the concatenate method, without modifying the main method. Unlike with previous labs, you may not modify the code in the main method. More details are in the comments of AreaCalculationMethod.java. Example output of the program is shown below, with user input shown in bold:

Enter width: 2
Enter height: 3
Area: 6

Step 2: Edit DegreeConversionMethod.java

Download the DegreeConversionMethod.java file, and open it in jGrasp (or a text editor of your choice). You will need to define a method in order to get this code compiling, and you may not modify the main method. DegreeConversionMethod.java contains more details in the comments. Example output of the program is shown below, with user input shown in bold:

Enter temperature in Celsius: 4.5
Fahrenheit: 40.1

Step 3: Edit CompoundInterestMethod.java

Download the CompoundInterestMethod.java file, and open it in jGrasp (or a text editor of your choice). You will need to define a method in order to get this code compiling, and you may not modify the main method. CompoundInterestMethod.java contains more details in the comments. Example output of the program is shown below, with user input shown in bold:

Enter principle (long): 53
Enter annual interest rate (double): 0.02
Enter number of times interest is compounded per year (int): 6
Enter number of years invested (int): 18
Compound interest including principal: 75.92099374307139

Step 4: Configure jGrasp to Use JUnit

If you're using jGrasp, you'll need to configure jGrasp to use JUnit. You can do this by following the instructions here. You only need to do this once per computer you have jGrasp installed on.

Step 5: Download, Open, Compile, and Run the Tests In AreaCalculationMethodTest.java

Download the AreaCalculationMethodTest.java file, being sure to put it in the same folder/directory as your AreaCalculationMethod.java file from step 1. AreaCalculationMethodTest.java contains a bunch of tests for the calculateArea method you wrote in step 1. Use the instructions here to open AreaCalculationMethodTest.java as a tests file, then compile and run the tests. All the tests have been provided, and assuming you implemented your calculateArea method correctly, they should all pass.

While you don't need to edit any code for this step, be sure to save AreaCalculationMethodTest.class in a safe spot. This file is produced automatically by jGrasp upon compiling and running the tests. This file will later be submitted along with the rest of the code you write.

Step 6: Copy Over AddFive.java and AddBoth.java From the Previous Lab

In the previous lab, you had to write code in AddFive.java and AddBoth.java. Copy over those two files into the same folder/directory where the rest of the files for this lab are. In this lab, you will write tests for the methods you wrote in the previous lab.

Step 7: Edit AddFiveTest.java

Download the AddFiveTest.java file, being sure to put it in the same folder/directory as your AddFive.java file. AddFiveTest.java contains a bunch of tests for the addFive method you wrote in the previous lab. One test has already been provided for you, namely zeroPlusFiveIsFive. You'll need to write two more tests, namely:

  1. onePlusFiveIsSix: Tests that addFive called with the parameter 1 produces 6, using assertEquals
  2. twoPlusFiveIsSeven: Tests that addFive called with the parameter 2 produces 7, using assertEquals

Step 8: Edit AddBothTest.java

Download the AddBothTest.java file, being sure to put it in the same folder/directory as your AddBoth.java file. AddBothTest.java contains a bunch of tests for the addBoth method you wrote in the previous lab. One test has already been provided for you, namely zeroPlusOneIsOne. You'll need to write three more tests, namely:

  1. onePlusOneIsTwo: Tests that addBoth called with the parameters 1 and 1 produces 2, using assertEquals
  2. twoPlusOneIsThree: Tests that addBoth called with the parameters 2 and 1 produces 3, using assertEquals
  3. twoPlusFourIsSix: Tests that addBoth called with the parameters 2 and 4 produces 6, using assertEquals

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 5”. From here, you can upload your answers and your code. Specifically, you must turn in the following six 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 five files.

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