How to use e('java') in Customisation Template

How to use e('java') in Customisation Template

by Joannes Reyes -
Number of replies: 3

Hello. How can I use e('java') syntax in Customisation Template for Java code?

For Python, the syntax I use is as below.

__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""
I want to know how it should be for a Java code so I can check my student's answer more thoroughly. My coderunner vesion is 4.2.3.

Can someone show me an example Customisation Template for a Java code?
Thank you.

In reply to Joannes Reyes

Re: How to use e('java') in Customisation Template

by Richard Lobb -
I normally run any code that prechecks a student's code in Python. Then if the code passes all the prechecks I run the student code (in whatever language ) using the Python3 subprocess. In the case of Java you have to compile first, of course. Sample Python3 code to run Java from Python is in the multilanguage question type - if you create a question of that type and click the Customise checkbox you'll see the code in the Template box and can strip out the bit you need. If you're using this approach then of course the syntax is the same as you've give above. You use a python3 question type but it has to be customised of course and under Advanced Customisation you need to set the Ace Language (which is what the student gets to see) to Java.

However, at least for the Java Method and Java Class question types you could certainly do the prechecking in Java if you wanted. Here's the prototype for a rudimentary Java method that requires the student to use a while loop rather than a for loop. I attach an xml export. This just a proof of concept: the test for a while loop is very weak. A student could defeat this by putting in a comment using the word "while". 
 
This still uses a combinator template - a per-test-case template would be a bit simpler and would allow you to print rather than raising an exception, but would be slower to run.
 
public class __tester__ {
    static String SEPARATOR = "#<ab@17943918#@>#";
    {{ STUDENT_ANSWER }}

    public static void main(String[] args) throws Exception {
        String answer = "{{ STUDENT_ANSWER | e('java')}}";
        if (!answer.contains("while")) {
            throw new Exception("You don't seem to have used a while loop!");
        } else {
            __tester__ main = new __tester__();
            main.runTests();
        }
    }

    public void runTests() {
{% for testCase in TESTCASES %}
    {
    {{ testCase.testcode }};
    {% if not loop.last %}
    System.out.println(SEPARATOR);
    {% endif %}
    }
{% endfor %}
    }
}

 

Doing a write-a-program question in Java would be harder, though. As with the Python approach, you'd have to do the prechecks first then compile and run the student code in a subprocess. 

In reply to Richard Lobb

Re: How to use e('java') in Customisation Template

by Joannes Reyes -
Hi Richard,

Thank you so much for your detailed reply. I did as you stated in your post, and was able to get the results I wanted. I was not aware that if you chose Multilanguage for Question Type, a default customisation template would be used, then as you said, I can just insert the checks I have using Python syntax.

One compromise though is my students have to choose "Java" each time they answer my question. I tried playing with how to make it so I can choose Java for Question Type and have the Customisation Template run in Python, but as you stated, it's not possible. The closest thing I can have is have is copy the __tester__ class you posted to the template, and insert a call to some Java method or Java class I can ask my students to write and have the template call it from main.

Again, thank you so much for your help! I use Coderunner a lot in my Introduction to Programming class (Python), and now also in my Data Structures and Object Oriented Programming class (Java).

-Joannes Reyes
In reply to Joannes Reyes

Re: How to use e('java') in Customisation Template

by Richard Lobb -
Good to hear that you have a solution.

If you start with the multilanguage question type but want to restrict it just to Java, you can edit the Ace Language field under Advanced Customisation so that it's just Java. Then the language-selection dropdown will disappear. But the Twig {{ LANGUAGE }} variable won't be defined then, so you'll have to hard-code 'java' in the template.