Do this...
Create a test-case class named
Experiment02Test
.
Every program needs a place to store its data internally. A program that adds 2 and 3 together isn't particular interesting; a program that adds two numbers read in from the keyboard is much more interesting. However, we need some way in the program to refer to information that's, for example, read in from the keyboard.
As a programmer, you might have a general idea what type of data the program will get (e.g., "a number"), but you do not know the exact data itself (e.g., "5" or "3.14159"). A variable allows a Java programmer to specify the type of data and give a name to the data without actually knowing the exact value. The program itself will know the value when it is executed.
Java (more so than some other programming languages) does not
like to be surprised and tolerates little ambiguity. So you
cannot start using a variable without first
declaring it. Here's a
declaration for an int
variable named
value
initialized to 5:
int value = 5;
int
is the data type of the
variable value
; int
denotes the set of
integers, negative and positive. We'll look at int
in
more detail in the next experiment; for now, the key is that the
Java compiler is happy with the value
variable. You
can think of it as a box with the name "value
", and at
runtime, you should be able to find the integer 5 in it.
Do this...
Add the following test method to this experiment's test-case
class:
public void testVariables() { int value = 5; assertEquals("variable value evaluates to 5", 5, value); }
Compile and run the test-case class. You should get a green bar.
This is the pattern for a variable declaration:
variable-declaration pattern |
|
An identifier is any "word" used in a program. The identifiers in a variable declaration become variables in the program.
Here is another example:
int anotherValue = 2, sunday = 0, saturday = 6;
This declaration statement declares three variables.
Do this...
Add this declaration to testVariables()
(inside its curly braces), and add three
appropriate assertEquals()
statements to test these
variables. Compile and run the test-case class for a green
bar.
You do not have to test this, but note that these three declaration statements are equivalent to the one declaration above:
int anotherValue = 2; int sunday = 0; int saturday = 6;
Java has certain rules for creating identifiers:
_
).We, along with most Java programmers, will also hold to certain conventions for our variables:
anotherValue
, not
AnotherValue
or anothervalue
.)While the compiler will enforce the rules but not the conventions, conventions are good to follow so that your code is readable by others and so that it's easier for you to read code written by other people.
Make sure that you run all of the tests.
Question #03.02.01 How many "Runs" are there in
the JUnit window when you run your test-case classes?
To be answered in Exercise
Questions/lab03.txt
.
If your answer to that question is 1, then you're running only the tests in the current class. Run all of the tests in your project.
convention, data type, declaration, declaring, identifier, variable