Lab 2
IntelliJ and IntLists
Last updated
Was this helpful?
IntelliJ and IntLists
Last updated
Was this helpful?
Lab spec:
Welcome to lab 2! This lab is primarily focused on getting IntelliJ set up. Like the previous guide, I won't be going over setup at all. For now, I'll be focused on IntLists and JUnit.
An IntList
is 61B's implementation of an integer linked list. IntList
has two main properties: head
and tail
. You can see these properties defined in the following code.
We call a method destructive if it modifies the original object passed into the method. Conversely, we call a method non-destructive if it preserves the original object passed into the it. Below, I've provided a few code snippets, and your job is to figure out if it is destructive or not.
Here is the base code that will be run:
Following are various versions of method()
. Try to figure out if each method is destructive or not, and as a bonus, what value each variable will end up as.
Here, I'll break down the JUnit test syntax. You'll have to write a lot of tests over the course of the semester, so it's best to get to know the syntax early.
Lines 1-2 contain the import statements for JUnit. These are important for the Java compiler to understand where to find the relevant JUnit expressions, and if you don't include these, you'll probably see a lot of errors in IntelliJ.
Lines 13-17 contains the setup to do the test. Here, we are verifying that our IntList.list()
method is functioning properly. In lines 13-15, we construct the expected IntList
in a way that we know is correct (remember, we pretend we don't know if IntList.list()
is correct or not). Then, in line 17, we construct the actual value that we wish to test.
Line 11 contains the @Test
annotation. This basically signals to JUnit that the following method is a JUnit test and should be run. There are some other options that you can include in this annotation, which I'll link .
Line 18 contains the JUnit assert -- this is what is actually being tested. There are a number of different assert statements, such as assertEquals
, assertTrue
, and assertNotNull
. A full list can be found . By convention, we put the expected value first, then put the actual value (aka the value that we are testing).