Adding in a Zero

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:

The answer is given below:

Adding in a Zero

Here is the complete program.

import java.util.Scanner;
class TaxProgram
{
  public static void main (String[] args)
  {
    final double taxRate = 0.05;
    Scanner scan = new Scanner( System.in );
    double price;
    double tax ;

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

    if ( price >= 100.0  )
      tax = price * taxRate;   
    else
      tax = 0.0;

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

Is the logic of the program is correct? The expression (price+tax) in the last statement sometimes adds zero to price. This is fine. Adding zero does not change the result.

QUESTION 15:

The user buys a shirt for $100 What will be printed on the monitor?