2. Create the Java parts

Previous: 1. Create the Tex source from a template | Next: 3. Grading and feedback

The Java parts consist of two source files: one for the wrapper and one for the evaluator. Let's start with the wrapper. Jmmtex searchers for Java source files in the src/java folder of the package containing the Tex source of the problem. The search is recursive (thus, descends into subfolders). For this tutorial, the package is mumie_test_entities. You can use a different one, of course. The directory structure relative to src/java is assumed to reflect the Java package structure. Thus, a source file foo/bar/Bazz.java relative to src/java is assumed to contain the Java class with the qualified name foo.bar.Bazz.

Select the "Packages" tab in the left frame in MIAU. Unfold the src/java tree of the package and navigate to the folder where you want to create the wrapper class. For this tutorial, the folder is net/mumie/test/program relative to src/java. Right-click in the folder and choose "New > "Other...":

In the window that opens, select "Java > "Class":

Click "Next" and fill in a name for the new class. In this tutorial, we choose "MinimumInArrayWrapper":

Click "Finish". MIAU creates a template for the Java source and opens it in the editor:

Complete the template as follows:

package net.mumie.test.program;

public class MinimumInArrayWrapper
{
  public static int getMin(int[] array) {
    /*-@SAMPLE_SOLUTION_START@-*/
    int length = array.length;
    int min = Integer.MAX_VALUE;
    for ( int i = 0; i < length; ++i )
      min = Math.min(min, array[i]);          
    return min;
    /*-@SAMPLE_SOLUTION_END@-*/
  }
}

The two comments

/*-@SAMPLE_SOLUTION_START@-*/ 

/*-@SAMPLE_SOLUTION_END@-*/ 

are essential: they enclose the part of the source which is later replaced with the user input. Save the source.

Create another source template for the evaluator in the same way. In this tutorial, we create the evaluator in the same direcory as the wrapper, and give the class the name MinimumInArrayEvaluator. Complete the template as follows:

package net.mumie.test.program;

import java.util.Random;

public class MinimumInArrayEvaluator
{
  // Return values:
  public static final int OK = 0;
  public static final int WRONG_FOR_EMPTY_ARRAY = 1;
  public static final int WRONG_RESULT = 2;
  public static final int RUNTIME_ERROR = 3;

  // Maximum size of test arrays:
  protected static final int MAX_SIZE = 10;

  // Number of tests:
  protected static final int NUM_TESTS = 10;

  protected static Random random = new Random();

  protected static int[] createTestArray() {
    int size = random.nextInt(MAX_SIZE);
    if ( size == 0 ) size = MAX_SIZE; // Empty array are tested separately
    int[] array = new int[size];
    for (int i = 0; i < size; i++)
      array[i] = random.nextInt();
    return array;
  }

  public static int getExpectedResult(int[] array) {
    int length = array.length;
    int min = Integer.MAX_VALUE;
    for ( int i = 0; i < length; ++i )
      min = Math.min(min, array[i]);          
    return min;
  }

  protected static int getUserResult(int[] array) {
    int result = 0;
    try {
        result = MinimumInArrayWrapper.getMin(array);
      } catch (Throwable throwable) {
        throwable.printStackTrace();
        System.exit(RUNTIME_ERROR);
      }
    return result;
  }

  public static void main(String[] params) {
    for (int i = 0; i < NUM_TESTS; i++) {
        int[] array = createTestArray();
        int expectedResult = getExpectedResult(array);
        int userResult = getUserResult(array);
        if ( userResult != expectedResult )
          System.exit(WRONG_RESULT);
      }

    int[] emptyArray = {};
    int expectedResult = getExpectedResult(emptyArray);
    int userResult = getUserResult(emptyArray);
    if ( userResult != expectedResult )
          System.exit(WRONG_FOR_EMPTY_ARRAY);

    System.exit(OK);
  }
}

Save the evaluator source and return to the Tex source. We have to adapt the \prgwrapper and \prgevaluator commands. Their arguments must contain the qualified names of the respective classes. Thus:

\title{Minimum in array}

Write a Java method which computes and returns the minimum number of an array
of integer numbers. If the array is empty, the largest possible integer number
is to be returned. Complete the following code fragment:

\begin{prganswer}{20}{80}
  public static int getInt (int[] array) {
    @USER_ANSWER@
  }
\end{prganswer}

\prgwrapper{net.mumie.test.program.MinimumInArrayWrapper}
\prgevaluator{net.mumie.test.program.MinimumInArrayEvaluator}

\prggrading{0}{1.0}{praise-user-answer}                %
\prggrading{1}{0.5}{array-may-be-empty}                % <-- Change this
\prggrading{3}{0.0}{code-caused-runtime-error}         %

Now close your two java- and the tex-source you just created and reopen (only) the tex-source in the checkin-tree. This is necessary to refresh the two tabs "Evaluator" and "Wrapper". Through these two tabs you can access now all resources for this problem whithout switching between the checkintree and packages.

You can find another more advanced way of structuring the evaluator here: Advanced Evaluator Techniques
There it will be explained how you create subtests combined with a more individual feedback.

For security reasons inner classes are not allowed in the wrapper. How to use classes,interfaces, etc. written by yourself is explained here: Correction libraries

In the next part of the tutorial, we learn how to control grading and feedback.

Previous: 1. Create the Tex source from a template | Next: 3. Grading and feedback

create_program_problem_07_src_java_new_other.png (142 KB) Marek Grudzinski, 04/07/2013 11:53 AM

create_program_problem_08_wizzard_java.png (29.8 KB) Marek Grudzinski, 04/07/2013 11:53 AM

create_program_problem_09_java_class_properties.png (51.7 KB) Marek Grudzinski, 04/07/2013 11:53 AM

create_program_problem_10_java_template.png (100 KB) Marek Grudzinski, 04/07/2013 11:53 AM

Create_program_problem_07_src_java_new_other Create_program_problem_08_wizzard_java Create_program_problem_09_java_class_properties Create_program_problem_10_java_template
Add picture from clipboard (Maximum size: 500 MB)