This worksheet walks through the steps of setting up and testing a Java development environment. It includes some exercises to recap and practice basic Java development and testing skills. Solutions to the exercises are at the end of this worksheet; attempt all exercises yourself and consult the solutions only for comparison or when you get seriously stuck.
The Java Development Kit (JDK) is the most basic environment used to develop Java programs; it comes with command tools (a compiler, debugger, etc.), but no editing support. We will install an integrated development environment (IDE) later for more convenient development.
These instructions are not tested.
Download the appropriate JDK for your operating system, e.g., Oracle OpenJDK or Amazon Corretto OpenJDK. Extract and run the installer.
On MacOS with Homebrew: brew install openjdk. Test that the installation was successful by opening a terminal and executing java -version. You should see openjdk version "21.0.1" 2023-10-17 LTS or similar printed on the console.
If your version differs, multiple JDKs may be installed on your system.
Run
/usr/libexec/java_home -V
to print a list of all installed JDKs.
Check that there is a version 21.x in the list and run the following command to switch your current terminal temporarily to the latest Java version:
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
In order to switch permanently, add the line above to ~/.zshrc (if you use ZSH) or ~/.bash_profile (if you use bash).
Hello, World!Create a new file named HelloWorld.java and copy the code below into the file.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Open a console/terminal on your computer and change into the directory that contains your file HelloWorld.java.
Compile the code with javac HelloWorld.java.
Type ls -la, you should see a file HelloWorld.class in the list of printed files.
Run the compiled program with java HelloWorld.
You should see the output Hello, World! on the console.
If you do not yet have VS Code installed, follow the instructions to install a version with Java support.
If you do have VS Code installed, open the extensions pane in VS Code and search and install the extension Extension Pack for Java.
Hello, World!Create a new workspace SE350 and a folder 01-environmentsetup in the workspace.
Create subfolders src and test.
Create a file HelloWorld.java in folder src.
Copy the code below into the file.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
The IDE should start compiling the file automatically and you should see Run|Debug appear above the main method.
Click Run, which should open a terminal and print Hello, world!.
In folder 01-environmentsetup/src, create a file Fibonacci.java and implement a class with a static function fib that computes a Fibonacci number.
Implement a main function that parses the first argument as an Integer and prints the corresponding Fibonacci number to the console.
Run your program java Fibonacci 6; you should see fib(6) = 8 printed to the console (or similar, depending on how you chose to print).
Follow the JUnit setup instructions to install JUnit support in VS Code.
VS Code installs the JUnit linbraries into SE350/lib.
In folder 01-environmentsetup/test, create a file FibonacciTest.java.
Implement a unit test with JUnit to test the Fibonacci numbers 1..9 and run it.
public class Fibonacci {
public static int fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println("fib(" + n + " = " + fib(n));
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FibonacciTest {
@Test
public void testFib() {
assertEquals(0, Fibonacci.fib(0));
assertEquals(1, Fibonacci.fib(1), 1);
assertEquals(1, Fibonacci.fib(2));
assertEquals(2, Fibonacci.fib(3));
assertEquals(3, Fibonacci.fib(4));
assertEquals(5, Fibonacci.fib(5));
assertEquals(8, Fibonacci.fib(6));
assertEquals(13, Fibonacci.fib(7));
assertEquals(21, Fibonacci.fib(8));
assertEquals(34, Fibonacci.fib(9));
}
}