/* StatCalc --- a java applet for doing simple statistical calculations Copyright (C) 1998, Stephan Pelikan This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Contact Information: Stephan Pelikan 8 Tanglewood Lane Cincinnati, Ohio 45224-2823 (513) 681-2574 or at work: (pelikan@math.uc.edu) Steve Pelikan Mathematical Sciences University of Cincinnati Cincinnati, OH 45221-0025 (513) 556-4084 */ import java.applet.Applet; import java.util.*; import java.awt.*; import java.lang.Float; import java.lang.Math; public class StatCalc extends Applet { String Buffer; TextArea ta; Button button,button1,button2,button3,button4,button5,button6,button7,button8; Panel panel,bottom_panel; float data[]; GridBagLayout gridbag= new GridBagLayout(); private String Round(float x,int decimals) { double xx=x; double factor=Math.pow(10.0,(double)decimals); double temp=Math.floor(factor*xx+0.5)/factor; String ans=""+temp; return ans; } private String Round(double x,int decimals) { double xx=x; double factor=Math.pow(10.0,(double)decimals); double temp=Math.floor(factor*xx+0.5)/factor; String ans=""+temp; return ans; } private void bubble(float d[],int n) { int j,pass,switched=1; float temp; for(pass=0;passd[j+1]) { switched=1; temp=d[j]; d[j]=d[j+1]; d[j+1]=temp; } } } } public void init() { ta = new TextArea("",8,60); ta.setFont(new Font("Courier",Font.PLAIN,12)); button = new Button("Compute"); button1 = new Button("Sort"); button2= new Button("Clear"); button3 = new Button("Histogram"); button4=new Button("Regression"); button5=new Button("ChiSquare"); button6=new Button("Copy"); button7=new Button("Paste"); button8=new Button("tTest"); panel=new Panel(); bottom_panel=new Panel(); panel.setLayout(gridbag); bottom_panel.setLayout(gridbag); /* constrain(panel,button,0,0,1,1); constrain(panel,button1,1,0,1,1); constrain(panel,button2,2,0,1,1); constrain(panel,button3,3,0,1,1); constrain(panel,button4,4,0,1,1); constrain(panel,button5,5,0,1,1); constrain(panel,button6,6,0,1,1); constrain(panel,button7,7,0,1,1); constrain(panel,button8,8,0,1,1); */ //constrain(panel,button2,0,0,1,1); //constrain(panel,button6,1,0,1,1); constrain(panel,button,0,0,1,1); constrain(panel,button3,1,0,1,1); constrain(panel,button4,4,0,1,1); //constrain(panel,button7,0,1,1,1); constrain(panel,button5,3,0,1,1); constrain(panel,button1,2,0,1,1); constrain(panel,button8,5,0,1,1); constrain(bottom_panel,button2,0,0,1,1); constrain(bottom_panel,button6,1,0,1,1); constrain(bottom_panel,button7,2,0,1,1); this.setLayout(gridbag); constrain(this,panel,0,0,1,1); constrain(this,ta,0,1,1,1); constrain(this,bottom_panel,0,2,1,1); } public boolean action(Event e, Object arg) { if(arg.equals("Copy")) Buffer=ta.getText(); if(arg.equals("Paste")) ta.setText(Buffer); if(arg.equals("ChiSquare")) { int i,j; String mydata= ta.getText(); StringTokenizer ST=new StringTokenizer(mydata,"\n"); int ROWS= ST.countTokens(); ta.setText("ROWS="+ROWS); //If ROWS <=1 explain String CurrentLine= ST.nextToken(); StringTokenizer STT=new StringTokenizer(CurrentLine," \t,"); int COLS= STT.countTokens(); float data[][]=new float[ROWS+1][COLS+1]; for(i=0;i 0) { i=0; m=0; mm=0; while(ST.hasMoreTokens()) { temp=ST.nextToken(); mt =Float.valueOf(temp).floatValue(); data[i++]=mt; m+= mt; mm += mt*mt; } bubble(data,n); temp=""; for(i=0;i 0) { i=0; m=0; mm=0; while(ST.hasMoreTokens()) { temp=ST.nextToken(); mt =Float.valueOf(temp).floatValue(); data[i++]=mt; m+= mt; mm += mt*mt; } bubble(data,n); //now make histogram // range = data[n-1]-data[0] if(n < 60) BINS=5; else BINS = 7; step = (data[n-1]-data[0])/BINS; temp="\n"; j=0; for(i=1;i<=BINS;i++) { while( j < n && data[j] <= data[0]+i*step) { temp = temp + "x"; j++; } mylabel=""; if(i==1) mylabel = mylabel +"["+ (data[0]+(i-1)*step) + "-" +(data[0]+i*step)+"]"; else mylabel = mylabel +"("+ (data[0]+(i-1)*step) + "-" +(data[0]+i*step)+"]"; temp = temp + " "+ mylabel + "\n"; } //for(i=0;i 0) { i=0; m=0; mm=0; while(ST.hasMoreTokens()) { temp=ST.nextToken(); mt =Float.valueOf(temp).floatValue(); data[i++]=mt; m+= mt; mm += mt*mt; } d = (n*mm - m*m)/(n*(n-1)); d=Math.sqrt(d); m = m/n; bubble(data,n); f=n; //Find median; median rank is (n+1)/2 //And the corresponding value is located at (n+1)/2 -1 =(n-1)/2 //or is the average of (n-1)/2 \pm (1/2) = n/2-1 and n/2 if(f/2 == Math.floor(f/2)) {med=(data[n/2-1]+data[n/2])/2;} else {med= data[(n-1)/2];} //The quartile rank is (Integer(median rank) +1)/2 double quartilerank= (Math.floor((f+1)/2)+1)/2; //if quartilerank is an integer, subtract 1 to get the data values' index if(Math.floor(quartilerank)==quartilerank) { q1= data[(int)quartilerank-1]; q3= data[n-(int)quartilerank]; } else { q1=(data[(int)(quartilerank-1.5)]+ data[(int)(quartilerank-0.5)])/2; q3=(data[n-(int)(quartilerank-0.5)]+ data[n-(int)(quartilerank+0.5)])/2; } tmp=Round(d,4); ta.setText(mydata+"\nN is: "+n+"\nMean is: "+m+"\nStd Dev is: " + tmp +"\n\nQ1 is: "+q1+"\nMedian is: "+med+"\nQ3 is: "+ q3+"\n"); } return true;} else if(arg.equals("Clear")){ ta.setText(""); return true; } return false; } public void start() { } public void stop() { } public void destroy() { } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints(); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+bottom+left+right > 0) c.insets = new Insets(top, left, bottom, right); ((GridBagLayout)container.getLayout()).setConstraints(component, c); container.add(component); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, 0, 0, 0, 0); } public void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int top, int left, int bottom, int right) { constrain(container, component, grid_x, grid_y, grid_width, grid_height, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.0, 0.0, top, left, bottom, right); } }