CS206: Data Structures

Lab3: Javac, Java, and JUnit

Professor Blank


  1. Use command-line javac and java
  2. Use JUnit for testing
  3. Refine LinkedList and Node
  4. Use generics


  1. Not only must you have the correct answer, but it must also be formatted with proper indentation.
  2. Points are assigned to each test

This lab is worth 100 points.

Due date: Wed, Feb 15, 2017, beginning of lab.

[Your name in next cell as a level-2 header. Delete this line when done.]

[Enter your text here]

For this lab, we will use cells to merely create the Java files, and execute command-line commands.

For example, in the next cell we use the %%file magic to create a file named "Node.java" with these contents:

In [6]:
%%file Node.java

public class Node<T> {
    Node next;
    T value;
    public Node(T value) {
        this.value = value;
Created file '/home/dblank/nbgrader/cs206/source/cs206-lab3/Node.java'.

In the next cell, we will call the command-line which will compile the file Node.java in the shell:

In [8]:
! javac Node.java

We could write some testing code in "public static void main()" but there is a special library for writing tests, called JUnit. We will create a new file, "TestNode.java" and we will write many tests to test every aspect of our code.

First we define the file:

In [20]:
%%file TestNode.java

import junit.framework.TestCase;
public class TestNode extends TestCase {
    public void testConstructor() {
        Node<String> node = new Node<String>("Hello");
        assertTrue(node.value == "Hello");
Created file '/home/dblank/nbgrader/cs206/source/cs206-lab3/TestNode.java'.

To compile this, we need to include the TestCase class. We do this by including the junit4.jar file on the Java Class Path:

In [21]:
! javac -cp /usr/share/java/junit4.jar:. TestNode.java

Finally, we can now run the TestNode tests:

In [22]:
! java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore TestNode
JUnit version 4.5
Time: 0.005

OK (1 test)

OK (1 test)

Now, your turn. Do the same thing with your LinkedList class. That is:

  • first, define the class
  • compile it
  • create a TestLinkedList.java file
  • write lots of tests
  • compile it
  • run it
In [29]:
%%file LinkedList.java

public class LinkedList<T> {
    // paste your code here
Created file '/home/dblank/nbgrader/cs206/source/cs206-lab3/LinkedList.java'.
In [30]:
! javac LinkedList.java
In [26]:
%%file TestLinkedList.java

import junit.framework.TestCase;

public class TestLinkedList extends TestCase {
    // put your code here
Created file '/home/dblank/nbgrader/cs206/source/cs206-lab3/TestLinkedList.java'.
In [27]:
! javac -cp /usr/share/java/junit4.jar TestLinkedList.java
In [28]:
! java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore TestLinkedList
JUnit version 4.5
Time: 0.006
There was 1 failure:
1) warning(junit.framework.TestSuite$1)
junit.framework.AssertionFailedError: No tests found in TestLinkedList
	at junit.framework.Assert.fail(Assert.java:47)
	at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
	at org.junit.runners.Suite.runChild(Suite.java:115)
	at org.junit.runners.Suite.runChild(Suite.java:23)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:116)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:107)
	at org.junit.runner.JUnitCore.runMain(JUnitCore.java:88)
	at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:54)
	at org.junit.runner.JUnitCore.main(JUnitCore.java:46)

Tests run: 1,  Failures: 1

Tests run: 1,  Failures: 1


The reflection cell is the most import cell in this lab... in all labs. In your reflection, you should:

  • Reflect! Think and comment on what you have learned this lab, and this week.
  • Connect the ideas onto your own life, and experiences.
  • Comment on what you found challenging, and what you found intuitive.
  • How did this lab match your expectations?

Put your reflections in the cell below. You should write more than a paragraph. Remember:

  • don't indent paragraphs
  • use Markdown highlighting
  • use the spell checker

You should delete all of the cells except yours when you are done. Make sure to submit your lab.

[Please enter your text here]