import javagently.*;

import java.io.*;

class Integrator

{

public static void main(String args[])

{

ReadFile();

RectangleIntegrate(ReadFile.x[i], ReadFile.y[i], ReadFile.N);

TrapezoidIntegrate(ReadFile.x[i], ReadFile.y[i], ReadFile.N);

SimpsonIntegrate(ReadFile.x[i], ReadFile.y[i], ReadFile.N);

LeastSquares(ReadFile.x[i], ReadFile.y[i], ReadFile.N);

/* Graph(ReadFile.x[i], ReadFile.y[i], ReadFile.N, LeastSquares.m, LeastSquares.b); */

WriteFile(ReadFile.outFile, RectangleIntegral.rectangle, TrapezoidIntegral.trapezoid, SimpsonIntegral.simpson, LeastSquares.m, LeastSquares.b);

}

public static void ReadFile() throws IOException

{

Display display = new Display("Data files");

display.prompt("Input file name","input.data");

display.prompt("Output file name","output.data");

display.ready("Click to begin!");

String inFilename = display.getString("Input file name");

String outFilename = display.getString("Output file name");

BufferedReader fin = Text.open(inFilename);

int N = Text.readInt(fin);

double x[] = new double[N+1];

double y[] = new double[N+1];

for (int i = 1; i <= N; i++)

{x[i] = Text.readDouble(fin); }

for (int i = 1; i <= N; i++)

{y[i] = Text.readDouble(fin); }

}

public static double RectangleIntegrate(double x[], double y[], int N)

{

double width = 0, height= 0, SubArea= 0, trapezoid = 0;

for (int i = 1; i <= N; i++)

{

width = x[i+1] - x[i];

height = (y[i] + y[i+1])/2;

SubArea = width * height;

trapezoid += SubArea;

System.out.println("Trapezoid integral: " + trapezoid);

}

}

public static double TrapezoidIntegrate(double x[], double y[], int N)

{

double width = 0, height= 0, SubArea= 0, trapezoid = 0;

for (int i = 1; i <= N; i++)

{

width = x[i+1] - x[i];

height = (y[i] + y[i+1])/2;

SubArea = width * height;

trapezoid += SubArea;

System.out.println("Trapezoid integral: " + trapezoid);

}

}

public static double SimpsonIntegrate(double x[], double y[], int N)

{

double width = 0, height = 0, SubArea = 0, TotArea = 0;

for(int i = 2; i <= N - 1; i = i+2)

{

width = (x[i+1] - x[i-1])/3;

height = y[i-1] + (4 * y[i]) + y[i+1];

SubArea = width * height;

TotArea += SubArea;

System.out.println(TotArea);

}

}

public static void LeastSquares(double x[], double y[], int N)

{

double sumX = 0, sumY = 0, sumX2 = 0, sumXY = 0, m = 0, b = 0;

for(int i = 1; 1 <= N; i++)

{

sumX += x[i];

sumY += y[i];

sumX2 += x[i] * x[i];

sumXY += x[i] * y[i];

}

m = ((sumX * sumXY) - (sumX2 * sumY)) / ((sumX * sumX) - (N * sumX2));

b = ((sumX * sumY) - (N * sumXY)) / ((sumX * sumX) - (N * sumX2));

}

/* Graph goes here */

static void WriteFile(String outfile, double rectangle, double trapezoid, double simpson, double m, double b) throws IOException

{

display.println("Writing to file " + outFilename);

PrintWriter fout = Text.create(outFilename);

fout.println("The rectangular integral is " + rectangle);

fout.println("The trapezoidal integral is " + trapezoid);

fout.println("The Simpson's rule integral is " + simpson);

fout.println("The best-fit line is: f(x) = " + m + "x + " + b);

fout.close();

}

}