Autograders in Java rely on reading methods and output from the different class files that students use to complete each exercise. In general, to create a Java autograder, exercises need to have the solution code already written. There are multiple ways to write autograders, but the simplest example relies on students creating a method or function. The method is then tested in the main method in the same class file.
Creating an Autograder from the Create Page
Click Create New Assignment or choose an assignment that you've already created. (Autograders can only be added after an assignment is created - see Creating Autograders to Check Student Code for more information.)
To the far right of the assignment name, click ... > Edit
In the edit window, scroll down and click Custom Autograder Edit in the lower left corner.
Click the blue Add Custom Autograder button.
Write your code in the Grader.java file to build your Autograder, and make sure to save your changes as you go.
Creating an Autograder from the Assignments Page
Create a New Assignment or choose an assignment that you've already created. (Autograders can only be added after an assignment is created - see Creating Autograders to Check Student Code for more information.)
To the right of the assignment name, click [Edit].
In the edit window, scroll down and click Custom Autograder Edit in the lower left corner.
Click the blue Add Custom Autograder button. Write your code in the Grader.java file to build your Autograder, and make sure to save your changes as you go.
Building Your Autograder:
The Autograder.java file contains useful methods that you can use to help write autograders and the other files are used to help gather input information and objects from the code editor. The only file that you will write in to make the autograder is the Grader.java file.
Create a new file that will host the exercise solution. It's useful to name the file similarly to the name in the student code. For example, if file name for students is
ExtractDigits.java
, the solution file will beExtractDigitsSol.java.
Next, write an autograder test to see if the student has completed the assignment correctly.
There are two ways to write an autograder test -
grader.addTest
andgrader.assertEqual
.The
addTest
method takes a boolean value and presents the student with a pass/fail depending on the value of the boolean:grader.addTest("This is the test name", true, "This is the student output", "This is the solution output", "This is the message students see!");
The
assertEqual
method compares two values and determines if they are equivalent:grader.assertEqual("This is the test name", "This is the student output", "This is the solution output", "Message if passed!", "Message if failed");
Getting Student Output:
To get student output from student programs, create an object using the student class, and then use the methods associated with the student class.
For example you can use:
ExtractDigits student = new ExtractDigits();
student.extractDigits(12345);
and test that versus the solution output:
ExtractDigitsSol solution = new ExtractDigitsSol();
solution.extractDigits(12345);
To access output to the console, use grader.getOutput
. grader.getOutput
takes the class name as its parameter, and returns a String of all the outputs for a given class.
grader.getOutput
should be called after the methods that print to the console are executed. If you want to run multiple tests to test output, call grader.clearOutput();
to removes all previous output to the console. This should be called before the new print calls to the console.
Now that you've gotten the student and solution output from the grader, run a test to see if the values are equivalent:
grader.assertEqual("Extract Digit has the correct output?", studentOutput, solutionOutput, "Great!", "Try again!");
Reading Student Code
With Java, it's possible to extract the code that students have written in their program. For example, an autograder can be created to check if a student has created a variable called myName
, or to make sure that they've used the keyword private
to declare a variable.
In order to read student input, include the follow code in your autograder:
import java.io.File;
ArrayList<String> userLines = new ArrayList<String>();
try {
Scanner input = new Scanner(new File("YourFileName.java"));
while (input.hasNext()){
userLines.add(input.nextLine());
}
}
catch (Exception e){
grader.addTest("File Not Found", false, "", "",
"Looks like we have a problem on our end");
}
To read student input, the parameter YourFileName.java
should be replaced with the name of the file students are working on.
Limitations of Reading Student Code
It's important to note that this method has limitations. For example, this way of reading student input does not exclude comments. If the student wrote a comment with the phrased, the autograder is checking for, the test case will return true
.
Creating Java Autograder Demo
Still have questions? Contact our team at hello@codehs.com to learn more!