Write Java Program to grade Scala Homeworks
I am a TA for a programming class. There is one assignment in which the students have to write Scala. I am not proficient enough in Scala to read it quickly to verify that the program works or capable of quickly writing a script in Scala to run test inputs.
However, I am very capable in Java. I need some advice on a simple way to grade Scala assignments using my knowledge of Java. Is there a way to load in a Scala file into Java so开发者_JAVA技巧 I could have some simple Java methods to run test inputs for their programs? I am aware of the fact that they both compile to Java byte code, so I figure this should be possible.
Scala generates class files. Scala class files can be run with java
, only requiring the scala-library.jar
to be on the class path. The entry point on Scala programs appears to Java as a static main
method on a class, just like in Java. The difference is that, in a Scala program, that main
method is a method declared on an object
. For example:
Java:
public class Test {
public static void main(String[] args) {
}
}
Scala:
object Test {
def main(args: Array[String]) {
// or:
// def main(args: Array[String]): Unit = {
}
}
The idea of testing by giving unit tests is interesting, but it will probably force non-idiomatic Scala code. And, in some rare cases, might even prevent the solution to be written entirely in Scala.
So I think it is better to just specify command line arguments, input (maybe stdin), and output (stdout). You can easily run it with either scala Test parms
, or java -cp /path/to/scala-library.jar Test parms
.
Testing input on individual functions might be a lot harder, though, as they may require Scala classes as input, and some of them can be a bit tough to initialize from Java. If you go that route, you'll probably have to ask many more questions to address specific needs.
One alternative, perhaps, is using Scala expressions from the command line. For example, say you have this code:
object Sum {
def apply(xs: Seq[Int]) = xs reduceLeft (_ + _)
}
It could be tested as easily as this:
dcs@ayanami:~/tmp$ scalac Sum.scala
dcs@ayanami:~/tmp$ scala -cp . -e 'println(Sum.apply(Seq(1, 2, 3)))'
6
To do the same from Java, you'd write code like this:
import scala.collection.Seq$;
public class JavaTest {
static public void main(String[] args) {
System.out.println(Sum.apply(Seq$.MODULE$.apply(scala.Predef.wrapIntArray(new int[] {1, 2, 3}))));
}
}
When you put the .class files generated by the student's code into your classpath, you can simply call the methods which your students developed. With a good Java IDE, you will even have code completion.
Rephrase the question: Assume you have a Java library that you need to test. But you only have the class files, not the source code. How do you do it? - Now, it's exactly the same case with Scala. In some cases, you will need to access strange static variables (such as $MODULE
), but that should not be a hindrance. tobym has pointed you in the right direction with his answer.
But seriously, what can be the didactic value for the students? You will only be able to tell them whether or not their programs do the right thing, but you cannot point out to them exactly what mistake they made and how to correct it. They will already know by themselves whether or not their programs are correct. When they made errors, just telling them that they made something wrong won't help them at all. You need to show them exactly where the mistake was made in the code, and how to fix it. This is the only way you can help them learn.
If it is only one assignment and not more, maybe you can find a better way. Maybe you can invite another student who is proficient in Scala to help you out with this. Or maybe you can show some of the erroneous programs to the whole class and initiate a discussion amongst the students, in which they will find out themselves what went wrong and how to correct it. Talking about code in this way can help them a lot, and, if done right, can be a valuable lesson. Because this reflects what they will do later in business life. There won't be a prof telling them how to correct their errors. Instead, they will have to figure it out together with their coworkers. So maybe you can turn this lack of knowledge on your part into an opportunity for your students.
You can compile Scala into a .class file (e.g. "scalac ./foo.scala") and run methods from your Java grading program.
This might be useful reference: How do you call Scala objects from Java?
Well, you could write unit tests (with JUnit, for instance) before the assignment and have the students write the programs to conform to the tests. Or you could decompile scala to java (with JD-gui, for instance).
But to be fair, if the students are only going to use scala for this one specific assignment, chances are that they are mostly going to translate directly from java to scala, intead of writing idiomatic scala. In that case I'm sure you will be able to understand scala code very easily as it will look almost exactly like java...
You can run
scalac SomeProgram.scala
scala SomeProgram input1
a lot of time during the time it would take to write some java that triggers scala compile and running of the bytecode generated
精彩评论