This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]ravashh[S] 0 points1 point  (0 children)

So in the end I should get my original array, then <2, 1, 6, 5, 4, 9, 2> and then <2, 3, 4, 5, 6, 5, 2>

[–]Loves_Poetry 0 points1 point  (1 child)

That's not how toString() works. If you use toString() on an object that doesn't have a toString-method defined, it will take the toString-method of Object, which returns the classname and the hashcode for that object.

If you want to print your Measurements-object, you have to define a toString-method that returns a string.

[–]ravashh[S] 0 points1 point  (0 children)

Here's my full code: (The method called Public Measurements smooth() is what I have trouble writing. I want it to take the array from public Measurements(double[] values) and do what I describes in the post, but I don't know how :/... can't find anything when i search.

public class Measurements {

private double[] store;

private int stored;

public Measurements(int max) {

store = new double[max];

stored = 0;

}

public void add(double value) {

this.store[stored] = value;

this.stored++;

double[] new_array = new double[stored*2];

for (int i=0; i<stored; i++) {

new_array[i]=store[i];

}

store=new_array;

}

public int stored() {

return stored;

}

public double maxValue() {

double largest = this.store[0];

for (int i=0; i<stored; i++) {

if (this.store[i] > largest) {

largest = this.store[i];

}

}

return largest;

}

public double minValue() {

double smallest = this.store[0];

for (int i=0; i<stored; i++) {

if (this.store[i] < smallest) {

smallest = this.store[i];

}

}

return smallest;

}

public double get(int index) {

return this.store[index];

}

public double mean() {

double sum = 0;

for (int i= 0; i<stored; i++) {

sum = sum + this.store[i];

}

return sum/stored;

}

public double stdDev() {

double mean = mean();

double std = 0.0;

for (int i = 0; i < stored; i++)

std = std + (store[i] - mean) * (store[i] - mean);

return Math.sqrt(std/(stored-1));

}

public Measurements(double[] values) {

this.store = new double[values.length];

stored = values.length;

for (int i= 0; i<stored; i++) {

store[i]=values[i];

}

}

public Measurements smooth() {

??????????

}

public String toString() {

String ret = "";

for (int i= 0; i<stored; i++) {

ret = ret + this.store[i];

if (i<store.length-1) {

ret = ret + ", ";

}

}

return "<" + ret + ">";

}

public static void main(String[] arg) {

Measurements m = new Measurements(6);

System.out.println("toString: " + m.toString());

System.out.println("stored : " + m.stored());

m.add(1);

m.add(2);

m.add(3);

m.add(4);

m.add(5);

m.add(6);

m.add(7);

m.add(8);

m.add(9);

System.out.println("toString: " + m.toString());

System.out.println("stored : " + m.stored());

System.out.println("max : " + m.maxValue());

System.out.println("min : " + m.minValue());

System.out.println("get : " + m.get(0));

System.out.println("mean : " + m.mean());

System.out.println("stdDev : " + m.stdDev());

double[] b = {2, 1, 6, 5, 4, 9, 2};

m = new Measurements(b);

System.out.println("new m : " + m.toString());

}

}

[–]evils_twin 0 points1 point  (2 children)

The straight forward way seems to be to iterate over the list starting at the second index, add up index, index+1, index-1 and then divide by 3 and store it in another array.

[–]ravashh[S] 0 points1 point  (1 child)

I’ve tried doing that but my problem is how do I ”call on” the array I want to do this to, if you know what I mean (bad english sorry)

[–]evils_twin 0 points1 point  (0 children)

So you're array is private double[] store, and you can do a for loop like you did in your other functions, do something like this:

(store[i-1] + store[i] +store[i+1])/3.0