creating a method in using a (sum series) Write a method to compute the following series

StackOverflow https://stackoverflow.com/questions/21592809

  •  07-10-2022
  •  | 
  •  

Question

Then the output looks as followed:

i                         m(i)
1                         0.5
2                         1.1667
... 
19                        16.4023
20                        17.3546




/**
 * Write a description of class fiveonethree here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class homework
{
    public static void main( String args[] )
   {
      double value = 0;
      System.out.println("i" + "\t\t" +  "m(i)");
      System.out.println("-------------------");
      for( int i=0; i < 21; i++)
      {
         System.out.println( i + " \t\t " + value + "\n" );
         value += ((double) i + 1)/(i+2);
      }
   }
}

I have my output correct and everything, but my professor wants it in a method. I find methods difficult, so I created it with a simple for loop. The directions are: Write a method to compute the following series: 1/2+2/3.......+i/i+1

Was it helpful?

Solution

I'm guessing he/she wants a method that looks like

public double theSum(int n) {
    ...
}

where n is the number of terms (so that the last term is n/(n+1)). Then the main method will call theSum(??) with some value as the parameter that will be used for n. Of course that will mean you're doing extra computation to print the table, but don't worry about that. (Maybe you should call it m instead of theSum since that what they called the function. I just don't like one-letter function names.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top