Write Expressions that Require if Statements
Your Turn
- Write program code to complete the provided tasks.
- Run the code in the Java Playground to see if your code generates the desired result.
- Make necessary changes until you have satisfied the tasks.
Practice 1: Free Shipping
- Write an
ifstatement that printsYou qualify for free shipping!if the order total is greater than $50. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 2: Low Battery Warning
- Write an
ifstatement that printsLow Batteryif the battery level drops below 10. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 3: String Length Requirement
- Write an
ifstatement that printstoo shortif the length of the string is less than 8 characters. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 4: Lights Out
- Write an
if-elsestatement that printsLight is oniflightSwitchistrue, otherwise printLight is off. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 5: Discount Eligible
- Write an
if-elsestatement that printsYou are eligible for a discountifageis less than 18 or greater than 55, and printsYou are ineligible for a discountotherwise. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 6: Special Offer
- Write an
ifstatement that printsSpecial offer unlockedifpurchaseAmountis over $100 andmemberistrueindicating the customer is a registered member. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 7: Automatic Thermostat
- Write an
ifstatement for a smart thermostat that printsTurn heat oniftemperatureis below 50 oroverrideistrueindicating the system has been manually overridden, andHeat is offotherwise. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 8: Speed Check
- Write a statement for checking
speed.- if
speedis over 120, printOver speed limit - if
speedis between 60 and 120(inclusive), printWithin safe range - otherwise, print
Below minimum speed
- if
- Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 9: Emergency Mode
- Write a statement to print
Emergency mode activatedif any of the following aretrue:fireAlarm,intruderAlert, orfloodSensor. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 10: Customer Loyalty
- Write a statement to prints a message based on
loyaltyLevelandlastVisitand the following:- if
loyaltyLevelisGoldandlastVisitis less than 30, printWelcome back valued member! - if
loyaltyLevelisGoldandlastVisitis at least 30, printWe've missed you! - if
loyaltyLevelis notGold, printJoin today!
- if
- Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.