Documentation
Quick Start Guides

# How to setup an exercise?

## Introduction

In the following we will show how to create an exercise and set it up in CodeJudge. We will use Java as language, but it can easily be substituted by another language (see How to setup test data for my exercise?). In this guide, we will go through the following steps:

Please note this is a quick start guide, and CodeJudge supports many more features for testing than described here. But this guide will take you through the most common features.

### Create an exercise

First we should figure out what the exercise should be about. In this guide we will use the following problem:

Create a Java class Calculator that supports the method int sum(int a, int b) that should return the sum of the integers a and b. Furthermore Calculator should support the method int factorial(int a) that must return 1 * 2 * ... * a = a! (should work for a >= 0).

• The problem should be testable, i.e. input/output must be well defined.
• Clearly state what classes/methods/etc. the students should make and what the indended behaviour is.
• Mark the exercise with "CodeJudge" or something similar, to indicate the exercise can be tested on CodeJudge.
• For more complex problems, it can be a good idea to include samples of how the methods could be called and what they should return.

### Prepare a solution

Next, we'll create a solution to the problem. The solution will serve multiple purposes:

• We will get a feeling of how difficult the problem is to solve.
• It will be used to generate the expected output of our test data.
• We will use it as the "suggested solution" for the problem in CodeJudge.
public class Calculator {
public int sum(int a, int b) {
return a + b;
}

public int factorial(int a) {
if (a == 0) {
return 1;
}

return a * factorial(a - 1);
}
}
• Use the coding style you want your students to use, i.e. remember to do indentation, commenting, etc. since it will be used as a suggested solution.
• Spend some extra time to verify the solution is correct - otherwise, you will probably get some annoyed students.

### Prepare test cases

Let us jump right in to an example of a test file with test cases for our problem, and then discuss how it works afterwards:

// File: Tests.java
public class Test {
public static void main(String[] args) {
Calculator calculator = new Calculator();

/// Test
System.out.println(calculator.sum(3, 6));
/// Test
System.out.println(calculator.sum(-5, -9));
/// Test
System.out.println(calculator.sum(33, 64));
/// Test
System.out.println(calculator.factorial(5));
/// Test
System.out.println(calculator.factorial(10));
/// Test
System.out.println(calculator.factorial(2));
/// Test
System.out.println(calculator.factorial(1));
/// Test
/// Hint: Remember 0! = 1
System.out.println(calculator.factorial(0));
/// EOT
}
}

Hopefully, it is relatively clear what the above test cases are, so now we will look at the structure of the file. Note the above test code is a fully correct and runnable Java program when combined with the solution, so we can easily develop both the solution and the test file directly in our favorite IDE.

The beginning of a test should be marked with "/// Test", and after the last test a line with "/// EOT" must be written (EOT = End Of Test). A test can consists of multiple code lines. All lines not inside a test are considered common to all tests. Finally a hint can be added with "/// Hint: The hint text here" - this hint will only be shown, when a program fails the test. Hints are especially useful for special/tricky test cases.

When the file is uploaded to CodeJudge it is split into a runnable program for each test case. For instance the first test case will be (the class is automatically renamed):

public class Test01 {
public static void main(String[] args) {
Calculator calculator = new Calculator();

System.out.println(calculator.sum(3, 6));
}
}

You might now be thinking that we also need to specify what the expected output of our test cases are. However, in this example we will let CodeJudge produce the expected output by uploading just the "input" of the test cases and the solution, i.e. Tests.java and Calculator.java. (It is possible to manually specify the expected output instead - see the documentation for this)

Test cases can be much more complex than this example; they may involve input from the console, command line arguments, files, multiple correct outputs, etc. For more about this, see the documentation.

• Put common case tests first, then special cases and finally larger tests. This will make debugging easier for the students.
• Be thorough with the test cases, students sometimes make very unexpected solutions. In our experience 5-15 test cases will be sufficient for most problems - obviously depending on the difficulty.
• Try not to make unnecessary long and complex test code, remember the students should be able to understand what is tested.
• When creating tests for special cases, make sure the correct behaviour is actually described in the problem text.
• You can test error handling with "invalid" input by surrounding test code with a try-catch block (again, remember to tell students the program are supposed to throw an exception in this case).

### Upload to CodeJudge

It is now time to setup the exercise on CodeJudge, so we will navigate to our course on CodeJudge. Before setting up the exercise, we have to create an exercise group. Normally one will create an exercise group per week/exercise class/similar. Follow these steps:

1. Select "Exercises" in the menu.
2. Select "Edit" in the page tabs.
3. Click "New Exercise Group".
4. Give the exercise group a name (like "Week XX") and set when the students should be able to see the test group in "Visible From".
5. Click "Create".

We are now ready to setup the actual exercise. We will use the files "Calculator.java" and "Tests.java" created before:

1. Navigate to "Edit"-tab of the exercise group you just created. (You should already be here after creating the exercise group.)
2. Click "New Exercise".
3. Fill in the name of the exercise and the description
4. If Java is not the default language for your course, you should select this under "Languages" and press "Save".
5. Click "Next".

We are now ready to upload the solution we have made:

1. Select the file "Calculator.java" under "Solution Files".
2. Select when the students should be able to see the suggested solution: always, after they have solved the exericse, or never.
3. Click "Next".

Finally we must upload the test data. This will automatically generate the expected output as well. An exercise can consist of multiple test groups, but simple exercises only need one. See the documentation for more information:

1. Give the test group a name, e.g. "Tests"
2. For "Test Data" select the file "Tests.java". Leave the remaining fields unchanged.
3. Click "Create".

After a short while of processing, the exercise is ready!

### Test it!

As a final step, it is a good idea to see if the exercise actually works as expected (especially when it is the first time you create one). Navigate to the "View"-tab of the exercise and try to upload your solution. If you have not tried CodeJudge before, it is also a good idea to experiment with the different kind of mistakes students might make (compiler errors, programs producing exceptions, wrong output, etc).