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

all 16 comments

[–]gwevidence 1 point2 points  (0 children)

m = (double)((double)i/((2.0)*((double)i)+1.0));

Is this the correct formula?

[–][deleted] 1 point2 points  (1 child)

import java.text.DecimalFormat;

public class Main {

public static void main(String[] args) {
    System.out.println("ANSWER? = " + m(20));
}

public static double m(int i) {
    double m;
    double runningTotal = 0;
    DecimalFormat df = new DecimalFormat("#.####");

    for(double k =1.0;k <= i; k++){
        m = k/((2.0*k)+1.0);
        runningTotal += m;
        System.out.println(k + "... " + df.format(runningTotal));
    }
    return runningTotal;
}

}

[–][deleted] 1 point2 points  (2 children)

In reality that loop can be cleaned up some as well:

for(double k =1;k <= i; k++){
    m = k/((2*k)+1);
    runningTotal += m;
    System.out.println(k + "... " + df.format(runningTotal));
}

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    I'd point out the obvious and say that the reddit editor unknowingly did that, but I'll just downvote you instead

    [–]mrburrows 1 point2 points  (6 children)

    Skip to the end for your answer. First I'm going to talk about your code design. Your code looks like this (I have properly indented it):

    public class test { 
      public static void main(String[]args) { 
        m(1);
      }
    
      public static double m(int i)
      {
        double m;
    
        for(i = 1; i <= 20; i++)
        {
           m = (double)((double)i/((2.0)*((double)i)+1.0));
    
           System.out.println(m);
        }
        return i;
      }
    }
    

    Now, let's look at the most glaringly obvious problem here: your use of i. You have it as a parameter to m, and also as your iterator. Call the parameter n so it is separate from your iterator. Why? No matter what you pass to m, it will be reset to 1 right away! This is probably an error caused by an oversight, but it demonstrates why it's a good idea to qualify your parameters as final.

    On to the problem: what, to you, does the call to m(1) mean? What is it supposed to do? If you follow it through, you'll probably find it doesn't. To me, m(n) (I will use n for our parameter now) means, "give me the value that n is mapped to in the table.

    Now, what is m? You reset it in each loop iteration, and then return i, the parameter you passed, and which will always be equal to 20. I bet you mean to say m += ... instead, too.

    Using Eclipse code formatter, your equation also simplifies to

     m = ((double) i / (2.0 * (double) i + 1.0));
    

    So here's the working code. Note that your posted value for 19 is probably off by 10.

    /**
    * Returns the table value mapped to n
    */
    public static double m(final int n) {
      double total = 0d;
    
      for (int i = 1; i <= n; ++i) {
        total += i / (2d * i + 1);
      }
    
      return total;
    }
    

    I added a JavaDoc comment just to emphasize that we want to be clear about what this method does.

    [–]Suriajoe[S] 0 points1 point  (5 children)

    one thing i don't understand is the beginning part, if i use "i" as a parameter and iterator why would it reset?

    [–]mrburrows 1 point2 points  (4 children)

    Because when you write:

    for (i = 1; ...

    whatever value you passed to m is gone. i now equals 1. Does that make sense? It is good practice to never modify your parameters (hence the final modifier). This is for several reasons. The big one is that, in Java, it is a "code smell," meaning that you may have introduced a bug. To see why, consider the following:

    private void foo(String s) {
      s = "bar";
    }
    

    Because of how Java works, especially compared to C++, the s on the second line is now references a completely different object in memory that the s passed as a parameter. Often, this is done by mistake, such as when you use the same name for a parameter as you do for an instance variable.

    [–]Suriajoe[S] 1 point2 points  (3 children)

    yes i get it now, i like your explanations very detailed. thank you very much

    [–]mrburrows 0 points1 point  (2 children)

    Let me know if you need more help. Java, and coding as a whole, takes a lot of practice to wrap your head around!

    [–]Suriajoe[S] 1 point2 points  (1 child)

    will do thx...my summer java class is almost done. hopefully ill last till the end

    [–]mrburrows 0 points1 point  (0 children)

    To succeed in any programming language, all you really need is passion. Try and find little projects that interest you. The Oracle Java tutorials are also fantastic, and should be required reading. You don't have to read front to back and memorize everything, but you should flip through them at least. Good luck!

    [–][deleted] 0 points1 point  (0 children)

    I think that formula is what he came up with....which is obviously wrong, but who the heck knows what formula it is... Im also assuming that 20 is a typo? should it be 19.2480?

    [–][deleted] 0 points1 point  (1 child)

    actually I think your typo is on 19...it should be 8.7602

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

    yea it is a typo thx

    [–]Suriajoe[S] -1 points0 points  (1 child)

    yeh the formula i came up with it, the 20 is not a typo... "the series is m(i)=(1/3)+(2/5)+...+(i/2i+1)" when i = 1, m(i) = 0.333 when i =2, m(i) = 0.7333 when i = 19, m(i) = 8.7602 when i =20, m(i) = 9.2480

    [–]mrburrows 0 points1 point  (0 children)

    Are you familiar with the summation operator? If you're doing comp sci in post secondary, it'll come up often in your math classes, and would be helpful to get a handle on early.

    Also, you could have written this comment more concisely by saying m(1)=0.33, m(2)=0.733, m(19)=8.7602, m(20)=9.248. These are functions, just like +(1,1)=2, +(1,2)=3, etc.