Created
October 25, 2012 10:51
-
-
Save TabsPH/3951985 to your computer and use it in GitHub Desktop.
Testing the Correlation of the two variables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Project Title: Correlation | |
* Author: Alvin Tabontabn | |
*/ | |
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.*; | |
import javax.swing.border.*; | |
import javax.swing.table.*; | |
import java.util.*; | |
import java.io.*; | |
public class correlation extends JFrame implements ActionListener { | |
JButton[] proceedB = new JButton[ 3 ]; | |
JButton computeB = new JButton(); | |
JButton resetB = new JButton(); | |
JLabel[] lblCaptions = new JLabel[ 3 ]; | |
JLabel lblVar1, lblVar2, lblResult; | |
JTable table = new JTable(); | |
Vector row = new Vector(); | |
Vector column = new Vector(); | |
DefaultTableModel tabModel; | |
JScrollPane spane; | |
JTextField[] tfInput = new JTextField[ 3 ]; | |
JPanel pneInput = new JPanel(); | |
JPanel pneTable = new JPanel(); | |
JPanel pneResult = new JPanel(); | |
JPanel pneBack; | |
Border bdrInput, bdrTable, bdrResult; | |
JTable tbl; | |
File imgPath = new File("main.jpg"); | |
String[] strCaptions = { "How Many Inputs:", | |
"Variable One:", | |
"Variable Two:" }; | |
int ctrInput = 0; | |
boolean fVar = false; | |
boolean sVar = fVar; | |
public static void main(String[] args){ | |
try{ | |
new correlation(); | |
} catch(Exception e){ | |
} | |
} | |
public correlation(){ | |
super( "CORRELATION" ); | |
if( imgPath.exists() ){ | |
setBorder(); | |
setJLabel(); | |
setJTextField(); | |
setJButton(); | |
setJPanel(); | |
setContentPane(); | |
setJFrame(); | |
}else{ | |
JOptionPane.showMessageDialog(correlation.this, "Application stop initializing due to the following error: \n" + | |
" cant find \"main.jpg\""); | |
System.exit(0); | |
} | |
} | |
private void setBorder(){ | |
bdrInput = BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), | |
" INPUTS ", | |
TitledBorder.CENTER, | |
TitledBorder.CENTER, | |
new Font( "Courier", Font.BOLD, 15), | |
Color.ORANGE ); | |
bdrTable = BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), | |
" TABLE ", | |
TitledBorder.CENTER, | |
TitledBorder.CENTER, | |
new Font( "Courier", Font.BOLD, 15), | |
Color.ORANGE ); | |
bdrResult = BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), | |
" CORRELATION RESULT ", | |
TitledBorder.CENTER, | |
TitledBorder.CENTER, | |
new Font( "Courier", Font.BOLD, 15), | |
Color.ORANGE ); | |
} | |
private void setJTable(){ | |
tabModel = new DefaultTableModel(); | |
tabModel.setDataVector( row, column ); | |
table = new JTable(tabModel); | |
spane = new JScrollPane( table ); | |
spane.setBounds(10, 20, 245, 200); | |
pneTable.add( spane ); | |
} | |
private void setJLabel(){ | |
int yAxis = 20; | |
lblResult = new JLabel( "" , SwingConstants.CENTER ); | |
lblResult.setBounds( 30, 25, 520, 60 ); | |
lblResult.setFont( new Font("Courier", Font.BOLD, 13)); | |
lblResult.setForeground( Color.WHITE ); | |
lblResult.setOpaque( false ); | |
pneResult.add( lblResult ); | |
for(int i=0; i<lblCaptions.length; i++){ | |
lblCaptions[ i ] = new JLabel( strCaptions[ i ] ); | |
lblCaptions[ i ].setBounds( 10, yAxis, 150, 30 ); | |
lblCaptions[ i ].setFont( new Font("Courier", Font.BOLD, 12)); | |
lblCaptions[ i ].setForeground( Color.WHITE ); | |
lblCaptions[ i ].setOpaque( false ); | |
pneInput.add( lblCaptions[ i ] ); | |
yAxis += 50; | |
} | |
} | |
private void setJTextField(){ | |
int yAxis = 50; | |
for(int i=0; i<tfInput.length; i++){ | |
tfInput[ i ] = new JTextField(); | |
tfInput[ i ].setBounds( 10, yAxis, 180, 22 ); | |
tfInput[ i ].setFont( new Font("Courier", Font.PLAIN, 12)); | |
tfInput[ i ].setHorizontalAlignment( JTextField.CENTER ); | |
pneInput.add( tfInput[ i ] ); | |
yAxis += 50; | |
} | |
} | |
private void setJButton(){ | |
int yAxis = 50; | |
for(int i=0; i<proceedB.length; i++){ | |
proceedB[ i ] = new JButton( "PROCEED" ); | |
proceedB[ i ].setBounds( 200, yAxis, 90, 20 ); | |
proceedB[ i ].addActionListener( this ); | |
pneInput.add( proceedB[ i ] ); | |
yAxis += 50; | |
} | |
proceedB[ 1 ].setEnabled( false ); | |
proceedB[ 2 ].setEnabled( false ); | |
computeB = new JButton( "Perform Correlation" ); | |
computeB.setBounds( 10, 180, 140, 35 ); | |
computeB.addActionListener( this ); | |
computeB.setEnabled( false ); | |
resetB = new JButton( "New Correlation" ); | |
resetB.setBounds( 150, 180, 140, 35 ); | |
resetB.addActionListener( this ); | |
pneInput.add( computeB ); | |
pneInput.add( resetB ); | |
} | |
private void setJPanel(){ | |
pneInput.setLayout( null ); | |
pneInput.setBounds( 10, 100, 305, 230 ); | |
pneInput.setBorder( bdrInput ); | |
pneInput.setOpaque( false ); | |
pneTable.setLayout( null ); | |
pneTable.setBounds( 320, 100, 265, 230 ); | |
pneTable.setBorder( bdrTable ); | |
pneTable.setOpaque( false ); | |
pneResult.setLayout( null ); | |
pneResult.setBounds( 10, 330, 575, 100 ); | |
pneResult.setBorder( bdrResult ); | |
pneResult.setOpaque( false ); | |
pneBack = new JPanel(){ | |
public void paint(Graphics g){ | |
ImageIcon image = new ImageIcon( imgPath.getAbsolutePath() ); | |
g.drawImage( image.getImage(), 0, 0, null, null ); | |
super.paint( g ); | |
} | |
}; | |
pneBack.setLayout( null ); | |
pneBack.setBounds( 0, 0, 600, 480 ); | |
pneBack.setOpaque( false ); | |
pneBack.add( pneInput ); | |
pneBack.add( pneTable ); | |
pneBack.add( pneResult ); | |
} | |
private void setContentPane(){ | |
Container c = getContentPane(); | |
c.add( pneBack ); | |
} | |
private void setJFrame(){ | |
setSize( 600, 480 ); | |
setVisible( true ); | |
setResizable( false ); | |
setLocationRelativeTo( null ); | |
setDefaultCloseOperation( EXIT_ON_CLOSE ); | |
} | |
public void actionPerformed( ActionEvent ae ){ | |
try{ | |
if (ae.getSource() == proceedB[0] ){ | |
ctrInput = Integer.parseInt( tfInput[0].getText() ); | |
tfInput[0].setEditable( false ); | |
proceedB[0].setEnabled(false); | |
proceedB[1].setEnabled(true); | |
} | |
else if (ae.getSource() == proceedB[1] ){ | |
if( !tfInput[1].getText().trim().equals("") ){ | |
column.addElement( tfInput[1].getText() ); | |
proceedB[1].setEnabled(false); | |
proceedB[2].setEnabled(true); | |
tfInput[1].setEditable( false ); | |
fVar = true; | |
} | |
}else if (ae.getSource() == proceedB[2] ){ | |
if( !tfInput[2].getText().trim().equals("") ){ | |
column.addElement( tfInput[2].getText() ); | |
proceedB[2].setEnabled(false); | |
tfInput[2].setEditable( false ); | |
sVar = true; | |
setJTable(); | |
row.clear(); | |
computeB.setEnabled( true ); | |
resetB.setEnabled( false ); | |
for(int i=0; i<ctrInput; i++){ | |
Vector r = new Vector(); | |
r = createBlankElement(); | |
row.addElement(r); | |
} | |
} | |
} | |
else if( ae.getSource() == resetB ){ | |
int ans = JOptionPane.showConfirmDialog(correlation.this, "Are you sure?", "New Correlation",JOptionPane.YES_NO_OPTION); | |
if( ans == 0 ){ | |
new correlation(); this.dispose(); | |
} | |
} | |
else if( ae.getSource() == computeB ){ | |
if( computeB.getActionCommand().equals("Perform Correlation")){ | |
double[] X = new double[ ctrInput ]; | |
double[] Y = new double[ ctrInput ]; | |
double[] X2 = new double[ ctrInput ]; | |
double[] Y2 = new double[ ctrInput ]; | |
double[] XY = new double[ ctrInput ]; | |
//---------------------- Getting the Value from the JTable ----------------------------// | |
for(int i=0; i<ctrInput; i++){ | |
String strX = table.getValueAt(i, 0).toString(); | |
String strY = table.getValueAt(i, 1).toString(); | |
X[ i ] = Double.parseDouble( strX.trim() ); | |
Y[ i ] = Double.parseDouble( strY.trim() ); | |
XY[ i ] = ( X[ i ] * Y[ i ] ); | |
X2[ i ] = X[ i ] * X[ i ]; | |
Y2[ i ] = Y[ i ] * Y[ i ]; | |
} | |
double N = ctrInput; | |
double sumX = 0; | |
double sumY = 0; | |
double sumXY = 0; | |
double sumX2 = 0; | |
double sumY2 = 0; | |
//---------------------- Getting the Summation -------------------------// | |
for(int i=0; i<ctrInput; i++){ | |
sumX += X[ i ]; | |
sumY += Y[ i ]; | |
sumXY += XY[ i ]; | |
sumX2 += X2[ i ]; | |
sumY2 += Y2[ i ]; | |
} | |
double numerator = 0; | |
double denominator = 0; | |
double correlateVal = 0; | |
String strResult = ""; | |
numerator = (N * sumXY) - (sumX * sumY); | |
denominator = Math.sqrt( ((N * sumX2) - (sumX * sumX)) * ((N * sumY2) - (sumY * sumY)) ); | |
correlateVal = numerator / denominator; | |
if (correlateVal >= 0){ | |
strResult = String.format("<html>The correlation for our cases is %.2f, which is a fairly strong " + | |
"positive relationship. I guess there is a relationship between %s and %s, " + | |
"at least in this made up data!</html>" , correlateVal, tfInput[1].getText(), tfInput[2].getText()); | |
}else{ | |
strResult = String.format("<html>The correlation for our cases is %.2f, which is a fairly strong " + | |
"negative relationship. I guess there is no relationship between %s and %s, " + | |
"at least in this made up data!</html>" , correlateVal, tfInput[1].getText(), tfInput[2].getText()); | |
} | |
lblResult.setText( strResult ); | |
computeB.setText("Close"); | |
table.setEnabled( false ); | |
} | |
else if( computeB.getActionCommand().equals("Close")){ | |
int ans = JOptionPane.showConfirmDialog(correlation.this, "Do you really want to close?", "Exit",JOptionPane.YES_NO_OPTION); | |
if( ans == 0 ){ | |
System.exit(0); | |
} | |
} | |
} | |
if( fVar && sVar ){ | |
resetB.setEnabled( true ); | |
JOptionPane.showMessageDialog( correlation.this, "Please start filling up the table"); | |
fVar = sVar = false; | |
} | |
}catch(NumberFormatException nfe){ | |
JOptionPane.showMessageDialog( correlation.this, "Please Enter a valid data!"); | |
} | |
} | |
public Vector createBlankElement() | |
{ | |
Vector t = new Vector(); | |
t.addElement((String) " "); | |
t.addElement((String) " "); | |
return t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment