Open marrrolom opened 1 month ago
import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*;
public class SmartHomeAutomation {
// Abstract SmartDevice class
public static abstract class SmartDevice {
// Abstract method to be implemented by subclasses
public abstract void performAction();
}
// Device class
public static class Device extends SmartDevice {
private String deviceId;
private String deviceName;
private boolean isOn;
public Device(String deviceId, String deviceName) {
this.deviceId = deviceId;
this.deviceName = deviceName;
this.isOn = false; // By default, devices start off
}
// Getters and setters
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public boolean isOn() {
return isOn;
}
public void setOn(boolean on) {
isOn = on;
}
// Override performAction method
@Override
public void performAction() {
// Define action for generic device
System.out.println("Performing action for generic device: " + deviceName);
}
// Override toString method
@Override
public String toString() {
return "Device{" +
"deviceId='" + deviceId + '\'' +
", deviceName='" + deviceName + '\'' +
", isOn=" + isOn +
'}';
}
}
// Light subclass
public static class Light extends Device {
private int brightness;
public Light(String deviceId, String deviceName, int brightness) {
super(deviceId, deviceName);
this.brightness = brightness;
}
public int getBrightness() {
return brightness;
}
public void setBrightness(int brightness) {
this.brightness = brightness;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Adjusting brightness of light " + getDeviceName() + " to " + brightness);
}
// Override toString method
@Override
public String toString() {
return "Light{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", brightness=" + brightness +
'}';
}
}
// Thermostat subclass
public static class Thermostat extends Device {
private int temperature;
public Thermostat(String deviceId, String deviceName, int temperature) {
super(deviceId, deviceName);
this.temperature = temperature;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Setting temperature of thermostat " + getDeviceName() + " to " + temperature);
}
// Override toString method
@Override
public String toString() {
return "Thermostat{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", temperature=" + temperature +
'}';
}
}
// SecurityCamera subclass
public static class SecurityCamera extends Device {
private String resolution;
public SecurityCamera(String deviceId, String deviceName, String resolution) {
super(deviceId, deviceName);
this.resolution = resolution;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Recording with security camera " + getDeviceName() + " at " + resolution + " resolution");
}
// Override toString method
@Override
public String toString() {
return "SecurityCamera{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", resolution='" + resolution + '\'' +
'}';
}
}
// SmartHomeSystem class (unchanged)
// Swing GUI for Smart Home System (unchanged)
public static void main(String[] args) {
// Testing the SmartHomeSystem and devices
SmartHomeSystem smartHomeSystem = new SmartHomeSystem();
Light light = new Light("L1", "Living Room Light", 75);
Thermostat thermostat = new Thermostat("T1", "Living Room Thermostat", 22);
SecurityCamera camera = new SecurityCamera("C1", "Front Door Camera", "1080p");
smartHomeSystem.addDevice(light);
smartHomeSystem.addDevice(thermostat);
smartHomeSystem.addDevice(camera);
// List devices
smartHomeSystem.listDevices();
// Example usage of iterator
SmartHomeSystem.DeviceIterator iterator = smartHomeSystem.new DeviceIterator();
while (iterator.hasNext()) {
Device device = iterator.next();
System.out.println("Iterator device: " + device);
}
// Example usage of sorting by name
ArrayList<Device> deviceList = new ArrayList<>(smartHomeSystem.devices.values());
deviceList.sort(new SmartHomeSystem.SortByName());
System.out.println("\nDevices sorted by name:");
for (Device device : deviceList) {
System.out.println(device);
}
// Example usage of Swing GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SmartHomeGUI();
}
});
}
}
To modify the existing SmartHomeAutomation code to include an abstract class SmartDevice with an abstract method performAction() that each device and its subclasses (Light, Thermostat, SecurityCamera) will implement, follow these steps:
Create Abstract Class SmartDevice:
Define an abstract class SmartDevice with an abstract method performAction(). Modify Device and Subclasses:
Modify Device, Light, Thermostat, and SecurityCamera to extend SmartDevice. Implement the performAction() method in each subclass to define specific actions for each device. Here’s how you can integrate these changes into your existing code:
java Copy code import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*;
public class SmartHomeAutomation {
// Abstract SmartDevice class
public static abstract class SmartDevice {
// Abstract method to be implemented by subclasses
public abstract void performAction();
}
// Device class
public static class Device extends SmartDevice {
private String deviceId;
private String deviceName;
private boolean isOn;
public Device(String deviceId, String deviceName) {
this.deviceId = deviceId;
this.deviceName = deviceName;
this.isOn = false; // By default, devices start off
}
// Getters and setters
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public boolean isOn() {
return isOn;
}
public void setOn(boolean on) {
isOn = on;
}
// Override performAction method
@Override
public void performAction() {
// Define action for generic device
System.out.println("Performing action for generic device: " + deviceName);
}
// Override toString method
@Override
public String toString() {
return "Device{" +
"deviceId='" + deviceId + '\'' +
", deviceName='" + deviceName + '\'' +
", isOn=" + isOn +
'}';
}
}
// Light subclass
public static class Light extends Device {
private int brightness;
public Light(String deviceId, String deviceName, int brightness) {
super(deviceId, deviceName);
this.brightness = brightness;
}
public int getBrightness() {
return brightness;
}
public void setBrightness(int brightness) {
this.brightness = brightness;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Adjusting brightness of light " + getDeviceName() + " to " + brightness);
}
// Override toString method
@Override
public String toString() {
return "Light{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", brightness=" + brightness +
'}';
}
}
// Thermostat subclass
public static class Thermostat extends Device {
private int temperature;
public Thermostat(String deviceId, String deviceName, int temperature) {
super(deviceId, deviceName);
this.temperature = temperature;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Setting temperature of thermostat " + getDeviceName() + " to " + temperature);
}
// Override toString method
@Override
public String toString() {
return "Thermostat{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", temperature=" + temperature +
'}';
}
}
// SecurityCamera subclass
public static class SecurityCamera extends Device {
private String resolution;
public SecurityCamera(String deviceId, String deviceName, String resolution) {
super(deviceId, deviceName);
this.resolution = resolution;
}
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
// Override performAction method
@Override
public void performAction() {
System.out.println("Recording with security camera " + getDeviceName() + " at " + resolution + " resolution");
}
// Override toString method
@Override
public String toString() {
return "SecurityCamera{" +
"deviceId='" + getDeviceId() + '\'' +
", deviceName='" + getDeviceName() + '\'' +
", isOn=" + isOn() +
", resolution='" + resolution + '\'' +
'}';
}
}
// SmartHomeSystem class (unchanged)
// Swing GUI for Smart Home System (unchanged)
public static void main(String[] args) {
// Testing the SmartHomeSystem and devices
SmartHomeSystem smartHomeSystem = new SmartHomeSystem();
Light light = new Light("L1", "Living Room Light", 75);
Thermostat thermostat = new Thermostat("T1", "Living Room Thermostat", 22);
SecurityCamera camera = new SecurityCamera("C1", "Front Door Camera", "1080p");
smartHomeSystem.addDevice(light);
smartHomeSystem.addDevice(thermostat);
smartHomeSystem.addDevice(camera);
// List devices
smartHomeSystem.listDevices();
// Example usage of iterator
SmartHomeSystem.DeviceIterator iterator = smartHomeSystem.new DeviceIterator();
while (iterator.hasNext()) {
Device device = iterator.next();
System.out.println("Iterator device: " + device);
}
// Example usage of sorting by name
ArrayList<Device> deviceList = new ArrayList<>(smartHomeSystem.devices.values());
deviceList.sort(new SmartHomeSystem.SortByName());
System.out.println("\nDevices sorted by name:");
for (Device device : deviceList) {
System.out.println(device);
}
// Example usage of Swing GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SmartHomeGUI();
}
});
}
} Explanation: Abstract Class SmartDevice:
Introduced an abstract class SmartDevice with an abstract method performAction(). Each subclass (Device, Light, Thermostat, SecurityCamera) extends SmartDevice and implements performAction() with specific behavior. Subclass Implementations:
Each subclass (Light, Thermostat, SecurityCamera) overrides performAction() to define actions appropriate for their functionality (brightness adjustment, temperature setting, recording). Main Method:
Example usage remains the same, showcasing how devices are added to SmartHomeSystem, listed, iterated through, sorted, and used in a Swing GUI interface. This structure ensures that each device type can perform its unique actions through the performAction() method, adhering to the principles of object-oriented design and abstraction.
edited:შექმენით abstract კლასი smartDevice და საშუალება მიეცით device-ს და მის subclass-ებს დააიქსთენდონ ის(დაიმემკვიდრონ ის) კონკრეტულად ამ კლასშ შექმენით abstract მეთოდი performAction() და დააიმპლემენტირეთ ის თითოეულ დივაისში