standard deviation using linked lists

hi, if anybody couold help me out with this Id be really grateful. I need the code for standard deviation using linked lists.I dont have a clue about linked lists and my assignment is due Tuesday!!!Thanks again,Niamh
[237 byte] By [niamhgurla] at [2007-9-29 13:21:02]
# 1

I hate being conned into doing homework for students, but I'm going to do it here because I want those ten Dukes.

This works:

import java.util.*;

public class Deviator

{

public static void main(String [] args)

{

if (args.length > 0)

{

Deviator deviator = new Deviator();

List values= deviator.getValues(args);

double mean= deviator.calculateMean(values);

double stddev= deviator.calculateStandardDeviation(values);

System.out.println("values: " + values);

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

System.out.println("stddev: " + stddev);

}

else

{

System.out.println("No arguments");

}

}

public List getValues(final String [] args)

{

List values = new ArrayList();

for (int i = 0; i < args.length; ++i)

{

try

{

Double value = Double.valueOf(args[i]);

values.add(value);

}

catch (NumberFormatException e)

{

System.err.println("Not a number: " + args[i]);

}

}

return values;

}

public double calculateMean(final List values)

{

double mean = 0.0;

int numValues = values.size();

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

mean += ((Double)values.get(i)).doubleValue();

return mean/numValues;

}

public double calculateStandardDeviation(final List values)

{

double mean = this.calculateMean(values);

double sum = 0.0;

int numValues = values.size();

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

{

double value = ((Double)values.get(i)).doubleValue();

double diff = value - mean;

sum += diff*diff;

}

return Math.sqrt(sum/(numValues - 1));

}

}

Compile it and run it by typing this in a command shell, navigating down the directory where Deviator.class is:

java -classpath . Deviator 1 2 3 4 5 6 7 8 9 10

Put the values you want, as many as you want and they don't have to be integers, on the command line. This example gave a mean of 5.5 and standard deviation of 3.02765, as it should.

Make sure you award those ten Dukes. - MOD

duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 2
Ooops.Replace:List values = new ArrayList();with:List values = new LinkedList();And you'll fulfill the linked list requirement. ;-)
rkconnera at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 3

Thanks for the correction, rkconner. Right as usual.

Of course, the way the calculation methods are written they'll work with ANY java.util.List. The most general thing would have been to write them in terms of java.util.Collection:

import java.util.Collection;

import java.util.Iterator;

import java.util.LinkedList;

public class Deviator

{

public static void main(String [] args)

{

if (args.length > 0)

{

Deviator deviator = new Deviator();

Collection values = deviator.getValues(args);

double mean= deviator.calculateMean(values);

double stddev= deviator.calculateStandardDeviation(values);

System.out.println("values: " + values);

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

System.out.println("stddev: " + stddev);

}

else

{

System.out.println("No arguments");

}

}

public Collection getValues(final String [] args)

{

Collection values = new LinkedList();

for (int i = 0; i < args.length; ++i)

{

try

{

Double value = Double.valueOf(args[i]);

values.add(value);

}

catch (NumberFormatException e)

{

System.err.println("Not a number: " + args[i]);

}

}

return values;

}

public double calculateMean(final Collection values)

{

double mean= 0.0;

if (values.size() > 0)

{

int numValues = 0;

for (Iterator iter = values.iterator(); iter.hasNext(); ++numValues)

mean += ((Double)iter.next()).doubleValue();

mean /= numValues;

}

return mean;

}

public double calculateStandardDeviation(final Collection values)

{

double dev= 0.0;

if (values.size() > 1)

{

double mean= this.calculateMean(values);

double sum= 0.0;

int numValues= 0;

for (Iterator iter = values.iterator(); iter.hasNext(); ++numValues)

{

double value = ((Double)iter.next()).doubleValue();

double diff = value - mean;

sum += diff*diff;

}

dev = Math.sqrt(sum/(numValues - 1));

}

return dev;

}

}

I think this code is much improved. It's more general, because it uses java.util.Collection, and it guards against passed data that would cause it to blow up (e.g., a Collection with zero elements in it). Thanks for making me think again, rkconner. - MOD

duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 4
Man, this student is going to be scamming some FINE code. I hope s/he comes across with the Dukes. - MOD
duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 5

Thanks for the code, As you can see I didn't remit on the dukes.I dont see whats wrong with geting help for assignments from people who know what their at.Im in a crappy course that is making us program when I know I wont do it as a full time job. Ok, so maybe it's cheating the degree, but surly you can see the logic that if Im not going to be doing it as a living,why should I bother?

oh yeah, Im a girl by the way!

niamhgurla at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 6

Thanks for the Dukes, niamhgurl. I'm glad to see that you held up your end of the bargain, anyway.

Help is one thing, but having someone do it for you and then passing it off to your professor as your own is quite another. What would the prof think if s/he found out that the code you turn in wasn't yours? What about future assignments? Will you be able to match the quality and inimitable style of my work? :) I might not be seduced by Dukes next time.

Sounds like the same rationalization that convinces some folks that downloading music from Napster is somehow different from lifting a CD at a record store.

Maybe you won't make a living as a programmer, but your integrity stays with you wherever you go. Your loss. - MOD

duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 7

> Sounds like the same rationalization that convinces

> some folks that downloading music from Napster is

> somehow different from lifting a CD at a record

> store.

It is a bit different. Through Napster the artist and

record company miss out on royalties (which they might

not have had anyway--people might download what they

wouldn't have otherwise bought). Lifting the CD also

costs the shop money. Not that I'm defending downloading

through Napster, but some offences _are_ worse than others.

As far as the main discussion goes, I'm with you 100%.

Anyway who knows what they might be doing in 10 year's

time. Even if you never use linked lists (and the Java

collections framework means that it can be done for you

much of the time) it's a good exercise in thinking things

through. Any work that requires a university education

is going to require you to be able to think.

TerryMoorea at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 8

No doubt, TerryMoore. Downloading from Napster isn't murder. But I still don't think it's much different from shoplifting.

Not following your argument here either, TM. She DIDN'T think anything through, beyond finding this forum. It's not an intellectual issue, it's about integrity.

I suppose that while I'm on my high horse I should slap myself. I'm an enabler by providing code to her. I should have resisted those 10 Dukes and blown her off. I will from now on. - MOD

duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 9

> No doubt, TerryMoore. Downloading from Napster isn't

> murder. But I still don't think it's much different

> from shoplifting.

I agree it's not much different. But more people get

hurt from shoplifting, the shop pays actual cash for

it's wholesale goods, that's all.

> Not following your argument here either, TM. She

> DIDN'T think anything through, beyond finding this

> forum. It's not an intellectual issue, it's about

> integrity.

Here I was agreeing with you. I was saying to her (if

she's still listening) that studying herself is

valuable for her own intellectual development.

> I suppose that while I'm on my high horse I should

> slap myself. I'm an enabler by providing code to her.

> I should have resisted those 10 Dukes and blown her

> off. I will from now on. - MOD

Don't worry, she won't get many marks for it. I think

she was meant to design her own linked lists, not use

those in the collections class.

TerryMoorea at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 10
hi jay here i tried out that program on link lists that ye did out for niamhgurl as i find out that their is a problem with it as it is not outputting the message to the screen, is this the only part of the program and if not where is the rest of it yours truely jay
jmcgrath123a at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...
# 11

You're an eejit, jmcgrath123.

Did you look at the code at all before you ran it? Did you see that it expects you to supply values on the command line so it can calculate the mean and standard deviation? What values did you give it? If none, then it did nothing!

The program works perfectly. It's your brain that's defective. - MOD

duffymoa at 2007-7-15 3:35:07 > top of Java-index,Other Topics,Algorithms...