Java questions

Java questions

by Anton Dil -
Number of replies: 9

Thank you for coderunner. It looks like it has great potential!

I've not experimented much, but my impression is that, for a method question, showing the test code would be misleading, since (although the method can be specified as a static or instance method in the submitted answer) if it were an instance method the test code would have to show an object of a class being created and tested (and I'm not sure how that would work).

The idea of output is also somewhat cloudy. I would normally consider the output of a method that returns a value to be its returned value, not an output printed to a screen, using println (which also returns void). It looks like I need to understand the templates better to specify the test differently.

I presume that methods returning void are not supported?

And I presume that details such as parameter names cannot be checked. (It would be nice, because then we can see if students understand words like 'parameter name'.)

Thanks...

In reply to Anton Dil

Re: Java questions

by Richard Lobb -

Hi Anton

Thanks for the interesting post.

Java is the only language with built-in question types that I haven't personally used in teaching, because we changed from Java to Python for first-year teaching about the time I started developing CodeRunner. The three built-in question types (write-a-method, write-a-class and write-a-method) were just ones that I thought might be useful. Had I tried using them in teaching I would have undoubtedly come up with extra and/or different ones.

Now that I look more closely, I think your comment that the 'write-a-method' test could be misleading is valid. The template for that question type is

public class Main {

    {{ STUDENT_ANSWER }}

    public static void main(String[] args) {
        Main main = new Main();
        main.runTests();
    }

    public void runTests() {
        {{ TEST.testcode }};
    }
}

but if the student doesn't understand the context, the test does indeed look anomalous, as it appears to be calling a global function, something that's not possible in Java.

You could instead modify the runTests method in the template to:

public class Main {

    {{ STUDENT_ANSWER }}

    public static void main(String[] args) {
        Main main = new Main();
        main.runTests();
    }

    public void runTests() {
        System.out.println({{ TEST.testcode }});
    }
}
With that change, and after removing the call to System.out.println from all tests, the test result panel looks like the following:

Image of result table with modified template

Does that suit you better? 

I'll consider adding that into the distribution as a new alternative Java write-a-method question type. However, although it looks more logical in the above test, it is a bit more restrictive in that the test case now must be a single expression rather than a sequence of one or more Java statements. With the existing question type, you could instead write the tests in multiple lines, e.g.

int[] data = {10, 17, -3};
int total = sumSquares(data);
System.out.println(total);

which might perhaps be simpler for learners, with the caveat that they still need to understand that the test code is wrapped as a method in the same class as their method declaration. [Edit: I belatedly realise that writing the tests like that with the built-in Java method question type doesn't quite work, because its combinator template combines all the tests into a single method. That gives syntax errors due to the variables being redeclared. So to make that form of test work with Java you either have to customise the question and turn off the combinator template or make a new question type like the built-in one with the combinator disabled. Edit to Edit: actually, the combinator template can be easily changed to allow tests like this, just by wrapping each test case in braces, so it runs in a separate independent block. ]

A further restriction on that new format of question is that you can't test void functions using it, whereas you can test them with the existing write-a-method question type just by dropping the call to System.out.println from each test.

You're right of course that there is an degree of fuzziness in the conversion of a method return value to a string. However, you have to assume some conversion in order to give the student an example of the value that their method should return, as in "Your function should return the value 23". Passing the return value as a parameter to println is the easiest conversion but it's over to the question author to choose whatever conversion suits them. Occasionally we do add extra test cases (usually hidden ones, or at least ones that display only if they fail) that check the type of the value returned by the method/function.

You raise the question of checking parameter names. This is part of the thorny question of how you check style. In Python we use the pylint program as a pre-processor - if a student submission fails pylint checks it's not even tested. However, no program can check if variable names are "good" or not, so ultimately you still have to have humans check code at some stage in a course.

I've added to this site an article that a colleague, Jenny Harlow, and I have written on CodeRunner, to appear in ACM Inroads. See here.  It reports on our experiences teaching with CodeRunner and discusses some of the issues with style checking.

-- Richard

In reply to Richard Lobb

Re: Java questions

by Anton Dil -

Thank you for your detailed reply... That's all very helpful. I've had a go with a different template and I'm sure we can make that work for us, with a little wrap around text to fit the way we teach things.

I'll follow up on that article...

Anton

In reply to Anton Dil

Re: Java questions

by Richard Lobb -

Sounds good.

Just a couple of parting thoughts. Firstly, my comment about not being able to run tests that declared their own variables under a combinator template isn't quite right. You can just edit the combinator template to wrap each test case in braces so that it runs in its own independent block. Secondly, in case it's not obvious, you can use the "Extra template data" field of each test case in various creative ways, e.g. to add extra testing code that's hidden from the students to avoid confusing them.

Enjoy!

Richard

In reply to Richard Lobb

Re: Java questions

by Sayee Krishnan -

We hav pulled new repostiory from Github.Now its working fine

Thank you.

In reply to Richard Lobb

Re: Java questions

by Sayee Krishnan -

I tried this code but i cant able to get the results.i think i need to0 change the template code.Update me with specific template code for java program.Its working fine with Java method and Java Class.

Thank You.

Sayee Krishnan S


import java.util.Scanner;

public class FileReader {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.hasNextLine() ? scanner.nextLine() : "";
        while (line.length() != 0) {
            String[] numbers = line.split(",");
            int total = 0;
            for (int i = 0; i < numbers.length; i++) {
                total += Integer.parseInt(numbers[i]);
            }
            System.out.printf("%d\n", total);
            line = scanner.hasNextLine() ? scanner.nextLine() : "";
        }
    }
}

In reply to Sayee Krishnan

Re: Java questions

by Richard Lobb -

Have you tried checking the "Template debugging" checkbox? That will tell you exactly what code is being run and from there you should be able to find out what's going wrong and, if necessary, edit the template or create your own.

Richard

In reply to Richard Lobb

Re: Java questions

by Sayee Krishnan -

This my template code for above mentioned program.

public class Main {

    {{ STUDENT_ANSWER }}

    public static void main(String[] args) {
        Main main = new Main();
        main.runTests();
    }

    public void runTests() {
        {{ TEST.testcode }};
    }
}

I can't able to attach the image so i copied the error message.


Syntax Error(s)

Main.java:3: error: illegal start of type
    import java.util.Scanner;
    ^
Main.java:3: error: ';' expected
    import java.util.Scanner;
          ^
Main.java:3: error: illegal start of type
    import java.util.Scanner;
               ^
Main.java:3: error: ';' expected
    import java.util.Scanner;
                    ^
Main.java:3: error: <identifier> expected
    import java.util.Scanner;
                            ^
Main.java:27: error: not a statement
        10,20,30
        ^
Main.java:27: error: ';' expected
        10,20,30
          ^
Main.java:30: error: unclosed character literal
shouldn't get here;
       ^
Main.java:30: error: not a statement
shouldn't get here;
^
9 errors


In reply to Sayee Krishnan

Re: Java questions

by Richard Lobb -

If you want to set "Write a program" questions in Java, you should use the java_program question type. Your template appears to be a per-test template for a "Write a method" question.

Richard

In reply to Richard Lobb

Re: Java questions

by Sayee Krishnan -

i have set as java_program only. Update me with a template for java_program question type.