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);
}
}
}
This is a horrible excuse for an enterprise application. The fact you have to enter "2 *|x i" twice is creating duplicate code that makes your program difficult to maintain. I recommend using reflection to build a method at runtime so you only need to specify the operator and operand once.
Paradoxically, writing good ENTERPRISE parody code does require that you have more than a passing acquaintance with Java. Why don't you try again at the end of your first semester?
>>12
And you duplicated your arithmetic expression in each of them. lrn2read.
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;
}
}
Name:
Anonymous2009-04-13 9:58
>>14
Doesn't look very scalable to me. You should create an Operator class so that it is possible to separate the external presentation of an operator from its semantics, and then possibly an OperatorFactory to ease Operator creation.
Name:
Anonymous2009-04-13 10:02
>>15
Excellent observation, sadly I don't wish to waste time writing code where the idioms used are already committed to memory. I'll leave that as an excercise to the OP.
I haven't written serious java code in about 4 years so i have no idea what the fuck they've done to the language since. Hell, I remember when Swing was the new hotness.
Name:
Anonymous2009-04-13 21:14
>>19
Back, villian, or I will break thy pate across!
>>30
Well, C support adding functions at runtime anyway because of how much control you get over memory. If you ignore small implementation defined things such as the size of an integer in the standard, you can pretty well write a function into memory yourself at runtime and have it port to any major platform. Your argument fails anyway, because Java actually can add methods at runtime. You don't actually have to use an external compiler, you could write your own into the program so that it can translate your code into bytecode on the fly, and then use a class loader. I really don't see where your argument is coming from.
>>37
Why wouldn't he? I though he lived and breathed for that kind of stuff. I couldn't fathom how much of a pain in the ass it must be to have this site's nose above water to keep it the whole thing from drowning.