Hands on Testing Java: Lab #3

Java Expressions (Objects and Operations)

Introduction

Today's exercise involves a series of experiments, each investigating a different aspect of a fundamental Java concept:

An expression is a sequence of one or more operands combined together with zero or more operators; when evaluated, an expression produces a value.

This code is an expression:

12

This fits the definition of an expression, since it consists of one operand (i.e., the value 12) and zero operators that combine to produce a value (i.e., 12).

This code is perhaps something you'd normally think of as an expression:

2 + 3

It consists of two operands (i.e., the values 2 and 3) and one operator (+) that combine to produce a value (5).

Operands need not be literal values:

2.5 * x - 1.0

This too fits the definition of an expression since it consists of three operands (i.e., value 2.5, value 1.0, and variable x) and two operators (*, -) that combine to produce a value (1 less than the product of 2.5 and x). It does not matter that x is an unknown value as far as writing the expression goes; it only matters when the program wants to evaluate it at runtime.

These examples have been arithmetic expressions, that is, expressions whose operators are familiar arithmetic operators. Java provides all of the familiar arithmetic operators as well as many operators you've probably never seen before. This lab exercise is tour through many (though certainly not all) of Java's operators.

Getting Started

Do this...

The Experiments

This lab consists of several experiments to play around with expressions. It'll make extensive use of the JUnit library for the various experiments you'll work through.

The main foci of this lab are to write some JUnit tests and to answer some questions along the way.

Experiment #1: JUnit Test Case

Experiment #2: Declarations

Experiment #3: Arithmetic Expressions

Experiment #4: Relational Expressions

Experiment #5: Logical Expressions

Experiment #6: Operator Precedence

Experiment #7: Operator Associativity

Experiment #8: Characters and Character Strings

Experiment #9: Method Calls

Experiment #10: Constant Declarations

Experiment #11: Assignment Statements

Experiment #12: Assignment Shortcuts

Experiment #13: Increment and Decrement Expressions

Submit

Terminology

arithmetic expression, expression, operand, operator, value