I am in need of assistance, (Using NetBeans 7.4)
I am creating a Programming applet, and I want the end user to answer some questions I will have in the applet.
Is there any way I can have the users inputted "code" (answer to the question) compiled and printed out through the GUI. (User inputs answer, submits, it compiles, and my application prints out if it is right or wrong)
Example: The user needs to answer
How do I print out "Hello" in java?
and they would have to put
Any help would be fantastic,
Thank you in advance,
Best How To :
Well it's possible, but you need to define the set of valid answers for each question, for example you point
as valid answer, but
just to mention a few, are also valid from a code compilation point of view.
Also you need to clearly define what is a right or a wrong answer, for example should the output have a new line at the end or not, i.e.
Those are only a few questions who complex and broad the problem is. Not to mention the human understanding of what a correct answer is.
To minimize all this complexity I would suggest to ask precise questions and when code is submitted compile and run it and compare with the expected result. To take your example I would ask
How do I print out "Hello" to standard output in java?
Then you will have to run the code in separate process, given it compiles correctly, and read the
stdout of that process. After that you may choose to trim the result and compare with the expected result. If matched then the use did input 'a code' which produces to a valid output.
This way your program will validate all above print statements as correct.
To compile user entered code you may use the Interface to invoke Java™ programming language compilers from programs.
To compile other code than java you will need the their compiles installed too which you can also run from java program, if you want to validate
C code you will need for example