gudii9
asked on
assertion example
What will happen if you attempt to compile following code with JDK1.4 with assertions enabled
public class Bbridge{
int iRunningTotal=0;
public static void main(String argv[]){
Bbridge bb = new Bbridge();
bb.go(argv[0]);
}
public void go(String s){
int i = Integer.parseInt(s);
setRunningTotal(i);
assert (iRunningTotal > 0): getRunningTotal();
}
public String getRunningTotal(){
return "Value of iRunningTotal "+iRunningTotal;
}
public int setRunningTotal(int i){
iRunningTotal+=i;
return iRunningTotal;
}
}
1) Compile time error, getRunningTotal does not return a boolean
2) Compile time error malformed assert statement
3) Compilation and no output given a command parameter of 1
4) Compilation and assert error given a command parameter of 0
I was trying above example from link
http://www.jchq.net/certkey/0205certkey.htm
did not understand how to run and get output while running in eclipse ide.How to enable assertion and run this example.
I was trying to understand the output.
Any links, ideas, resources,sample code highly appreciated. thanks in advance.
public class Bbridge{
int iRunningTotal=0;
public static void main(String argv[]){
Bbridge bb = new Bbridge();
bb.go(argv[0]);
}
public void go(String s){
int i = Integer.parseInt(s);
setRunningTotal(i);
assert (iRunningTotal > 0): getRunningTotal();
}
public String getRunningTotal(){
return "Value of iRunningTotal "+iRunningTotal;
}
public int setRunningTotal(int i){
iRunningTotal+=i;
return iRunningTotal;
}
}
1) Compile time error, getRunningTotal does not return a boolean
2) Compile time error malformed assert statement
3) Compilation and no output given a command parameter of 1
4) Compilation and assert error given a command parameter of 0
I was trying above example from link
http://www.jchq.net/certkey/0205certkey.htm
did not understand how to run and get output while running in eclipse ide.How to enable assertion and run this example.
I was trying to understand the output.
Any links, ideas, resources,sample code highly appreciated. thanks in advance.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
the program is expecting input on the command line
ASKER
i am running through eclipse ide. How can i pass command line input through eclipse. please advise
specify it in the run configuration
ASKER
ok
ASKER
>>>Under the Default VM Arguments: field type in -ea . This is a switch to enable assertions when Eclipse compiles you Java code.
ran the program getting like
>>>Exception in thread "main" java.lang.ArrayIndexOutOfB
at com.vaannila.student.Bbrid
please advise