Write a Java Program to implement command pattern to test Remote Control

// Command interface interface Command { void execute(); } // Concrete Command class to turn the light on class LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOn(); } } // Concrete Command class to turn the light off class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { this.light = light; } @Override public void execute() { light.turnOff(); } } // Receiver class class Light { public void turnOn() { System.out.println("Light is ON"); } public void turnOff() { System.out.println("Light is OFF"); } } // Invoker class class RemoteControl { private Command command; // Method to set the command public void setCommand(Command command) { this.command = command; } // Method to execute the command public void pressButton() { command.execute(); } } // Client public class RemoteControlTest { public static void main(String[] args) { // Create a Light instance (Receiver) Light light = new Light(); // Create Command instances Command lightOnCommand = new LightOnCommand(light); Command lightOffCommand = new LightOffCommand(light); // Create RemoteControl (Invoker) RemoteControl remoteControl = new RemoteControl(); // Set and execute Light ON command remoteControl.setCommand(lightOnCommand); remoteControl.pressButton(); // Output: Light is ON // Set and execute Light OFF command remoteControl.setCommand(lightOffCommand); remoteControl.pressButton(); // Output: Light is OFF } }

Explanation

  • Command Interface (Command): Defines the execute() method, which each concrete command class will implement.
  • Concrete Command Classes (LightOnCommand and LightOffCommand): These classes implement the Command interface, specifying the action to turn the light on or off.
  • Receiver (Light): Contains the methods to turn the light on or off, which are invoked by the command objects.
  • Invoker (RemoteControl): Holds a command and executes it when pressButton() is called.
  • Client (RemoteControlTest): The main method creates instances of Light, commands, and the RemoteControl. It sets commands and tests the remote control functionality.

How to Compile and Run

  1. Save this code in a file named RemoteControlTest.java.
  2. Open a terminal and navigate to the directory where you saved the file.
  3. Compile the program:

    javac RemoteControlTest.java
  4. Run the program:

    java RemoteControlTest

Expected Output


Light is ON Light is OFF
























---------------------------







To create a temperature notification system using Arduino, we can use a temperature sensor like the DHT11 or DHT22 (for temperature and humidity). The Arduino will read the temperature from the sensor and display a notification if the temperature exceeds a certain threshold.

Here's how to do it:

Components Needed

  1. Arduino board (e.g., Arduino Uno)
  2. DHT11 or DHT22 temperature and humidity sensor
  3. Connecting wires

Steps

  1. Connect the DHT sensor to your Arduino:
    • DHT11/DHT22 Pin 1 (VCC) to Arduino 5V
    • DHT11/DHT22 Pin 2 (Data) to Arduino digital pin 2
    • DHT11/DHT22 Pin 4 (GND) to Arduino GND
  2. Upload the code to the Arduino board.
  3. Install the DHT library if not already installed.

Code Explanation

The code reads the temperature from the DHT sensor and sends a notification if the temperature exceeds a specified threshold.

Code


// Include necessary libraries #include <DHT.h> // Define the pin connected to the DHT sensor #define DHTPIN 2 // Pin connected to the data pin of DHT sensor // Define the type of DHT sensor #define DHTTYPE DHT11 // DHT11 or DHT22 // Initialize the DHT sensor DHT dht(DHTPIN, DHTTYPE); // Define temperature threshold const float temperatureThreshold = 30.0; // Set your threshold temperature here (e.g., 30°C) void setup() { Serial.begin(9600); // Start serial communication dht.begin(); // Start DHT sensor } void loop() { // Reading temperature as Celsius (the default) float temperature = dht.readTemperature(); // Check if any reads failed and exit early (to try again) if (isnan(temperature)) { Serial.println("Failed to read from DHT sensor!"); return; } // Display temperature Serial.print("Current Temperature: "); Serial.print(temperature); Serial.println("°C"); // Check if temperature exceeds threshold if (temperature > temperatureThreshold) { Serial.println("Warning: Temperature has exceeded the threshold!"); // You can add more notification mechanisms here, like an LED or buzzer alert } // Wait a bit before taking another reading delay(2000); // Delay for 2 seconds }

Explanation of the Code

  • DHT Library: The DHT library is used to read data from the DHT11/DHT22 sensor. Make sure this library is installed in your Arduino IDE (Tools > Manage Libraries > search for "DHT" by Adafruit and install).
  • Threshold Temperature: Set a threshold temperature (temperatureThreshold) that, when exceeded, triggers a notification in the serial monitor.
  • Notification: When the temperature exceeds the threshold, a message is printed to the serial monitor. You could also add an LED, buzzer, or even connect this to an IoT platform for SMS/email alerts.

How to Run the Code

  1. Connect your Arduino to your computer and open the Arduino IDE.
  2. Copy and paste the code into the IDE.
  3. Select your Arduino board and the correct port from the Tools menu.
  4. Click Upload to upload the code to the Arduino.
  5. Open the Serial Monitor from the Tools menu to see temperature readings and any notifications.

With this setup, you’ll receive a serial monitor notification each time the temperature goes above the specified threshold. You can add other notifications like an LED or a buzzer by adding code to control those outputs when the threshold is reached.




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.