#cs-swapping-implementations-lab
- Compile and run Java code and tests.
- Replace one implementation with another.
- Try out two errors: overspecifying a type or trying to instantiate an interface.
Since this is the first lab, we'll keep it simple. You will take the code from the previous README and "swap the implementation"; that is, you will replace the LinkedList with an ArrayList. Because the code "programs to an interface", you will be able to swap the implementation by changing a single line (and adding an import
statement).
-
Set up your development environment. For this Track you will need to be able to compile and run Java code. We developed the examples using the using Java SE Development Kit 7. If you are using a more recent version, everything should still work. If you are using an older version, you might find some incompatibilities.
We recommend using an IDE like Eclipse that provides syntax-checking, auto-completion, and source code refactoring. These features help you avoid errors or find them quicky. However, if you are preparing for a technical interview, remember that you will not have these tools during the interview, so you might also want to practice writing code without them.
Either way, we'll assume that you know how to compile and run Java code in your environment of choice.
-
When you check out the repository for this lab, you should find these files:
-
CONTRIBUTING.md contains information about how you can notify us if you find a problem in a lesson.
-
javacs-lab01 is a directory that contains the source code for this lab.
-
LICENSE.md contains license information about these materials.
-
README.md contains the text you are reading now.
-
-
If you open
javacs-lab01
, you'll find these files:-
bin contains compiled
class
files. -
build.xml is an Ant file that makes it easier to compile and run the code.
-
lib contains the libraries you'll need (for this lab, just JUnit).
-
src contains the source code.
-
-
And if you navigate into
src/com/flatironschool/javacs
, you'll find the source code for this lab:-
ListClientExample.java
contains the code from the previous README. -
ListClientExampleTest.java
contains a JUnit test forListClientExample
.
-
-
Review
ListClientExample
and make sure you understand what it does. Then compile and run it. If you use Ant, you can navigate tojavacs-lab01
and runant ListClientExample
. You can read about Ant here.NOTE: You might get a warning like
List is a raw type. References to generic type List<E> should be parameterized
. To keep the example simple, we didn't bother to specify the type of the elements in the List. If this warning bothers you, you can suppress it by replacing each List or LinkedList withList<Integer>
orLinkedList<Integer>
. -
Review
ListClientExampleTest
. It runs one test, which creates aListClientExample
invokesgetList
, and then checks whether the result is an ArrayList. Initially, this test will fail because the result is a LinkedList, not an ArrayList. Run this test and confirm that it fails.NOTE: This test makes sense for this exercise, but it is not a good example of a test. Good tests should check whether the class under test satisfies the requirements of the interface; they should not depend on the details of the implementation.
-
In the
ListClientExample
, replace LinkedList with ArrayList. You might have to add an import statement. Compile and runListClientExample
. Then run the test again. With this change, the test should now pass. -
To make this test pass, you only had to replace
LinkedList
withArrayList
in the constructor. You did not have to change any of the places whereList
appears. What happens if you do? Go ahead and replace one or more appearances ofList
withArrayList
. The program should still work correctly, but now it is "overspecified". If you change your mind in the future and want to swap the interface again, you would have to change more code. -
In the
ListClientExample
constructor, what happens if you replaceArrayList
withList
? Why can't you instantiate aList
?