Skip Top Navigation Bar

Writing Expressions to Iterate Over enum Values

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: Days of the Week

  • Define an enum that can be used to represent the days of the week: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY.
  • Iterate over the values of the enum and for if it is a weekend (SATURDAY OR SUNDAY), print Welcome to the weekend!, otherwise print Have a great day at work or school!
  • Try the code in the Java Playground.

Practice 2: Seasons

  • Define an enum that can be used to represent the months of the year: JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER.
  • Iterate over the values of the enum and print the corresponding season:
    • DECEMBER, JANUARY, FEBRUARY are winter months
    • MARCH, APRIL, MAY are spring months
    • JUNE, JULY, AUGUST are summer months
    • SEPTEMBER, OCTOBER, NOVEMBER are fall months
  • Try the code in the Java Playground.

Practice 3: Payment Methods

  • Define an enum that can be used to represent the different ways to pay: CREDIT_CARD, PAYPAL, CASH, CHECK.
  • Iterate and display the different payment methods that are available to make a purchase.
  • Try the code in the Java Playground.

Practice 4: Shipping Options

  • Define an enum that can be used to represent the different shipping options: STANDARD, EXPRESS, OVERNIGHT.
  • Iterate over the values of the enum and display the type of shipping and corresponding additional charge:
    • STANDARD is an additional $2
    • EXPRESS is an additional $5
    • OVERNIGHT is an additional $7
  • Try the code in the Java Playground.