Using Boolean Expressions

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:

Expression ValueExpression Value
25 == 25 true 25 != 25 false
25 <= 25 true 25 > 25 false
25 >= 25 true 25 = 25 illegal
-5 < 7 true -305 <= 97 true

Using Boolean Expressions

In an if statement, the true or false of a boolean expression picks whether the true branch or the false branch is executed. Look at another story problem:

A store wants a program that calculates the tax on an item of clothing. An item that costs $100 or more has a 5% tax. An item that costs less than $100 is tax free. Write a program that asks for a price, then calculates and prints the tax, then prints the total cost.

Here is the program, not quite finished:


 
class TaxProgram
{
  public static void main (String[] args) 
  {
     
     
    double price;
    double tax ;

    System.out.println("Enter the price:");
          

    if (   )

         

    else
       

    System.out.println("Item cost: " + price + " Tax: " + tax + " Total: " + (price+tax) );    
  }
}

Here are some program fragments to use in completing the program. Use your mouse to copy-and-paste them into the program.

tax = price * taxRate;          Scanner scan = new Scanner( System.in );
price = scan.nextDouble();      final double taxRate = 0.05;
price >= 100.0                  import java.util.Scanner;
tax = 0.0;

(Of course, it would be nice to copy the program to a file, enter your corrections and run the program.)

QUESTION 14:

Complete the program by filling in the blanks.