Forums

Search results: 44

Hi Giuliana

If you're editing code within an Ace window and type the comma key while holding down Ctrl, you should get the menu I've shown above. Is that not working for you? Perhaps try doing that in the demo page on this website to see if it behaves differently from your site as confirmation?

If syntax highlighting isn't working with multiple languages, I think Ace is somewhat broken on your system. Has it ever worked properly in the past? I assume it also doesn't work within the question authoring form (e.g. the sample answer field)?

Ace is packaged into CodeRunner, complete with around 175 different language modes. The full set of modes should be displayed in the mode dropdown in the Ace menu, if you're able to bring that up.

My current guess is that Ace is failing to fetch the mode scripts. If that's the case, you should see an error in the JavaScript console. To check for this, use the Chrome browser, open the Developer Tools page (e.g. type Shift+Ctrl+I) and select the Console tab. Then load a CodeRunner question. What do you see in the Console tab? You might also try selecting the Network tab and reloading. Are there any 404s in there?

I'm struggling to think of a likely cause for this issue, but three vague possibilities are:
  1. The CodeRunner install is corrupted. Updating to the latest version might be worth a try.
  2. The file access permissions are wrongly set within the ace folder of the CodeRunner plugin. 
  3. There's some extra security plugin on your Moodle site that is disallowing the loading of the mode files.

Hi all,

I've run into a problem and I wondered if there's any way round it? I had some Java CodeRunner questions running where students were required to upload additional files. Unfortunately, some of the students forgot to upload these which meant their tests failed. I tried going back in and editing the questions to add these files as support files and ran a dry run of a regrade. This worked but then would change the results of the students who did remember to upload the files or (harder to detect) the students who uploaded the files but managed to accidentally introduce errors into them by adding things like package statements. Is there anyway the student files can be ignored (where they exist) and instead just use my support files? I tried editing the questions to disallow additional files but I guess since their answer has already been entered that if they've uploaded a file it will still be there in the background even though it no longer appears in their answer?

To give an example:

Students have to add code to the answer box.

They also have to add Person.java as an additional file. If I upload Person.java as a support file is there anyway I can run a regrade to use my file? I'm guessing it will either not allow both files to exist in the same directory with the same name or if it does allow it, it will cause an error due to the naming conflict and not run the tests?

Is there anyway round this?

Thanks,

Martin

I don't teach Java myself and I thought someone who did might pipe up instead. But in their absence ...

I think you would need to construct a Java question type that is scripted in Python. It could build the appropriate package file structure, copy relevant files into place, then compile and run the code. This old thread includes a demo question type that could probably be adapted to your needs. See in particular the posting by Peter Sander on Tuesday, 26 July 2016, 8:20 AM.
 
 

Hi Richard,

at the moment our students mainly program our Java exercises in Eclipse and add these files to "supporting-files-area":

Java code example

QUESTION:
In Eclipse we use package-declarations like:

package uebungsblatt_1;

public class InvalidBirthdayException extends Exception {
    String message;

    public InvalidBirthdayException() {
        super();

    }
    ...


Here (https://coderunner.org.nz/mod/forum/search.php?id=1&search=java+packages ) I found:

{{ STUDENT_ANSWER|replace({'package': '// package'}) }}

This works fine with all code in the coderunner editor.

Is there any way to apply the above command to the supporting files ( "InvalidBirthdayException.java" in our case)?   I know this is a heavy one. :-)

Best Regard from Coburg
Chris

Question Authors' Forum -> Parson's Problems -> Re: Parson's Problems

by Richard Lobb -

This was a very timely posting, as our stage 1 teaching team has just been discussing whether to use Parson's Problems. So I jumped in over the weekend and tried to get something working, using your code as a starting point. 

It turned out not to be all that easy, but I do have a working system, albeit a rather ugly one from the question-author's perspective (hopefully not from the student's).

I ripped out all the unnecessary JavaScript (which was most of it) but still had to rewrite most of what was left in order to be able to sync CodeRunner with both the LHS and RHS panels. Having got that working I wondered why we even had two panels - it seems to waste a lot of valuable screen real estate. So I collapsed the UI down into a single panel.

I haven't yet got rid of the JQuery (two version). I would have hoped we could just use Moodle's JQuery but it didn't work out of the box and I haven't had time to explore further. I have however stripped out the underscore package which isn't needed.

Constructing a prototype for such a question type proved problematic. I really need to find a decent way to pass parameters to HTML-UI questions. The best I've been able to come up with is to define the global-extra code in the prototype as a Twig macro, which you then invoke as a single line within the Global Extra field. It's a rather ugly non-intuitive approach but it does work easily enough once you know what to do.

I attach the prototype and an example question. But they'll only work in CodeRunner 4.0 or later (which I released around January this year). Do you have that? 

Here's what it looks like when first loaded:

Initial view of Parsons Problem

And when finished:

Solved parsons problem

There is currently no way to handle distractors, but they weren't something I wanted. They could be done in this single-pane approach by adding a 'disable' toggle to each code line. There is also no syntax colouring, but that didn't appear to work with the original js-parsons code, either. 

Let me know if you're able to get it working or not. Please realise it's still a work-in-progress and will undoubtedly change over the next few weeks. I'll move the question to the University of Canterbury github repo of question types soon.

Thanks again for introducing me to js-parsons and generally kicking things into action. And for the idea of using a MutationObserver.

Richard


Wow, you've had a busy day while I was asleep!

That's exactly the sort of thing I would have recommended if you hadn't beaten me to it. Well done!

The files initially loaded into the working directory are all write protected for security, so that student code can't corrupt question-author-supplied files. So indeed you needed to create yourself a separate directory first.

My Java's pretty rusty, but it all seems OK to me. The documentation for -d says the directory must already exists but you haven't created the 'dist' directory explicitly. I assume the documentation is out of date?

The code assumes the main class is called Main. Another option might be to assume that if the student provides a class in the answer box, then that is the main class?

If you haven't already done so, I suggest making that into a new question type. Then, over time, you add template parameters to add functionality, such as checking for required constructs in student code, style checks, etc.

I assume you've discovered that you can set the Ace language separately from the template language, so students get Java syntax colouring?

Something to consider for the future is whether you could profit from a combinator template. You've written a per-test template, and that's how the JavaProgram  question type usually behaves anyway; by default, even with combinator templates, CodeRunner drops back to one-test-at-a-time mode when each test has its own standard input. But that means you edit and recompile the student code for each test. If you instead used a combinator template with the Allow multiple studins box checked, too, you could get by with just a single compile. Assume the compile works, you would then loop to run each test, setting up standard input from the TEST.stdin field. You need to insert the SEPARATOR string between outputs.

However, that's probably more complexity than you want right now, and all it does is speed up testing a bit.

Well, in the end I rolled up my sleeves and created a python template as you suggested.

I first faced an issue trying to write to files owned by apache (as jobe is executed as jobe), so I decided to place all the source in a src directory, place the compiled classes in dist and then execute.

I try to obtain the class name for the text in the answer box and, if a file is supplied with that name, it gets overwritten.

e.g.: the answer box is prefilled with a public class Main {} but the student submits their Main.java which is the one which they expect to be executed.

I comment the package declaration from every file and then execute the class named Main (as per the other question).

I think everything works. Do you see anything weird here?

Thank you!

import os
import subprocess
import sys
import re

# The files that are going to be compiled.
files = []

# We create a src directory to place the (edited) source files.
# This is because we cannot write to the original source files
# for permission issues.
os.mkdir('src')

# We infer the submitted answer class name.
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
match = re.search(r'class (.+?)[\W{]', student_answer)
if match:
classname = match.group(1)
student_answer = student_answer.replace('package', '// package')
out = os.path.join('src', classname + '.java')
with open(out, 'w') as file:
file.write(student_answer)
files.append(out)

# We remove the package declaration from all the files.
for f in os.listdir():
if f.endswith('.java'):
with open(f, 'r') as file:
code = file.read()
code = code.replace('package', '// package')
out = os.path.join('src', f)
with open(out, 'w') as file:
file.write(code)
files.append(out)

# Compile
return_code = subprocess.call(['javac', '-d', 'dist'] + files)

if return_code != 0:
print('** Error de compilació **\nComprova el missatge d\'error i torna a provar-ho.', file=sys.stderr)
else:
# Execute
subprocess.call(['java', '-cp', 'dist', 'Main'])

Hi, of course this is related to the other question.

So, the IDE we use automatically sets up the package for the project, which is unnecessary for these kind of assignments.

I think the issue with that code was the spaces at both sides of the pipe:

{{ STUDENT_ANSWER|replace({'package': '// package'}) }}

This apparently works.

Do you think this is a bad idea if I'm using a java_program question type? I don't expect them to submit more than a single class in the answer box (but I expect them to provide multiple (0+) files as attachments, one class in each.

Also, they'll import some code from the Java API.

The removal of packages would only be an issue if they wanted to distinguish between java.lang.String and my.package.String, right?

Thank you!

EDIT: this replace does not work in the attachments! Agh! Any workaround there?

What fails exactly?

If you use the line

{{ STUDENT_ANSWER | replace({'package foobar;': ''}) | replace({'public class ': 'class ' }) }}

as the body of the template, then you'll have stripped the public class from the student answer which will then defeat the name fiddling that tries to name the student answer according to the name of their main class.

But what about:

{{ STUDENT_ANSWER | replace({'package foobar;': ''}) }}

?

Assuming, of course, that students have been asked to declare their code as being in a package foobar. And that they're pasting their answer into the main answer box, not as an attachment.

If you're trying to do any clever editing of student's code, I do recommend that you switch to using a Python-scripted question type that gives you complete control over the whole edit/compile/run process, including dealing with any attachments.

Hello.

We will use this to grade rather simple Java projects that will have no need for the package system.

I've seen two threads here that dealt with very complex solutions that attempted to implement the package system.

My question is: is there a simple way to just remove all package declarations?

Like the last line in this message:

{{ STUDENT_ANSWER | replace({'package foobar;': ''}) | replace({'public class ': 'class ' }) }}

I tried doing this and failed.

Thank you.