Question: Java: Solution Description The Drink Enum Contains CHOCOLATE', COFFEE, TEA and 'EMPTY' as possible values. The above should have the following name and price (instances
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 nane; 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 i 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 2 Fields are private
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
