195k views
1 vote
Assuming that a valid price should be between 30 and 50, does the following code snippet test this condition correctly?final int MIN_PRICE = 30; final int MAX_PRICE = 50; int price = 0; Scanner in = new Scanner(System.in);System.out.print("Please enter the price: ");price = in.nextInt();if (price < MIN_PRICE){ System.out.println("Error: The price is too low.");} else if (price > MAX_PRICE) { System.out.println("Error: The price is too high.");} else{ System.out.println("The price entered is in the valid price range.");}a) This code snippet ensures that the price value is between 30 and 50.b) This code snippet only ensures that the price value is greater than 30.c) This code snippet only ensures that the price value is less than 50.d) This code snippet ensures that the price value is either less than 30 or greater than 50

1 Answer

4 votes

Answer

A) This code snippet ensures that the price value is between 30 and 50

Step-by-step explanation:

The code snippet given ensures that the acceptable price values lies in the range of 30 and 50 inclusive of the lower and upper bound values. This condition is enforced with the the if...else if... else statements.

The first if statement;

if (price < MIN_PRICE){

System.out.println("Error: The price is too low.");

} checks if user inputted price is less that the MIN_PRICE which is 30 and displays the error message.

The second, an else if statement;

else if (price > MAX_PRICE) {

System.out.println("Error: The price is too high.");

} This checks if the user inputted price is above the MAX_PRICE which is 50 and displays the error message.

finally the else statement; else{ System.out.println("The price entered is in the valid price range.");

} Prints the message confirming a valid price.

User Jennique
by
3.6k points