Developers' Forum

Code Runner for quizes in Java: methods, that return array

Code Runner for quizes in Java: methods, that return array

by Alina Torbunova -
Number of replies: 2

Hello!

I am making quizes in Moodle with use of Code Runner for Java. I have a question regarding how should the line in a test case for checking an output of the method, that returns an array, should be filled in? For example the task is following: 

Given an array of ints, swap the first and last elements in the array. Return the modified array. The array length will be at least 1.


swapEnds([1, 2, 3, 4]) → [4, 2, 3, 1]
swapEnds([1, 2, 3]) → [3, 2, 1]
swapEnds([8, 6, 7, 9, 5]) → [5, 6, 7, 9, 8]

In answer box preload I have following: 
public int[] swapEnds(int[] nums) {
  }

And in the line for text case I write following:

int [] nums=new int [] {1, 2, 3, 4};

System.out.println(Arrays.toString(swapEnds(nums)));


In this case I get a response, complaining regarding Arrays.

I tried to  import java.util.Arrays , but it is not working either. 

Howshould it be done in a wright way?


In reply to Alina Torbunova

Re: Code Runner for quizes in Java: methods, that return array

by Richard Lobb -

Hi Alina

There's no right way, but here are some options:

  1. Give the Arrays class its full path name, e.g.,
    System.out.println(java.util.Arrays.toString(swapEnds(nums)));
  2. Write the test without using the Arrays.toString method, e.g. printing each element in a loop using System.print.
  3. Customise the question template by clicking the Customise checkbox. Then insert the following line at the start of the template:
    import java.util.*;
  4. Create your own derivative of the Java Method question type with that extra line inserted at the start. See the documentation on defining new question types. Then you will be able to use the shorter version (Arrays.toString) in all your questions.
Richard