CPE 102
Winter
2008
Lab 2
Errata:
None – any updates or corrections will appear here.
Objectives
- To become familiar with the ArrayList,
Integer, Double, and String classes from the Java
Standard Library.
-
To continue becoming familiar
with JUnit tests.
-
To become familiar with the
idea of test-driven development.
Resources
- Your text.
- Your peers.
- Your instructor.
- The Java Standard
Library – Book-mark this web page, you will be using the Java Standard
Library documentation regularly this quarter. Find String in the
alphabetic ordered “All Classes” frame and pay special attention to the
discussion of the “+” operator for string concatenation – especially
useful for this lab.
Ground
Rules
- You must use the ArrayList
class to store the various user responses (see Hints & Suggestions
below for assistance).
-
You must complete
the specification items in order.
Orientation
You will be developing a simple Java class named
InputSeparator that makes use
of the ArrayList class from the Java Standard Library and separates the inputs into different
collections based on the type of input.
You can (and should) write this class in the structure suggested to you
from the JUnit tests.
Specification
- Your program must
match the specification exactly as described.
-
Supplied to you is the
following JUnit test class and
test driver. Download both files. You will yet not
be able to compile them as they depend on an InputSeparator class.
The test driver, named AllTests, simply runs the tests specified in the test
class.
The test class, named InputSeparatorTest, tests all of the InputSeparator
class's functionality. Writing these tests before implementing the
InputSeparator class forces us to design the class's implementation a little
before jumping in to code it. For example, this causes us to think about what
kind of methods and functionality will be required in the InputSeparator class.
-
In the InputSeparatorTest
class, look at the testInputSeparatorConstructor() method. Make sure that you understand
what is going on in this method. What functionality for the InputSeparator
class can you deduct from reviewing this code?
Now, for a slightly more complicated test, look at the testAddIntegers()
method. Again, make sure that you understand what is going on in this method.
What functionality for the InputSeparator class can you deduct from reviewing
this code? What methods should be implemented, and what are their
parameters and return types?
-
There exists a more concise way
to compare ArrayLists to each other. Look at the testAddIntegers2() method. It
is identical to the previous method except for the way in which it checks for
equality at the end. Arrays.asList() simply restructures an array as a list.
Comparing one list to another relieves you of the duty of having to compare
each individual ArrayList item and the ArrayList length as both are included in
the ArrayList comparison.
Also look at the testAddDoubles() and testAddOthers() methods, which
also use the more concise equality check. Again, make sure that you
understand what is going on in these methods. What functionality for
the InputSeparator class can you deduct from reviewing this code? What
methods should be implemented, and what are their parameters and return
types?
- Create a class named
InputSeparator. Do you know what the name of the file must be? If not, read
item 4 of lab 1 more carefully. Use the information deducted in item 4 of this
lab to
implement the class. After your class is fully implemented and compiles,
compile and run
the supplied JUnit test class to verify its correctness.
-
As described in the Orientation
section of this lab, one purpose of the InputSeparator class is to separate
inputs into different collections based on the type of input. However, thus far
each test only inputs a single type.
The testAddAll() test method should test the InputSeparator to ensure that this
functionality works properly. Following the comments, fill in the placeholders
to complete this test method. Make sure that this test case passes.
-
The InputSeparator class should
have two constructors: a detault constructor (no parameters) that initializes each ArrayList, and a
constructor with a single ArrayList<String> parameter which contains a
collection of objects to be added. Using the testAddAll() test method as an
example, write the JUnit test for this functionality. Then, implement the
constructor in the InputSeparator class. Hint: you do not
need to write any new code in this constructor! Simply use already-implemented
methods.
Hints
& Suggestions
- The ArrayList class is a
generic
collection class meaning it can be used to collect any type of
object. You must specify the type
of object that will be collected in an ArrayList when you declare and
construct it. You will be working
with int, double, and String values.
You cannot store primitive data values (byte, char, short, int,
long, float, double, or boolean) in an
ArrayList. To add integer and real
values to an ArrayList you will need to use the appropriate wrapper
classes. A wrapper class is a class
that “wraps” some other type and provides methods to operate on the
wrapped type. There is a class
called Integer that wraps primitive int values and a class called Double
that wraps primitive double values in the Java Standard Library – read the
javadocs for help on using these classes.
-
Here is an example of declaring
and constructing an ArrayList for Integer objects:
ArrayList<Integer> intList = new
ArrayList<Integer>();
Notice that the type to be collected in an ArrayList, in
this case Integer, is specified immediately after the ArrayList class by
putting it inside the <>-brackets.
To create an ArrayList to collect different types simply substitute the
appropriate class name for Integer in the example above.