Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Enum examples #61

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions advanced_java/enum/Day.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum Day {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use other Enum in built methods as well and show their functionality here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please name a few?

SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
6 changes: 6 additions & 0 deletions advanced_java/enum/MyClass1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class MyClass1 {
Tawishi marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
Day variable = Day.TUESDAY
System.out.println(variable);
}
}
6 changes: 6 additions & 0 deletions advanced_java/enum/MyClass2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class MyClass2 {
Tawishi marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
Day myVar = Day.SUNDAY;
System.out.println(myVar);
}
}
7 changes: 7 additions & 0 deletions advanced_java/enum/MyClass3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class MyClass3 {
Tawishi marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
for (Day myVar : Day.values()) {
System.out.println(myVar);
}
}
}
29 changes: 29 additions & 0 deletions advanced_java/enum/MyClass4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class MyClass4 {
Tawishi marked this conversation as resolved.
Show resolved Hide resolved
public static void main(String[] args) {
Day myVar = Day.WEDNESDAY;

switch(myVar) {
case MONDAY:
System.out.println("Workday.");
break;
case TUESDAY:
System.out.println("Presentaion day.");
Tawishi marked this conversation as resolved.
Show resolved Hide resolved
break;
case WEDNESDAY:
System.out.println("Workday.");
break;
case THURSDAY:
System.out.println("Workday.");
break;
case FRIDAY:
System.out.println("PARTY!");
break;
case SATURDAY:
System.out.println("Relax.");
break;
case SUNDAY:
System.out.println("Complete deadlines.");
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move all the constants to a final variable and use it instead of hardcoding them in here.

Copy link
Author

@Tawishi Tawishi Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the statements I am displaying?

}
}
}
16 changes: 16 additions & 0 deletions advanced_java/enum/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# About Repository
This repository contains the *Enum class* in Java, with examples. Enum is short for "enumerations", which means "specifically listed".<br><br>
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. *Note* that they should be in uppercase letters.

## About files
1. [Day.java][1] : Contains the enum class used in all other files (test class).
1. [MyClass1.java][2] : Access enum constants with the dot syntax.
1. [MyClass2.java][3] : Having enum inside another class.
1. [MyClass3.java][4] : Returning an array of all enum constants using value() methis of enum type.This method is useful when you want to loop through the constants of an enum.
1. [MyClass4.java][5] : Using enum in switch case statement

[1]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/Day.java
[2]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/MyClass1.java
[3]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/MyClass2.java
[4]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/MyClass3.java
[5]:https://github.com/Tawishi/datastructures/blob/enum-java/advanced_java/enum/MyClass4.java