Wednesday, May 24, 2006

Programming Problem

We have a java program(Read only) and lets name it A.java, and this program is compiler error free. Now lets make certain changes to it(delete/comment) some logic and print statements and store it as B.java.

Since B.java is a new program there are possibilities for compiler errors in it. Now without compiling B.java we should figure out if there are any syntax errors in it.

Clue: Your program can take only B.java and find the solution (or) both A.java and B.java do some comparison (or) other logic and should print there is no error (or) error in the new program.

Ans:

class A {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println(" World!");
}
}

class B {
public static void main(String[] args) {
int i='a'; // condition 1
int i="a"; // condition 2
// System.out.println("Hello");
System.out.println(" World!");
}
}

public class CTest {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec("javac A2.java");
p.waitFor();
System.out.println(p.exitValue() == 0 ? "no error" : "error");
}
}

OUTPUT:

For condition 1 in B.java:
no error: since int accepts char value


For condition 2 in B.java:
error: since int doesn't accepts String

There is another tool that checks syntax but does not generate byte code. It is JavaDoc

0 Comments:

Post a Comment

<< Home