image
Sample Project - Traffic Lights Program with Enum
Start course
Difficulty
Beginner
Duration
15m
Students
93
Ratings
5/5
Description

In this course, we will learn the Enum Type in Java. 

Learning Objectives

  • What the Enum Type is
  • How the Enum type is defined and used

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll develop another project with Enum type to understand it better. In this project, we're going to develop a traffic light application by using the Enum type. We'll learn how values can be assigned to Enums in this project. If you're ready, let's start. First, I'll create a new Java project. I click on the file menu and select the New Java Project Option. I'll specify the project name as Enum Traffic Light. The Java version will be Java SE1.8. Don't change the other options and click the 'Finish' button. Now, I will create an Enum. In this project, right click on the source folder and select "New Enum." Specify the package name as traffic light and the Enum name as traffic light, and click the 'Finish' button to create it. 

We can assign some custom values to Enums. Let's define three constant colors in Traffic Light Enum class. First, red with stop value. Second, yellow with wait value. And last, green with go value. These definitions will also call Enum constructor with one string parameter. Let's declare a variable action with string type to get values of Enums. private String action. Let's create a constructor traffic light that takes one parameter action with string type. private TrafficLight(String action) And I'll assign the parameter value action to variable action of this Enum. this.action = action. At this stage, I'll show you an important thing. Note that we define the constructor as private, because the Enum constructors can only be private or default. Let's try to change the private access modifier with public. As you can see, we get a compilation error. 

If you place the mouse pointer on TrafficLight, the word that's underlined with the red color, the error message says illegal modifier for the Enum constructor, only private is permitted. So, I will change the modifier with private again. Finally, let's create the getter method of variable action. But this time, I will not use shortcuts as we did in the previous lessons. I'll create the getter method myself. Of course, you can always create it using a shortcut, but you should not forget how it was created manually. public String getAction. In the get method, I write return action. Yes, so we also created the getter method, let's save the code. Okay, now let's create the main test class to test it. I right click on the traffic light package and select new class. Specify the class name as TrafficLightTest, and select the checkbox to add the main method and click the 'Finish' button. 

Okay, first, let's create an Enum instance array that its name is lights. TrafficLight [ ] lights =, and I'll assign the Enum values to this array. So, after the equal sign, I write trafficlight.values. We use the values methods of the Enum class to get an array of Enum values. Okay, now I'll use the for-each loop to access Enum constants in their values. In for parentheses, we declare an Enum instance light with traffic light type. And after putting a colon, I write the lights array. Okay, we want to iterate each element in the lights array and store it in the light instance. And we will access the name and action of the Enum constants by using this instance. In the print method, first, I will call the name method to get the name of the Enum constant. sout "light + light.name". The name method is predefined in the Enum type. We can also use the toString method instead of this method. 

Okay, let's continue. Secondly, I'll call the getAction method to get the value of the Enum constant. So, after the plus sign in double quotes, I write, "Action + light.getAction". Okay, let's run the code. You see the results in the console, but if you notice, we didn't leave a space before the action statement. Let's fix this right now. I'm putting a space here. Now, let's run it one more time. Yes, now we can see the result better. Light is red, and its action is stop. Light is yellow, and its action is wait. Light is green, and its action is go. Let's change the name method with a toString method. There's a little difference between the name method and the toString method of Enum class. The name method is final, so it cannot be overridden, but the toString method is public, and it can be overridden. Okay, let's run the code again. You see the same result.

Yes, these are the things you need to know about Enum type. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
3984
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics