Opposites

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 (  cash < price )
    {
      System.out.println("You can't buy the sweater" );
      System.out.println("You need $" + 
        (price-cash)/100 + "." + (price-cash)%100 + " more." );
    }
    else
      System.out.println("You can buy the sweater" );

You (hopefully) picked a relational expression that was true when the user COULD NOT pay for the sweater.

Opposites

In the first program, the boolean expression is:

    if (  cash >= price )

because we want the true branch to execute when the user has enough money. In the second program, the boolean expression is:

    if (  cash < price )

because we want the false branch to execute when the user has enough money. The operator in the first program includes the "equals". The operator in the second program does not.

In a sense, >= and < are opposite of each other. Sometimes it is convenient to rearrange the contents of the true and the false branch. But be sure to change the boolean expression correctly!

QUESTION 9:

What is the "opposite" of   <= ?