A good answer might be:

Enter an integer:
-5
The number -5 is negative
Good-bye for now

Only the true-branch was executed because the answer to the question num < 0 was true.


Simulated Program

Here is a simulation of this program (using JavaScript and your browser). Of course, for maximum benifit you should "copy-paste-save-and-run" the real program. But, play with the following if you want. Perform the steps 1, 2, and 3 under the simulated monitor as many times as you want. (Note: if this does not work, your browser does not have JavaScript enabled. Skip this and go on to the next page.)


import java.io.*;
class NumberTester
{
  public static void main (String[] args) 
      throws IOException
  {
     BufferedReader stdin = 
        new BufferedReader ( 
        new InputStreamReader( System.in ) );

    String inData;
    int    num;

    System.out.println("Enter an integer:");
    inData = stdin.readLine();
    num    = Integer.parseInt( inData );      

    if ( num < 0 )
      System.out.println("The number " + num + 
          " is negative"); 
    else
      System.out.println("The number " + num + 
          " is positive");  

    System.out.println("Good-bye for now");  
  }
}
        Simulated Monitor





2. Click in the "monitor" window after the last line and enter a number


This is just a simulation (using JavaScript), so it is not exactly like a compiling and running a real Java program. Don't take it too seriously. Please do the real thing if you can.

QUESTION 5:

Try the program (or look at the flowchart) with the value 0 (zero). What is the output?