Practice

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:

if ( num < 0 )
  System.out.println("The number " + num + " is negative.");         // true-branch
else
{
  System.out.println("The number " + num + " is zero or positive");  // false-branch
  System.out.print  ("Positive numbers are greater ");               // false-branch   
  System.out.println("or equal to zero. ");                          // false-branch
}                      
System.out.println("Good-bye for now");                      // always executed

The true branch has one statement. The false branch has one statement, a block containing three statements.

Practice

[Movie Tickets]

At a movie theater box office a person less than age 13 is charged the "child rate". Otherwise a person is charged the "adult rate." Here is a partially complete program that does this:

import java.util.Scanner;
class BoxOffice
{
  public static void main (String[] args) 
  {
    Scanner scan = new Scanner( System.in );
    int age;
 
    System.out.println("Enter your age:");
    age = scan.nextInt();

    if (  )
    {
      System.out.println("Child rate.");   
    } 
    else
    {
      System.out.println("Adult rate.");   
    }
    System.out.println("Enjoy the show.");    // always executed
  }
}

In this program, the true branch and the false branch are both blocks. Each block contains only one statement, but this is OK. Often programmers do this for clarity.

QUESTION 11:

Fill in the blank so that a person under the age of 13 is charged the child rate.