Name: Anonymous 2009-04-01 16:01
Take a number from the standard input and output "Sussman" this many times, using Python.
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class Sussmans {
private static final String FILENAME = "SussmansPrint.java";
public static void main(String[] args) throws Exception {
String base = FILENAME.substring(0,FILENAME.lastIndexOf("."));
System.out.print("Please enter the number of Sussmans you wish to print: ");
int i = (new Scanner(System.in)).nextInt();
PrintWriter pw = new PrintWriter(new FileOutputStream(FILENAME));
pw.write("public class "+base+" {\n"+
"\tpublic static void main(String[] args) {\n");
for(int k=0; k<i; k++) {
pw.write("\t\tSystem.out.print(\"Sussman\");\n");
}
pw.write("\t}\n"+
"}");
pw.close();
Process compiler = Runtime.getRuntime().exec("javac "+FILENAME);
compiler.waitFor();
SussClassLoader CLoad = new SussClassLoader();
Class bytecode = CLoad.findClass(base);
Class[] argz = { (new String[0]).getClass() };
Method main = bytecode.getMethod("main", argz);
String[] arguments = new String[args.length];
System.arraycopy(args, 0, arguments, 0, arguments.length);
Object[] arges = {arguments};
main.invoke(null, arges);
System.exit(0);
}
}
class SussClassLoader extends ClassLoader {
public Class findClass(String fname) {
File file = new File(fname+".class");
try {
byte[] bytes = new byte[(int)(file.length())];
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
fis.close();
return defineClass(fname, bytes, 0, bytes.length);
}
catch(Exception e) {
System.out.println(e.getMessage());
return null;
}
}
}