Hello!
I'm kind of super confused on this in general.
In this code, I'm testing it through the main method that I put at the end of the code and essentially, I'm supposed to be testing that the methods execute correctly.
I keep getting nullPointException errors which I essentially know what they are, but I'm having difficulty tracing it back to where exactly doesn't have any memory address path.
Also, I didn't create a mode method completely yet.
Here's my code:
public class Stat { private double [] data; int x = 0; public Stat() { //default constructor double [] dataArray = new double [x]; dataArray[x] = 0.0; } public Stat(double[] d) { data = new double [d.length]; for (int i = 0; i<d.length; i++) { data[i] = d[i]; } } private double [] getDataArray; public double [] getData() { getDataArray = new double [data.length]; for (int i = 0; i < data.length; i++) { data [i] = getDataArray[i]; } return getDataArray; } private double [] setDataArray; public void setData (double [] d) { setDataArray = new double [d.length]; for (int i =0; i<d.length; i++) { d[i] = setDataArray[i]; } data = setDataArray; } public boolean equals(Stat s) { if (data.length == s.getData().length) { for (int i = 0; i < s.getData().length; i++) { data[i] = s.getData()[i]; } return true; } else { return false; } } public String toString() { return "[" + data + "]"; } double min = data[0]; public double min() { for (int i = 0; i < data.length; i++) { if (min > data[i]) { min = data[i]; } } return min; } double max = data[0]; public double max() { for (int i = 0; i < data.length; i++) { if (max < data[i]) { max = data[i]; } } return max; } public double average() { double sum = 0; double numOfElements = 0; for (int i = 0; i<data.length; i++) { sum = sum + data[i]; numOfElements = numOfElements + i; } return sum/numOfElements; } public double mode() { //go through each element in an array and compare it to the next element and see if it is the same, then add that one to count for (int i =0; i<data.length; i++) { } return 0.0; } public static void main(String[] args) { double[] data = {-5.3, 2.5, 88.9, 0, 0.0, 28, 16.5, 88.9, 109.5, -90, 88.9}; double[] data2 = {100.34, 50.01, 50.01, -8}; Stat stat1 = new Stat(); System.out.println("stat1 data = " + stat1.toString()); stat1 = new Stat(data); System.out.println("stat1 has been altered."); System.out.println("stat1 data = " + stat1.toString()); System.out.println("stat1 min = " + stat1.min()); System.out.println("stat1 max = " + stat1.max()); System.out.println("stat1 average = " + stat1.average()); System.out.println("stat1 mode = " + stat1.mode() + "\n"); Stat stat2 = new Stat(); stat2.setData(data2); Stat stat3 = new Stat(stat1.getData()); System.out.println("stat2 data = " + stat2.toString()); System.out.println("stat3 data = " + stat3.toString()); System.out.println(); System.out.println("stat1 is equal to stat2 using \"equals()\"? " + stat1.equals(stat2)); System.out.println("stat1 is equal to stat3 using \"equals()\"? " + stat1.equals(stat3)); System.out.println("stat1 is equal to stat3 using \"==\"? " + (stat1== stat3)); } }
Thank you so much!!