Traffic Lights in Various Programming Languages

Posted in category Software on
712 Words ~4 Minute Reading Time • Subscribe to receive updates on Software
Eric David Smith
Software Engineer / Musician / Entrepreneur
Building a Traffic Light System in Various Programming Languages by Eric David Smith

State transitions

Let's build a Traffic Light System in Various Programming Languages - starting with C++, then JAVA, and finally GO - in the end, there are some bonuses.

State transitions are a core concept in computer programming, illustrating how a system can transition between different states based on certain conditions. A good way to understand this concept is by modeling a traffic light system, where the system transitions between "red", "yellow", and "green" states. Let's take a look at how this works in different programming languages.

Traffic Light System in C++

C++ is a statically typed, compiled language known for its efficiency. Here's a simple traffic light system implemented in C++.

#include <iostream>
#include <thread>
#include <chrono>

enum class LightColor {
    Red,
    Yellow,
    Green
};

int main() {
    LightColor currentLight = LightColor::Red;
    while(true) {
        switch(currentLight) {
        case LightColor::Red:
            std::cout << "Red light, stop!" << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(5));
            currentLight = LightColor::Green;
            break;
        case LightColor::Yellow:
            std::cout << "Yellow light, get ready!" << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(2));
            currentLight = LightColor::Red;
            break;
        case LightColor::Green:
            std::cout << "Green light, go!" << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(5));
            currentLight = LightColor::Yellow;
            break;
        }
    }
    return 0;
}

Run It:

First, compile the code using a C++ compiler like g++. Assuming your C++ code is in a file called traffic_light.cpp, you can compile it using:

g++ traffic_light.cpp -o traffic_light

Traffic Light System in Java

Java is an object-oriented language that's widely used in enterprise software. Here's the same traffic light system implemented in Java.

public class Main {
    enum LightColor {
        Red,
        Yellow,
        Green
    }

    public static void main(String[] args) throws InterruptedException {
        LightColor currentLight = LightColor.Red;
        while(true) {
            switch(currentLight) {
                case Red:
                    System.out.println("Red light, stop!");
                    Thread.sleep(5000);
                    currentLight = LightColor.Green;
                    break;
                case Yellow:
                    System.out.println("Yellow light, get ready!");
                    Thread.sleep(2000);
                    currentLight = LightColor.Red;
                    break;
                case Green:
                    System.out.println("Green light, go!");
                    Thread.sleep(5000);
                    currentLight = LightColor.Yellow;
                    break;
            }
        }
    }
}

Run It:

Assuming your Java code is in a file called Main.java, you can compile it using the javac command:

javac Main.java

This will produce a file called Main.class that contains the bytecode. You can run it using the java command:

java Main

Traffic Light System in Go

Go, also known as Golang, is a statically typed compiled language developed at Google. It's known for its simplicity and efficiency. Here's the traffic light system in Go.

package main

import (
	"fmt"
	"time"
)

type LightColor int

const (
	Red LightColor = iota
	Yellow
	Green
)

func main() {
	currentLight := Red
	for {
		switch currentLight {
		case Red:
			fmt.Println("Red light, stop!")
			time.Sleep(5 * time.Second)
			currentLight = Green
		case Yellow:
			fmt.Println("Yellow light, get ready!")
			time.Sleep(2 * time.Second)
			currentLight = Red
		case Green:
			fmt.Println("Green light, go!")
			time.Sleep(5 * time.Second)
			currentLight = Yellow
		}
	}
}

Run It:

Assuming your Go code is in a file called traffic_light.go, you can compile and run it using the go run command:

go run traffic_light.go

Traffic Light System in Ada Programming Language

Ada is a statically-typed, high-level programming language designed for the development of very large software systems. It's known for its safety and reliability. Here's the traffic light system in Ada.

See my article on Building a Traffic Light System in Ada Programming Language

Traffic Light System in Julia Programming Language

Julia is a high-level, high-performance dynamic programming language for technical computing. It's known for its speed and ease of use. Here's the traffic light system in Julia.

See my article on Building a Traffic Light System in Julia Programming Language

Closing Thoughts

By creating a simple traffic light system in different programming languages, we've demonstrated the concept of state transitions. Each language has its own style and syntax, but the underlying concept of switching between states based on conditions is universal in programming. This foundational knowledge will prove useful as you delve deeper into more complex programming concepts. Happy and healthy coding! Please never run red lights! 🚦

Supporting My Work

Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!

Contact


Eric David Smith
Software Engineer / Musician / Entrepreneur
Software

Related Blog Posts

👈 Scroll 👉

Blog Post Tags