/*
	Sample application using System.in and Text class
	cucs  hp  7/97, 8/97, 1/98, 3/98, 6/98	
*/

import java.io.*;

public class makeup_hw
{
    
    public static void main(String args[])
    {
	double x, y, ub, lb, step;
	int n;
	int simp_count = 1;
	double trap_sum = 0;
	double simp_sum = 0;
	
	// initialize Text object in to read from standard input.
	TokenReader in = new TokenReader(System.in);
	
	System.out.print("Please enter an integer: ");
	
	// prompt user to input range of integration
	System.out.print("Please enter lower bound: ");
	System.out.flush();
	lb = in.readDouble();
	System.out.print("Please enter upper bound: ");
	System.out.flush();
	ub = in.readDouble();
	System.out.print("Please enter number of steps (must be even): ");
	System.out.flush();
	n = in.readInt();
	
	// check that n is even
	while(n%2 != 0){
	    System.out.println("n is not even!");
	    System.out.print("Please enter number of steps (must be even): ");
	    System.out.flush();
	    n = in.readInt();
	}
	
	// calculate the step for the loop
	step = (ub-lb)/n;
	
	// initialize y0 (not multiplied by constants)
	x=lb;
	trap_sum = Math.sqrt(Math.tan(x));
	x+=step;
	
	// calculate y1 to yn-1
	while(simp_count < n){
	    y = Math.sqrt(Math.tan(x));
	    x += step;
	    y *= 2;
	    trap_sum += y;
	    
	    // for simpson's, need 4*(odd occurance of y)
	    if(simp_count%2!=0){
		y *= 2;
	    }
	    System.out.println("y:  " + y);
	    simp_count++;
	    simp_sum += y;
	}
	
	// now calculate yn (not multiplied, again)
	y = Math.sqrt(Math.tan(x));
	trap_sum += y;
	trap_sum = (step * trap_sum) / 2.0;
	simp_sum += y;
	simp_sum = (step * simp_sum) / 3.0;
	
	System.out.println("The answer (trapzoidal) is:  " + trap_sum);
	System.out.println("The answer (Simpson's) is:  " + simp_sum);
	System.out.println("step is:  " + step);
	System.out.println("simp_count is:  " + simp_count);
	
	// Wait for user to enter input to ensure console window remains visible
	in.waitUntilEnter();
    }
}


