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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exercise 147

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 )
        {
          d.add( x );
          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.
    Last edited by ghostheadx; February 23rd, 2014 at 03:15 PM. Reason: fixing syntax


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default 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) {
    	d.add(x);
    	e.add(new Double(x));
    }
     
    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. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 147

    Thanks. It worked.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM

Tags for this Thread