1 Getting Eclipse Running
2 Creating a Test Class
3 Test Cases
4 Tests with doubles
5 Developing New Methods
5.1 Header
5.2 Purpose Statement
5.3 Test Case
5.4 The Function Body
6 Once more, with feeling
7 No Extra Credit
Version: 5.3.0.6

Lab 1, CSC 102, Spring 2012

1 Getting Eclipse Running

Apologies in advance for all the hand-holding.

  1. Log in to your UNIX account

  2. Open a Terminal window, from "System Tools > Terminal" (IIRC).

  3. run Eclipse:

      eclipse

2 Creating a Test Class

  1. choose "File > New > Java Project"

  2. give it the name "Lab1"

  3. ignore all of the other scary bits

  4. click "Finish"

  5. choose "File > New > New JUnit Test Case"

  6. enter the package name "lab1"

  7. enter the name "FirstTest"

  8. click "Finish"

  9. click through the dialogs to add the JUnit 4 Library to the Build Path

Okay, we’re ready to try running it. Choose "Run > Run". The test case window should pop up, showing that the test failed.

On recent versions of Eclipse (Indigo?), this creates a complete test case class. If the file that’s created for you doesn’t run, edit the file to contain this text:

package lab1;

 

import org.junit.Test;

import static org.junit.Assert.*;

 

public class FirstTest {

 

        @Test

        public void test() {

                fail("oh dear");

        }

 

}

Next, replace the "fail" line with a working test:

  assertEquals(7,3+4);

Save, and run again. With luck, this test should pass, and show a green bar. Success!

Change the ’7’ to an ’8’. Save, run. Does it still succeed?

3 Test Cases

The most basic form of test case uses assertEquals with two arguments: the expected value, and the actual value.

assertEquals(<expected>,<actual>)

Running this will check that the result of evaluating the "actual" expression produces the same result as evaluating the "expected" expression.

Please pay attention to the ordering of these two, as it’s really not what you might expect. Specifically, the test expression occurs after the result expression. I personally think that this was a poor choice. Sorry, JUnit.

4 Tests with doubles

This works great for many different values, but it turns out that there’s a problem for double values; in particular, computation with doubles often produces answers that are very close to what you expect, but not exactly right. So, for instance, 2/3 and (1 - 1/3) don’t produce quite the same number when computing with doubles.

Formulate this as a test case: show that it fails.

When writing test cases for doubles, then, you need to include an extra number, indicating a "tolerance." That is, if the actual value and the expected value are separated by less than the tolerance, the test case is considered to have succeeded. This form looks like:

assertEquals(<expected>,<actual>,<tolerance>)

Update your prior test case to use this form, with a tolerance of 0.0001, and show that it succeeds.

5 Developing New Methods

It’s time to develop a new method. Specifically, let’s develop the "caloriesPerDay" method, that consumes a number of calories and a number of days, and divides the one by the other to figure out how many calories per day you’re consuming.

5.1 Header

First, we need to figure out what kind of data it should consume, and what kind of data it should produce. In this case, it’s going to accept a number of calories–probably a double–and a number of days–an integer. The result should be a double.

For this method, then, the header is:

double caloriesPerDay(double calories, int days) {

  return(   );

}

This header should go after the "class" line, and before the "test method" text.

5.2 Purpose Statement

Next, we need a one-line comment describing what it does. Add this text, above the header:

/**

 * Compute the number of calories consumed per day

 */

5.3 Test Case

Before writing down the body of the method, we’re going to write one test case. For a method such as this one, involving a simple numeric computation, one test case is plenty. After your other assertEquals, add this one:

  assertEquals(7000.0,caloriesPerDay(21000.0,3),0.0001);

5.4 The Function Body

Now, let’s head back to the body of the function; specifically, the parentheses following the "return". Fill in these parentheses with an expression that computes the number of calories per day.

Save your file, compile it, run the tests. Do they succeed? If not, grab your neighbor and ask him or her to tell you what went wrong.

6 Once more, with feeling

Develop the following methods. For each one, follow the steps given above. In particular, it’s important to me that you write down test cases before you write the body of the method. This requires discipline. Are you disciplined?

  1. Develop the minutesOfAudio method, that consumes a number of megabytes and returns the number of minutes of compressed audio that will fit on the device. Consult the internet to see how much space a minute of compressed audio will consume.

  2. Develop the maxCapacity method, that consumes a (rectangular) room’s width and length and determines how many humans will fit in the space. Make your own best estimate of the space that a human takes up.

  3. Develop a problem of your own, that consumes one or more numbers and returns another number. Provide the question text and the solution.

When you’ve finished these, show them to me for lab credit.

7 No Extra Credit

You will receive NO EXTRA CREDIT AT ALL for solving these problems in JavaScript as well. Show the solutions to me if you decide to do it anyway.