/*
	DynamicCorr applet takes pairs of numbers from a HTML
	TEXTAREA FORM field, and computes Means and R square
	Version 1.0 - 13 Aug 1999 Chris Thiel <cct@ktb.net>
			2.0 - 25 Aug enhanced error messages
*/

import java.lang.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.util.*;

public class DynamicCorr extends Applet
{
	int MAX=50;
	int n=0;
	String userInput=null;
	String s=null;
	String message=null;
	float x[] = new float[MAX];
	float y[] = new float[MAX];
	float xMean, yMean;
    float B0, B1, R_square,Var_x,Var_y;

	int i=0;
	
/*
	Compute is the routine called to compute the Statistical Data
*/	

	public void  compute(){
	// Now calculate regression coeffecients
    	float Sx = 0, Sy =0;
    	float Sxx = 0, Sxy = 0, Syy = 0;
    	

    	for(int i=0; i< n; i++)
    	{
    		
      		Sx += x[i]; 
      		Sy += y[i];
    	}
    	xMean = Sx / n;
    	yMean = Sy / n;
    	for(int i=0; i< n; i++)
    	{
      		Sxx += (x[i]-xMean) * (x[i]-xMean);
      		Sxy += (x[i]-xMean) * (y[i]-yMean);
      		Syy += (y[i]-yMean) * (y[i]-yMean);
    	}
    	B1=Sxy/Sxx;
    	B0 = yMean - B1 * xMean;
    	R_square=(Sxy*Sxy)/(Sxx*Syy);
	}
	
 /**parse the user's input String from an HTML document */
    public void setString (String userInput) {
	i=0; //reset number of pairs
	message=null; //reset error message
	//convert new line chars from unix/win
	userInput.trim(); 
	while (userInput.indexOf("\n") >=0) {
	   userInput= 
	     userInput.substring(0, userInput.indexOf("\n")) + " ; " +
	     userInput.substring(userInput.indexOf("\n")+1);
	}
	//convert return chars from macOS 
	while (userInput.indexOf("\r") >=0) {
	   userInput= 
	     userInput.substring(0, userInput.indexOf("\r")) + " ; " +
	     userInput.substring(userInput.indexOf("\r")+1);
	}
	if ( !(userInput.endsWith(" ; "))) userInput = userInput+" ; ";
	StringTokenizer t= new StringTokenizer(userInput, " ", false);
	
	// Now that the lines are cleaned up read in pairs  
	while ((t.hasMoreTokens()) && (i<MAX)) {
	  try{
	  	s = t.nextToken();
	  	if (s.charAt(0)!=';'){	
	  	 x[i]=Float.valueOf(s).floatValue(); 
      	 y[i]=Float.valueOf(t.nextToken()).floatValue(); 
      	 if (t.hasMoreTokens() ) s=t.nextToken();// the ; separator
	  	 i++;
	  	 }//end of if
	  	}// end of try
	  catch(Exception e) {
	    //message = e.toString();
	    message = "Line "+(i+1)+" around \""+s+"\"";
	  }//end of catch
	}//end of while loop
	n = i;
	if (n > 0) {compute();}
	repaint();
    }// of parsing the userInput string
    
    public void init() {
    	i =0;
		repaint();
	}
	
	public void paint( Graphics g ) {
		
		g.drawString( "Number of Pairs Found:"+n, 30, 15 );
		if (n == MAX) {
			g.drawString( "WARNING: Maximum number of Pairs found", 5,size().height-48);
		 	g.drawString( "subsequent pairs ignored", 35,size().height-36);
		 	}
		g.drawString("Regression Line Equation:",25,30);
    	g.drawString("y  = "+B1+" * x + "+B0,25,45);
    	g.drawString("r   = "+R_square,25,60); 
    	g.drawString("^",25,43);
    	g.drawString("_",25,81);
    	g.drawString("_",25,96);
    	g.drawString("2",30,57);
    	g.drawString("x = "+xMean,25, 90);
		g.drawString("y = "+yMean,25, 105);
		if (message != null) {
	    	g.drawString("Error in data: "+message, 10, size().height-24);
	    	g.drawString("Two numbers per line separated by space", 10, size().height-12);

			}
		}

}    
    