Hands on Testing Java: Lab #3

Experiment #3: Arithmetic Expressions

Do this...
Create a test-case class named Experiment03Test.

Arithmetic

The process of computation involves the application of operators (the action or computation) to operands (the objects or data). Java provides a number of arithmetic operators that can be applied to numeric operands, including:

Operator Meaning
+ addition operator computes the sum of two numeric operands
- subtraction operator computes the difference of two numeric operands
* multiplication operator computes the product of two numeric operands
/ division operator computes the quotient of the division two numeric operands
% modulus operator computes the remainder of the division of two integer operands

Numeric Data Types

Java has two different types of numeric data: integers and real numbers.

An integer is usually represented with the int data type. You used this data type in the previous experiment. With an int, you cannot have a fractional amount in your number, but the number could be negative or positive.

We can make this declaration:

int i = 5, j = 2;

This declares two int variables, named i and j.

Helpful hint: Using double to denote real numbers is a historical, technical accident; it's not the most intuitive name to describe real numbers. So make a particular effort to remember "double" and its meaning in Java.

On the other hand, a real number is a number with a fractional amount, like 3.14159 (the most overused example of a real number). You cannot put these values into a variable declared as an int since you would lose the fraction part. Use double instead.

We can write this declaration:

double x = 5.0, y = 2.0;

Do this...
Create a test method testSimpleArithmetic(), and add these two declarations to it. The code should compile and run for a green bar.

Keep in mind that in this case you have two declarations to declare four variables. Also note that the green bar at this point it vacuous since you aren't testing anything yet.

The Arithmetic Operators

Addition, subtraction, and multiplication are similar for integers and real numbers. You've been using these operations for most of your life, so let's test your knowledge.

Do this...
Add these assertions to testSimpleArithmetic() after the declarations:

assertEquals("sum of i and j", ???, i + j);
assertEquals("sum of x and y", ???, x + y, 1e-8);
assertEquals("product of i and j", ???, i * j);
assertEquals("product of x and y", ???, x * y, 1e-8);

Replace the ???s with actual values so that the code actually compiles. Run the test-case class for a green bar.

I'll explain the 1e-8 in the next experiment.

Addition, subtraction, and multiplication hold only a few technical surprises, so these assertions suffice for now. Division and modulus, though, have some fundamental surprises.

Integer Division

Do this...
Create another test method in Experiment03Test, this one named testIntDivision(). Add these assertions to the new test method:

assertEquals(" 1 divided by 5", ???,  1 / 5);
assertEquals(" 2 divided by 5", ???,  2 / 5);
assertEquals(" 3 divided by 5", ???,  3 / 5);
assertEquals(" 4 divided by 5", ???,  4 / 5);
assertEquals(" 5 divided by 5", ???,  5 / 5);
assertEquals(" 6 divided by 5", ???,  6 / 5);
assertEquals(" 7 divided by 5", ???,  7 / 5);
assertEquals(" 8 divided by 5", ???,  8 / 5);
assertEquals(" 9 divided by 5", ???,  9 / 5);
assertEquals("10 divided by 5", ???, 10 / 5);

Wait to replace the ???s...

Each of these division expressions use int literals. A literal is a value we represent literally, not with a variable. Since these numbers do not have a decimal point in them, they're considered ints.

In order for you to fill in the ???s, you need to know that integer division throws away whatever is leftover. To divide 1 by 5, we think about how many times 5 goes into 1. Well, that's 0 times, right? Mathematically we're left with a remainder 1. Too bad! We asked Java for the quotient, not the remainder. Hence, 1 divided by 5 is 0, as integer division. (There's one answer for you!)

Do this...
Now replace the integer-division ???s. Compile, and run the test-case class for a green bar.

Real-Number Division

Real-number division is a bit different.

Do this...
Create another test method in Experiment03Test, this one named testDoubleDivision(). Add these assertions to the new test method:

assertEquals(" 1.0 divided by 5.0", ???,  1.0 / 5.0, 1e-8);
assertEquals(" 2.0 divided by 5.0", ???,  2.0 / 5.0, 1e-8);
assertEquals(" 3.0 divided by 5.0", ???,  3.0 / 5.0, 1e-8);
assertEquals(" 4.0 divided by 5.0", ???,  4.0 / 5.0, 1e-8);
assertEquals(" 5.0 divided by 5.0", ???,  5.0 / 5.0, 1e-8);
assertEquals(" 6.0 divided by 5.0", ???,  6.0 / 5.0, 1e-8);
assertEquals(" 7.0 divided by 5.0", ???,  7.0 / 5.0, 1e-8);
assertEquals(" 8.0 divided by 5.0", ???,  8.0 / 5.0, 1e-8);
assertEquals(" 9.0 divided by 5.0", ???,  9.0 / 5.0, 1e-8);
assertEquals("10.0 divided by 5.0", ???, 10.0 / 5.0, 1e-8);

Now the leftovers matter because a real-number (i.e., a double) allows us to represent fractional amounts. You may have to do some long division:

      0.2
    ------
5.0 | 1.0
     -1.0
     ----
        0

Do this...
Replace the real-number-division ???s so that the code compiles and runs for a green bar.

Integer Reaminders

You can get the remainder from an integer division, just not with the / operator. In grade school you probably phrased your division like this:

"Five goes into nine one time with a remainder of four."
"Five goes into one zero times with a remainder of one."

Your teacher actually had it right, thinking about the quotient and the remainder at the same time. This is a little harder to do in a computer program, so the quotient is computed with / and the remainder with %.

Incidentally, Java officially calls % the modulus operator. Technically, a modulus is different from a remainder operator, but not in a way that matters to us now.

Let's find those remainders.

Do this...
Create another test method in Experiment03Test, this one named testIntModulus(). Add these assertions to the new test method:

assertEquals(" 1 remainder 5", ???,  1 % 5);
assertEquals(" 2 remainder 5", ???,  2 % 5);
assertEquals(" 3 remainder 5", ???,  3 % 5);
assertEquals(" 4 remainder 5", ???,  4 % 5);
assertEquals(" 5 remainder 5", ???,  5 % 5);
assertEquals(" 6 remainder 5", ???,  6 % 5);
assertEquals(" 7 remainder 5", ???,  7 % 5);
assertEquals(" 8 remainder 5", ???,  8 % 5);
assertEquals(" 9 remainder 5", ???,  9 % 5);
assertEquals("10 remainder 5", ???, 10 % 5);

Replace the ???s with the appropriate remainders.

Test Count

Question #03.03.01 How many "Runs" are there reported in the JUnit window?
To be answered in Exercise Questions/lab03.txt.

This number should be at least 5 now.

Terminology

addition operator, arithmetic operators, computation, division operator, integer, literal, modulus operator, multiplication operator, operand, operator, real number, remainder, subtraction operator