I got a homework that I have tried for a long time and even asked professor about it and still not understand what gone wrong in this code. I have absolutely no idea on how to initialize time ( in SoilTempData constructor ) and not sure on what's wrong with the error so I got no idea on how to fix it. I also can't change codes much because of the Questions to remain the structure of the given code [This is the given Diagram][1]
package Iot;
import java.util.ArrayList;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SoilTempData implements ActionListener{
private final int REPORT_ROUND = 1000/100;
ArrayList<Integer> data;
private int round;
private int nextPrintIndex;
private Timer time;
private SoilTempIoT iot;
public SoilTempData(SoilTempIoT iot){
data = new ArrayList<>(10);
round = 0;
nextPrintIndex = 0;
time = new Timer(100, new SoilTempData(iot));
iot = new SoilTempIoT();
}
public void start() {
time.start();
}
public void actionPerformed(ActionEvent e){
if(!(round == REPORT_ROUND))
time.start();
else
printStat();
}
public void printStat() {
int min = data.get(nextPrintIndex);
int max = data.get(nextPrintIndex);
int sum = 0;
double average = 0.0;
double sd = 0.0;
System.out.println("********** Report Current Data: **********");
System.out.print("New set of data: ");
for(int i = nextPrintIndex ; i < data.size() ; i ) {
System.out.print(data.get(i) ", ");
sum = data.get(i);
round ;
}
average = sum/data.size();
for(int i = nextPrintIndex ; i < data.size() ; i ) {
sd = Math.pow((data.get(i) - average),2);
}
System.out.printf("\nTotal records: %d\n",REPORT_ROUND*round);
System.out.println("Statistics:");
if(!data.isEmpty()) {
System.out.printf("Min = %.2f\nMax = %.2f\nAverage = %.2f\nSD = %.2f\n",min,max,average,sd);
}
else
System.out.println("No data to report!");
nextPrintIndex = REPORT_ROUND*round;
}
}
and this is SoilTempIoT
package Iot;
import java.awt.event.ActionListener;
import java.util.Random;
public class SoilTempIoT {
public static final int MIN_TEMP = 10;
public static final int MAX_TEMP = 60;
private int temp;
private Random rand;
public SoilTempIoT() {
rand = new Random();
temp = rand.nextInt(50) 10;
}
public int getTemp() {
int i = rand.nextInt(21) - 10;
return temp i;
}
}
and Main class ( which can't change anything because of the Question but incase anyone want to take a look at it )
package Iot;
import javax.swing.JOptionPane;
public class SoilTempDataTest {
public static void main(String[] args) {
SoilTempData sensor = new SoilTempData(new SoilTempIoT());
sensor.printStat();
sensor.start();
JOptionPane.showMessageDialog(null, "Quit?");
System.exit(0);
}
}
If my question isn't good in any way please tell me I will improve overtime.
Edit : I also need to do it so that SoilTempData read data from SoilTempIoT 100ms and every 1000ms it will print out the Stat. [1]: https://i.stack.imgur.com/VDefv.png
CodePudding user response:
public SoilTempData(SoilTempIoT iot){ data = new ArrayList<>(10); round = 0; nextPrintIndex = 0; time = new Timer(100, new SoilTempData(iot)); iot = new SoilTempIoT(); }
When you initialize time
, you create another instance. That calls the same constructor, which initializes time
, which creates another instance, etc.
Just use time = new Timer(100, this);