Requirement 1) Modify the code to find and print sum of the same series from 1/2 through 1/n multiple times, where each n is read from data file. Requires nested loop with the inner loop summing up series and outer loop allow inner code to repeat once for each n read in from file. Loop until file EOF is reached.
Requirement 2) Modify the modified code. Write a 2nd method for class called summation. This method, which will be called by your main method, will receive the value of n from main, will sum from 1/2 through 1/n and will return the answer to main. Function header for new function: public static double summation (int n). Body of summation method will contain statements to do summation moved from main. Either have main method or summation print summation terms, but main must be one to print the answer returned to it by summation method.
Thanks in advance
My Code so far...
import java.io.*;
import java.util.Scanner;
public class Lab5A{
public static void main(String[] args) throws FileNotFoundException
{
File inputFile = new File("Series2.dat");
Scanner file = new Scanner(inputFile);
int maxDen=file.nextInt();
int den;
double sum = 0;
System.out.print("The summation of the series " );
for (den=2; den<=maxDen; den*=2)
{
System.out.print("1/" + den);
if (den < maxDen)
System.out.print(" + ");
else System.out.print(" = ");
// Get the sum so far.
sum = sum + (1.0/den);
}
System.out.print(sum);
}
}