Developers' Forum

Methods, returning Lists in Code Runner: how the test line should look like?

Methods, returning Lists in Code Runner: how the test line should look like?

by Alina Torbunova -
Number of replies: 6

Hello!

I ran into some issues with the acceptable code for test lines in Code Runner for Java.

The students are supposed to write the code inside the following method: 

public List wordsWithoutList(String[] words, int len) {

}

I have been trying different ways for the tester, but it is not being accepted.

Here is what I tried last:

String [] num = {"a", "bb", "b", "ccc"};

System.out.println(java.util.ArrayList.toArray.toString(wordsWithoutList(num, 1)));


And for example expected output should be:

["bb", "ccc"]


How should the code for text case look like, so that the Lists are accepted?

Kind Regards,

Alina

In reply to Alina Torbunova

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Richard Lobb -

A test of the form

String [] num = {"a", "bb", "b", "ccc"};
System.out.println(wordsWithoutList(num, 1).toString());

should generate output of the form [bb, ccc]. Will that suffice?

Are you aware of the Template debugging checkbox? That will let you see exactly what program is being generated by the template in conjunction with the student answer and your test code. You should be able to run the generated code in your preferred Java IDE. You can also customise the template if you wish by clicking the customise checkbox.

In reply to Richard Lobb

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Alina Torbunova -

Thank you very much! I have tried debugging and customisation, very helpful features. But there is still the problem that CodeRunner does not accept the code. Due to the error message I receive, it does not accept the type of parameter being which is stated as a type of return for method. The method should return type List. I added a line in the customisation of the test methods to import java.utli.* , but it did not help... In the line for test cases I wrote exactly as you suggested.

Regards,

Alina

In reply to Alina Torbunova

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Anton Dil -
We probably need to see the actual error message and what your template looks like to tell what's going on.
In reply to Anton Dil

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Alina Torbunova -
Hi,
thank you for the reply, I am attaching the screenshot of what I get when I run my solution in CodeRunner. Since the screenshot was too big to upload, I splitted it into two parts.
Regards,
Alina
In reply to Alina Torbunova

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Anton Dil -
The problem is that you are using raw types. Did you mean to do that? If so, you'd have to configure the system to ignore that. Otherwise you should use the more up to date approach, with a generic type parameter.

One way to allow the raw type is to suppress the warnings using an annotation, e.g.

 @SuppressWarnings("unchecked")
public List wordsWithoutList(String[] words, int len)
{
    ArrayList li = new ArrayList();
   
    for(int i = 0; i < words.length; i++)
      if(words[i].length() != len)
        li.add(words[i]);
       
    return li;
}

If you want to allow studdents to use raw types then, assuming you are using the java_method type of question, you could add the annotation to the template above the {{STUDENT_ANSWER}}, so the template says

//other stuff
@SuppressWarnings("unchecked")
{{STUDENT_ANSWER}}
//etc
In reply to Anton Dil

Re: Methods, returning Lists in Code Runner: how the test line should look like?

by Alina Torbunova -
Thank you very much, it works now! I should have uploaded the tasks as they were designed already.