Completion requirements
Read this chapter, which reviews how computers make decisions using if statements. As you read this tutorial, you will understand that sometimes it is important to evaluate the value of an expression and perform a task if the value comes out to be true and another task if it is false. In particular, try the simulated program under the heading "Simulated Program" to see how a different response is presented to the user based on if a number is positive or negative.
Pay special attention to the "More Than One Statement per Branch" header to learn how the 'else' statement is used when there is more than one choice.
15. Adding in a Zero
Answer:
The answer is given below:
Adding in a Zero
Here is the complete program.
import java.util.Scanner; public 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: