diff --git a/SimulazioneVerifica/.classpath b/SimulazioneVerifica/.classpath new file mode 100644 index 0000000..57bca72 --- /dev/null +++ b/SimulazioneVerifica/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/SimulazioneVerifica/.project b/SimulazioneVerifica/.project new file mode 100644 index 0000000..32c1f5c --- /dev/null +++ b/SimulazioneVerifica/.project @@ -0,0 +1,28 @@ + + + SimulazioneVerifica + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + + + 1758261738047 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/SimulazioneVerifica/src/Main.java b/SimulazioneVerifica/src/Main.java new file mode 100644 index 0000000..9619fc6 --- /dev/null +++ b/SimulazioneVerifica/src/Main.java @@ -0,0 +1,73 @@ +class SensorException extends Exception { + SensorException(String message) { + super(message); + } +} + +interface Readable { + double readValue() throws SensorException; +} + +abstract class Sensor implements Readable { + private String id; + Sensor(String id) { + this.id = id; + } + String getId() { + return id; + } + @Override + public abstract double readValue() throws SensorException; +} + +class TemperatureSensor extends Sensor { + TemperatureSensor(String id) { + super(id); + } + @Override + public double readValue() throws SensorException { + if (Math.random() < 0.1) { + throw new SensorException("Errore di lettura temperatura"); + } + return 18 + Math.random() * 8; + } +} + +class HumiditySensor extends Sensor { + HumiditySensor(String id) { + super(id); + } + @Override + public double readValue() throws SensorException { + if (Math.random() < 0.1) { + throw new SensorException("Errore di lettura umidità"); + } + return 30 + Math.random() * 40; + } +} + +public class Main { + public static void main(String[] args) { + Sensor[][] grid = new Sensor[3][3]; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + String id = "S" + i + j; + if ((i + j) % 2 == 0) + grid[i][j] = new TemperatureSensor(id); + else + grid[i][j] = new HumiditySensor(id); + } + } + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + try { + double value = grid[i][j].readValue(); + System.out.printf("Sensore %s: %.2f%n", grid[i][j].getId(), value); + } catch (SensorException e) { + System.out.printf("Errore nel sensore %s: %s%n", + grid[i][j].getId(), e.getMessage()); + } + } + } + } +}