Link to home
Create AccountLog in
Avatar of gudii9
gudii9Flag for United States of America

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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gudii9

ASKER

i eanbled

>>>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.ArrayIndexOutOfBoundsException: 0
      at com.vaannila.student.Bbridge.main(Bbridge.java:6)

please advise
the program is expecting input on the command line
Avatar of gudii9

ASKER

i am running through eclipse ide. How can i pass command line input through eclipse. please advise
specify it in the run configuration
Avatar of gudii9

ASKER

ok