Simulated Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

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 benefit copy the program to a file and run it. 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.util.Scanner;

class NumberTester
{
  public static void main (String[] args) 
  {
    Scanner scan = new Scanner( System.in );
    int num;

    System.out.println("Enter an integer:");
    num = scan.nextInt();
    
    if ( num < 0 )                
      System.out.println("The number " +
           num + " is negative"); 
    else
      System.out.println("The number " + 
           num + " is zero or 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 7:

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