forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThermometer.java
39 lines (31 loc) · 1.1 KB
/
Thermometer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Random;
// Thermometer class to represent a thermometer
class Thermometer {
private double temperature;
public Thermometer() {
// Initialize the temperature to a random value between 0 and 100
Random rand = new Random();
temperature = rand.nextDouble() * 100;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public void measure() {
// Simulate measuring the temperature by updating it randomly
Random rand = new Random();
temperature += rand.nextDouble() * 2 - 1; // Random change between -1 and 1
}
}
public class ThermometerApp {
public static void main(String[] args) {
Thermometer thermometer = new Thermometer();
System.out.println("Initial temperature: " + thermometer.getTemperature() + "°C");
for (int i = 0; i < 5; i++) {
thermometer.measure();
System.out.println("Updated temperature: " + thermometer.getTemperature() + "°C");
}
}
}