Question Authors' Forum

Java_Program Test Cases

Java_Program Test Cases

by Tommi Saksa -
Number of replies: 1
Hi,

is the following possible in the first place. We are using Java and the Question type is Java_program.

The quiz for the students:

1. Ask numbers from the user and assign the numbers to the ArrayList. If the user types 0, stop asking.
2. Print the whole ArrayList.
3. Print the average of the values of the ArrayList.

I can create a test case, where I check if the program is totally correct, but can I create test cases, where I could test if the part 1 is ok, and test case that checks, if the part 2 is ok and so on.

I think I could do this somehow if we would use Python: https://coderunner.org.nz/mod/forum/discuss.php?d=321&parent=1291&lang=de

Also Test case field in Java_program seems to be just a string, can’t write code there like in the Java Method Question Type. Am I missing something here? 

BR
Tommi
In reply to Tommi Saksa

Re: Java_Program Test Cases

by Richard Lobb -

The question type template defines the code that gets run. If you look at the template for your Java program question (click Customise) you'll see it's just 

{{ STUDENT_ANSWER }}

This means that the program that gets run is just whatever the student submitted. The test case code is ignored for this question type. In Python the question author can usefully add extra code to be run but that's not practicable with Java. With that question type all you can do is provide the standard input for each test and see if the output is what you wanted.

If you wish to give partial marks for partially correct output you can turn off all or nothing grading and use a regular expression grader (with the regular expression in the 'expected' field) to match a selected part of the output. A sequence of three such test cases would theoretically let you test how far the program got.

Frankly, though, I don't really recommend that approach. The regular expressions are likely to confuse the students. [And possibly the author too ... please note that the regular expression you provide is searched for in the output and isn't required to match the entire output unless you include the special characters to match at the start and end of the text.]

It's generally easier to set a sequence of questions that get the student to write a series of programs that build functionality one step at a time.

Alternatively use a Java method or Java class question type, which lets you test functionality in a finer-grained way. The test case code is not ignored for such questions - check out the templates to see how they work.