-6

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 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.

user670080
  • 15
  • 1
  • 21
    Honestly, ask the guy with the highest grade in the class. He'll tell you how he tested his program, which will probably help a lot! – corsiKa Mar 21 '11 at 20:14
  • you could try decompiling the scala programs to java with programs like [jd-gui](http://java.decompiler.free.fr/?q=jdgui) – Paolo Falabella Mar 21 '11 at 20:20
  • @glowcoder hahahahaha I'd like to give you 10 upvotes for that!!! – Sean Patrick Floyd Mar 21 '11 at 20:27
  • 15
    so you are asking how to teach something you do not know? – Kim Stebel Mar 21 '11 at 20:28
  • 12
    @user670080 if you are a TA in a class where Scala is taught, perhaps you should just go ahead and learn Scala yourself? – Sean Patrick Floyd Mar 21 '11 at 20:30
  • 10
    @Sean +1 I'd be upset if I was taking a class involving Scala and the TA didn't know it (or want to learn it). – dbyrne Mar 21 '11 at 20:34
  • 4
    @dbyrne *or want to learn it* That's the key phrase. Being taught to learn by someone who isn't willing to learn. What an oxymoron. – Sean Patrick Floyd Mar 21 '11 at 20:41
  • I don't teach it, I only grade the programming assignments. I write all the scripts that do the automatic grading. So you see, I could write a script in Scala, but for me, it would be faster to write one in Java. – user670080 Mar 21 '11 at 21:37
  • 1
    @user670080: what a shame! How dare you give grades to someone about something you don't understand? – Bruno Reis Mar 21 '11 at 23:21
  • 2
    "automatic grading", tell everyone which school is that, so we can avoid it, and its students as well... – Bruno Reis Mar 21 '11 at 23:22
  • 1
    I know computers are here to ease the burden on our lives, but this is just being lazy. The whole idea of 'automated grading' is lazy. You should be ashamed to be part of such a thing. So someone who obviously copied their assignment from another classmate or the internet would just get 100% because the automated grader doesn't have any morality. – Jasarien Mar 22 '11 at 21:03

5 Answers5

7

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}))));
    }
}
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
6

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.

Madoc
  • 5,841
  • 4
  • 25
  • 38
3

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?

Community
  • 1
  • 1
tobym
  • 1,354
  • 1
  • 10
  • 11
3

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...

Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
0

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

svrist
  • 7,042
  • 7
  • 44
  • 67
  • the students wrote scala programs with lots of different functions that would need to be tested separately, so running through inputs in this way would not work for my purposes. any other ideas? Do you know how to go about compiling scala within a java program? – user670080 Mar 21 '11 at 20:12
  • would you then use reflection for finding the functions or do you have some set package and interface they are using? – svrist Mar 21 '11 at 20:14
  • http://scala.sygneca.com/faqs/scala-to-java-mapping tells abit about how to see the java of a given scala class. I would use scalac for compiling – svrist Mar 21 '11 at 20:20