Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a drink enum in the drink class Solution Description The Drink Enum Contains CHOCOLATE', COFFEE, TEA' and 'EMPTY' as possible values. . The above

Write a drink enum in the drink class image text in transcribed
image text in transcribed
Solution Description The Drink Enum Contains CHOCOLATE', COFFEE, TEA' and 'EMPTY' as possible values. . The above should have the following name and price (instances variables with the correct types): CHOCOLATE is named "hot chocolate" with price 1.50 COFFEE is named coffee" with price 2.00 TEA is named "tea" with price 1.00 EMPTY is named "nothing" with price 0.00 Be sure to use proper convention when creating getters More information about enums: . Enums are like classes with a set number of instances. For instance, if you made a color enum with values RED, GREEN and BLUE, you would not be able to create a new YELLOW instance of color. Because enums are types of classes, we can write constructors for enums. However, all instances are created within the enum class, so the constructor will be private. So, we must call the constructor when declaring the instances of the enum. Enums can also have instance fields and methods, written as you would for any other classes. Here's an example: public enum Size S("small"), M("medium"), L("large"); String name; private Size (String name) ( this.name name; public String getName O ( return this.name; On line 2 above you see constructor calls after each instance of the enum is declared. This enum has three instances called S, M and L that each have a 'name' instance field with values "small", "medium" and "large" respectively. Note that the syntax for the enum is exactly the same as that for any class after the line of instance declarations. 27 Drink [14] Is an enum with only the values: EMPTY, TEA, COFFEE, and CHOCOLATE 13 Constructor .4 price and name fields 4 Getters for all fields 12] Fields are private

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions