Skip Top Navigation Bar

Write Expressions that Require if Statements

Your Turn

  • Write program code to complete the provided tasks.
  • Run the code in the Java Program to see if your code generates the desired result.
  • Make necessary changes until you have satisfied the tasks.

Practice 1: Free Shipping

  • Write an if statement that prints You 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 if statement that prints Low Battery if 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 if statement that prints too short if 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-else statement that prints Light is on if lightSwitch is true, otherwise print Light 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-else statement that prints You are eligible for a discount if age is less than 18 or greater than 55, and prints You are ineligible for a discount otherwise.
  • 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 if statement that prints Special offer unlocked if purchaseAmount is over $100 and member is true indicating 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 if statement for a smart thermostat that prints Turn heat on if temperature is below 50 or override is true indicating the system has been manually overridden, and Heat is off otherwise.
  • 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 speed is over 120, print Over speed limit
    • if speed is between 60 and 120(inclusive), print Within safe range
    • otherwise, print Below minimum speed
  • 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 activated if any of the following are true: fireAlarm, intruderAlert, or floodSensor.
  • 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 loyaltyLevel and lastVisit and the following:
    • if loyaltyLevel is Gold and lastVisit is less than 30, print Welcome back valued member!
    • if loyaltyLevel is Gold and lastVisit is at least 30, print We've missed you!
    • if loyaltyLevel is not Gold, print Join today!
  • 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.

Resources