-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34_Enums.java
More file actions
155 lines (117 loc) · 3.9 KB
/
34_Enums.java
File metadata and controls
155 lines (117 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Enums in Java
import java.util.*;
class Solution {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
ReqStatus r = ReqStatus.PENDING;
System.out.println(r);
switch (r) {
case PENDING:
System.out.println("0");
break;
case RUNNING:
System.out.println("1");
break;
default:
System.out.println("def");
break;
}
// Enums can be traversed using the values method.
for(ReqStatus s: ReqStatus.values()){
System.out.println(s);
}
// Values() : Returns an iterable list of all the constants in the enum
System.out.println(ReqStatus.values());
// Enum:
// Group of constants.
// all the user defined enums class are internally inheriting the
// Enum class(Java.lang.enum) from library(Java.lang) which includes default methods like
// values(), getClass(), etc.
// We cannot use new keyword with enum because it has private constructor
// only.
}
}
// All the enum member variable are final(const), static by default(implicitly)
// Syntax:
// enum {
// Your Constant goes here(seprated by comma);
// }
enum ReqStatus{
PENDING, RUNNING, REJECTED, WAITING, RESOLVED;
}
class Solution1 {
// Create enum inside Solution class.
public enum Week{MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
for(Week wk: Week.values()) System.out.println(wk);
}
}
// ------------------------------------------------------------------------
enum Laptops {
// Each Constant of enum acts as an instance of the class.
MAC(2500), XPS(1200, 2), HP(300), SURFACE(1800, 5), THINKPAD(1600);
int p;
// All the instance of the Laptops can be initialised using constructors only.
// These constructors are private by default.
// So we need not to pass any argument in main.
Laptops(int price){
p = price;
}
// We can access each instance using methods.
int displayPrice(){
return p;
}
// Overload constructor to add a multiplier to the price.
Laptops(int price, int multiplier) {
p = price * multiplier;
}
// Default Constructor
Laptops(){}
// Setter Function
void setPrice(int price, int mul) {
p = price * mul;
}
}
class Solution1 {
// function overloading
static int add(int a, int b) {
return a+b;
}
static int add(int a) {
return a + 1;
}
static void pr(){
System.out.println("Print1");
}
static void pr(int a){
System.out.println("Print2");
}
// Create enum inside Solution class.
public enum Week{MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
// Traverse.
for(Week wk: Week.values()) System.out.print(wk + " ");
System.out.println();
// Ordinal Method: Returns the index of constant.
Laptops lp = Laptops.HP;
System.out.println(lp.ordinal()); // Prints 2;
for(Laptops l: Laptops.values()){
System.out.print(l.ordinal() + " "); // 0,1,2,3,4,5
}
System.out.println();
for(Laptops l: Laptops.values()){
System.out.print(l.displayPrice() + " "); // Prints price of each laptop.
l.setPrice(32, 0); // Sets the price to zero for each constant.
}
System.out.println();
for(Laptops l: Laptops.values()){
System.out.print(l.displayPrice() + " "); // 0 0 0 0 0
}
System.out.println(add(3));
System.out.println(add(3, 4));
pr();
pr(40);
}
}