// 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 theexecute()
method, which each concrete command class will implement. - Concrete Command Classes (
LightOnCommand
andLightOffCommand
): These classes implement theCommand
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 whenpressButton()
is called. - Client (
RemoteControlTest
): The main method creates instances ofLight
, commands, and theRemoteControl
. It sets commands and tests the remote control functionality.
How to Compile and Run
- Save this code in a file named
RemoteControlTest.java
. - Open a terminal and navigate to the directory where you saved the file.
- Compile the program:
javac RemoteControlTest.java
- 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
- Arduino board (e.g., Arduino Uno)
- DHT11 or DHT22 temperature and humidity sensor
- Connecting wires
Steps
- 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
- Upload the code to the Arduino board.
- 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
- Connect your Arduino to your computer and open the Arduino IDE.
- Copy and paste the code into the IDE.
- Select your Arduino board and the correct port from the Tools menu.
- Click Upload to upload the code to the Arduino.
- 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.