Decisions

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:

Two.

Decisions

The windshield wiper decision is a two-way decision (sometimes called a binary decision). The decision seems small, but in programming, complicated decisions are made of many small decisions. Here is a program that implements the wiper decision:

import java.util.Scanner;
class RainTester
{
  public static void main (String[] args) 
  {
    Scanner scan = new Scanner( System.in );
    String answer;

    System.out.print("Is it raining? (Y or N): ");
    answer =  scan.nextLine();
    
    if ( answer.equals("Y") )                // is answer exactly "Y" ?              
      System.out.println("Wipers On");              // true branch
    else
      System.out.println("Wipers Off");             // false branch   
  }
}

The user is prompted to answer with a single character, Y or N :

    System.out.print("Is it raining? (Y or N): ");

The Scanner reads in whatever the user enters (even if the user enters more than one character):

    answer =  scan.nextLine();

The if statement tests if the user entered exactly the character Y (and nothing else):

    if ( answer.equals("Y") )                // is answer exactly "Y" ?              

If so, then the statement labeled "true branch" is executed. Otherwise, the statement labeled "false branch" is exectued.

The "true branch" is separated from the "false branch" by the reserved word else.

QUESTION 3:

What happens if the user enters anything other than exactly the character Y ?