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
enumthat can be used to represent the days of the week: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY. - Iterate over the values of the
enumand for if it is a weekend (SATURDAY OR SUNDAY), printWelcome to the weekend!, otherwise printHave a great day at work or school! - Try the code in the Java Playground.
Practice 2: Seasons
- Define an
enumthat 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
enumand 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
enumthat 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
enumthat can be used to represent the different shipping options: STANDARD, EXPRESS, OVERNIGHT. - Iterate over the values of the
enumand 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.