Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Exercise 147

So here is the deal:

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();

for ( double x : q )
{
e.add ( new Double ( x ) );
}

double dTotal = 0.0,
eTotal = 0.0;

for (double c : d)
{
double a += c;
}

for (double f : e)
{
double b += f;
}

return (dTotal / d.length);
return (eTotal / e.length);

--- Update ---

I switched it a little bit. It still gives me the same error:

for (double c : d)

{

dTotal += c;

}

for (double f : e)

{

double eTotal += f;

}

System.out.print(dTotal / d.size());

System.out.print(eTotal / e.size());

--- Update ---

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

2. ## Re: Exercise 147

Hi. The problem in your code:

double eTotal += f;

You should remove the 'double' for 'eTotal' as it has already been declared in

double dTotal = 0.0, eTotal = 0.0;

Here's the working code:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();

for (double x : q) {
}

double dTotal = 0.0, eTotal = 0.0;

for (double c : d) {
dTotal += c;
}

for (double f : e) {
// double eTotal += f;
eTotal += f;
}

System.out.println(dTotal / d.size());
System.out.println(eTotal / e.size());

It prints the mean as 1.3800000000000003.

3. ## Re: Exercise 147

Thanks. It worked.