import static java.lang.System.out;
public class Times2 {
public static void main(final String[] args) {
for (byte i = 1; i < 11; i++) {
out.println(2 + " x " + i + " = " + 2 * i);
}
}
}
public class Times2 {
public static void main(final String[] args) {
for (byte i = 1; i < 11; i++) {
System.out.println(2 + " x " + i + " = " + 2 * i);
}
}
}
Am I ENTERPRISE enough?
Name:
Anonymous2009-04-13 9:51
If you want to make it in an enterprise environment you have to create scalable solutions OP. What happens if you want to change your code to divide instead, you are going to have to go through and change all instances of multiplication with division which in some cases may become impractical. Me being a nice person, have created you an examplar from which you will be able to observe techniques for creating a scalable solution. NB: If the programmer now in hindsight wishes to change the operation to division say, all he needs to do is change a string constant at the start to a division sign. Likewise for arbitrary complex equations. You may wish to try, private static final String OPERATOR = "* -14 /";
private static final String OPERATORPRINT = OPERATOR;
private static final String OPERAND = "java.lang.Math.PI";
import java.io.*;
import java.lang.reflect.*;
public class TimesTwo extends ClassLoader {
private static final String DYNAMICFILE = "TimesTwoDynamic.java";
private static final String DYNAMICMETHOD = "printArithmetic";
private static final String OPERATOR = "*";
private static final String OPERATORPRINT = "x";
private static final String OPERAND = "2";
private static final int START = 0;
private static final int STOP = 10;
private static final int INCREMENT = 1;
public static void main(String[] args) throws Exception {
PrintWriter pw = new PrintWriter(new FileWriter(DYNAMICFILE));
String noExtName = DYNAMICFILE.substring(0, DYNAMICFILE.lastIndexOf("."));
writeArithmeticCode(pw, noExtName, DYNAMICMETHOD, OPERATOR, OPERAND, OPERATORPRINT, START, STOP, INCREMENT);
compileClass(DYNAMICFILE);
loadAndExecuteMethod(noExtName, DYNAMICMETHOD);
}
public static void compileClass(String sourceFile) throws Exception {
Runtime.getRuntime().exec("javac "+sourceFile).waitFor();
}
public static void writeArithmeticCode(PrintWriter pw, String className, String methodName, String operator,
String operand, String printableOperator, int min, int max, int step) throws IOException {
pw.write("public class "+className+" {\n" +
"\tpublic static void "+methodName+"() {\n");
for(int i=min; i<=max; i+=step)
pw.write("\t\tSystem.out.println("+operand+" + \" "+printableOperator+" \" + "+i+" + \" = \" + ("+operand+" "+operator+" "+i+"));\n");
pw.write("\t}\n");
pw.write("}");
pw.close();
}
public static void loadAndExecuteMethod(String className, String methodName) throws Exception {
Class c = (new TimesTwo()).loadClass(className, true);
Method m = c.getMethod(methodName, (Class[])null);
m.invoke(null, (Object[])null);
}
public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class newClass = null;
File classFile = new File(name.replace('.','/')+".class");
byte[] bytes = new byte[(int)classFile.length()];
try {
FileInputStream fis = new FileInputStream(classFile);
fis.read(bytes);
fis.close();
newClass = defineClass(name, bytes, 0, bytes.length);
}
catch(IOException e) {
newClass = findSystemClass(name);
}
if(resolve) resolveClass(newClass);
return newClass;
}
}