RSS

statistikGUI

statistik mean, median, modus, quartile, decile, percentile using java



import java.awt.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
* @author Inge
*/
public class StatistikForm extends javax.swing.JFrame {


public StatistikForm() {
super("irene statistik program");
initComponents();
screenPosition();
}


// //GEN-BEGIN:initComponents
private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
taData = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
tblHistogram = new javax.swing.JTable();
jScrollPane3 = new javax.swing.JScrollPane();
taStatistik = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
randomNumber = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtDataValue = new javax.swing.JTextField();
chartOption1 = new javax.swing.JComboBox();
jLabel4 = new javax.swing.JLabel();
jScrollPane4 = new javax.swing.JScrollPane();
tblHistogram2 = new javax.swing.JTable();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
chartOption2 = new javax.swing.JComboBox();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel2.setFont(new java.awt.Font("[z] Arista Light", 0, 14));
jLabel2.setText("Week 1 - Statistik");

taData.setColumns(20);
taData.setRows(5);
jScrollPane1.setViewportView(taData);

tblHistogram.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Data", "Frekuensi"
}
));
jScrollPane2.setViewportView(tblHistogram);

taStatistik.setColumns(20);
taStatistik.setRows(5);
jScrollPane3.setViewportView(taStatistik);

jLabel3.setFont(new java.awt.Font("[z] Arista Light", 0, 14));
jLabel3.setText("irene erlyn");

randomNumber.setText("process");
randomNumber.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
process(evt);
}
});

jButton1.setText("generate chart");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("Default number");
jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
defaultNumber(evt);
}
});

buttonGroup1.add(jRadioButton2);
jRadioButton2.setSelected(true);
jRadioButton2.setText("Randomize number");
jRadioButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
randomizeNumber(evt);
}
});

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel1.setText("Insert Max Data");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtDataValue, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtDataValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
);

chartOption1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chart Bar", "Chart Bar 3D", "Chart Line", "Chart Line 3D" }));

jLabel4.setText("Data tak Berkelompok :");

tblHistogram2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"Data", "Frekuensi"
}
));
jScrollPane4.setViewportView(tblHistogram2);

jLabel5.setText("Tabel Histogram Data Tunggal");

jLabel6.setText("Tabel Histogram Data Kelompok");

jLabel7.setText("Statistic Calculation");

jLabel8.setText("Data Berkelompok :");

chartOption2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chart Bar", "Chart Bar 3D", "Chart Line", "Chart Line 3D" }));

jButton2.setText("generate chart");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 247, Short.MAX_VALUE)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(randomNumber, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.addComponent(jLabel5)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.addComponent(jLabel6)
.addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.addComponent(jLabel7)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(28, 28, 28)
.addComponent(chartOption2, 0, 156, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(chartOption1, 0, 156, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)))
.addGap(16, 16, 16)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(randomNumber)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)
.addGap(7, 7, 7)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(chartOption1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(chartOption2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addContainerGap())
);

pack();
}//
//GEN-END:initComponents



private void process(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_process
if (jRadioButton2.isSelected()) {
if (txtDataValue.getText().toString().equals("")) {
JOptionPane.showConfirmDialog(null, "data value cannot be empty");
} else {
randomNumber();
}
}
taData.append("Data :\n");
show(ranNum);
sortArray(ranNum);
taData.append("Sorted Data :\n");
show(sort);
calculateStatistik(ranNum);
}//GEN-LAST:event_process


private void defaultNumber(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_defaultNumber
jPanel1.setVisible(false);
DefaultNumber();


}//GEN-LAST:event_defaultNumber


private void randomizeNumber(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_randomizeNumber
jPanel1.setVisible(true);
}//GEN-LAST:event_randomizeNumber


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
int index = chartOption1.getSelectedIndex();
new histogram(check, freq, index);
}//GEN-LAST:event_jButton1ActionPerformed


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
int index = chartOption1.getSelectedIndex();
new histogram(batasClass, classFreq, index);
}//GEN-LAST:event_jButton2ActionPerformed


private void DefaultNumber() {
ranNum = new int[]{48, 75, 48, 74, 59, 69, 72, 60, 76, 55, 73, 58, 68,
59, 55, 70, 65, 59, 73, 67, 94, 73, 57, 78, 62, 62, 79, 67, 70,
72, 79, 69, 68, 78, 71, 75, 75, 55, 64, 48};
}


private void randomNumber() {
ranNum = new int[Integer.parseInt(txtDataValue.getText().toString())];
for (int i = 0; i < ranNum.length; i++) { ranNum[i] = (int) (Math.random() * (max - min + 1)) + min; numberRandom += ranNum[i] + " "; } } private void show(int[] array) { String temp = ""; int j = 0; for (int i = 0; i < array.length; i++) { if (j < 4) { temp += array[i] + " "; j++; } else { temp += array[i] + " \n"; j = 0; } } taData.append(temp + "\n\n"); } private void sortArray(int[] array) { sort = new int[array.length]; System.arraycopy(array, 0, sort, 0, sort.length); Arrays.sort(sort); } private void calculateStatistik(int[] array) { mean += "\n\n--> Mean= " + findMean(ranNum);
median += "\n\n--> Median= " + findMedian(ranNum);
modus += "\n\n--> Modus= " + findMode(ranNum);
rentanganKuartil += "\n\n--> rentangan kuartil = " + rentanganKuartil(sort);
simpanganKuartil += "\n\n--> simpangan kuartil = " + simpanganKuartil(sort);
simpanganRata += "\n\n--> simpangan rata-rata = " + simpanganRata(sort);
simpanganBaku += "\n\n--> simpangan baku = " + simpanganBaku(sort);
variasi += "\n\n--> variasi = " + variasi(sort);
System.out.println("\n--> Quartile:");
findKuartil(ranNum);
if (ranNum.length >= 10) {
System.out.println("\n\n--> Decile:");
findDecil(ranNum);
}
if (ranNum.length >= 100) {
System.out.println("\n\n--> Precentile:");
findPrecentile(ranNum);
}
System.out.println("\n\n--> Histogram");
findHistrogram(sort);
findHistrogramGroup(sort);


banyakKelas += "\n\n--> banyak kelas = " + classNumber();
intervalKelas += "\n\n--> interval kelas = " + classInterval();


taStatistik.setText("Statistic Calculate!\n" + mean + median + modus + rentanganKuartil + simpanganKuartil + simpanganRata + simpanganBaku + variasi + banyakKelas + intervalKelas);


}


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {


public void run() {
new StatistikForm().setVisible(true);
}
});
}


//source for count
public static int findMean(int array[]) {
int sum = 0;
int average = 0;
int length = array.length;
for (int j = 0; j < length; j++) { sum += array[j]; average = sum / length; } return average; } // median public static int findMedian(int array[]) { int length = array.length; if (length % 2 == 0) { return (sort[(sort.length / 2) - 1] + sort[sort.length / 2]) / 2; } else { return sort[sort.length / 2]; } } // quartile public static void findKuartil(int array[]) { int length = array.length; int[] k = new int[3]; for (int i = 0; i < 3; i++) { k[i] = sort[(((i + 1) * (length + 1) / 4)) - 1]; System.out.print("k" + (i + 1) + " = " + k[i] + " "); } } // decile public static void findDecil(int array[]) { int length = array.length; int[] d = new int[10]; for (int i = 0; i < 9; i++) { d[i] = sort[(((i + 1) * (length + 1) / 10)) - 1]; System.out.print("d" + (i + 1) + " = " + d[i] + " "); } } // precentile public static void findPrecentile(int array[]) { int length = array.length; int[] p = new int[100]; for (int i = 0; i < 99; i++) { p[i] = sort[(((i + 1) * (length + 1) / 100)) - 1]; System.out.print("p" + (i + 1) + " = " + p[i] + " "); } } // modus public static int findMode(int array[]) { int max = 0, maxCount = 0; int length = array.length; for (int i = 0; i < length; ++i) { int count = 0; for (int j = 0; j < length; ++j) { if (array[j] == array[i]) { ++count; } } if (count > maxCount) {
maxCount = count;
max = array[i];
}
}
return max;
}
// rentangan kuartil


public static int rentanganKuartil(int array[]) {
return sort[((3 * (array.length + 1) / 4))]
- sort[((1 * (array.length + 1) / 4))];
}


// simpangan kuartil
public static int simpanganKuartil(int array[]) {
return rentanganKuartil(array) / 2;
}


// simpangan rata-rata
public static int simpanganRata(int array[]) {
int jumlah = 0;
for (int i = 0; i < array.length; i++) { jumlah += (Math.abs(array[i] - findMean(sort))); } return jumlah / array.length; } // simpangan baku public static int simpanganBaku(int array[]) { int temp = 0; for (int i = 0; i < array.length; i++) { temp += (Math.pow((array[i] - findMean(sort)), 2)); } return (int) Math.sqrt(temp / (array.length - 1)); } // variansi public static int variasi(int array[]) { return (int) Math.pow(simpanganBaku(array), 2); } //jumlah kelas public static int classNumber() { int x = (int) Math.log(ranNum.length); int y = (int) (3.3 * x); return y + 1; } //interval kelas public static int classInterval() { return (sort[sort.length - 1] - sort[0]) / classNumber(); } // histogram data tunggal public void findHistrogram(int array[]) { check = new int[array.length]; freq = new int[array.length]; for (int i = 0; i < array.length; i++) { boolean find = false; for (int j = 0; j < array.length; j++) { if (check[j] == array[i]) { freq[j] += 1; find = true; break; } } if (find == false) { check[i] = array[i]; freq[i] = 1; } } String data = "", frekuensi = ""; String judul[] = new String[]{"data", "frekuensi"}; def_mod = new DefaultTableModel(null, judul); tblHistogram.setModel(def_mod); for (int x = 0; x < check.length; x++) { int j = 0; if (check[x] != 0) { data = "" + check[x]; frekuensi = "" + freq[x]; String[] xg = {"nice", "try"}; String[] datacomp = {data, frekuensi}; System.out.println(def_mod.getRowCount()); //def_mod.addRow(xg); def_mod.addRow(datacomp); } } } //histogram data berkelompok public void findHistrogramGroup(int array[]) { int classNmber = classNumber(); int classIntrvl = classInterval(); classRange = new int[classNmber][classNmber]; classFreq = new int[classNmber]; batasKelas = new String[classNmber]; batasClass = new int[classNmber]; int batasBawah = array[0], batasAtas = 0; for (int i = 0; i < classNmber; i++) { batasClass[i] = batasBawah; batasAtas = batasBawah + classIntrvl; classRange[i][0] = batasBawah; classRange[i][1] = batasAtas; int temp = (i * classIntrvl); for (int j = 0; j < array.length; j++) { //hitung freq if (array[j] >= batasBawah && array[j] <= batasAtas) {
classFreq[i] += 1;
}
}
batasBawah = batasAtas + 1;


}
//print table
String data = "", frekuensi = "";
String judul[] = new String[]{"data", "frekuensi"};
def_mod = new DefaultTableModel(null, judul);
tblHistogram2.setModel(def_mod);
for (int i = 0; i < classNmber; i++) {
batasKelas[i] = "" + Integer.toString(classRange[i][0]) + " - " + "" + Integer.toString(classRange[i][1]);
System.out.println(batasKelas[i] + "\t|\t" + classFreq[i]);


String[] datacomp = {"" + batasKelas[i], "" + classFreq[i]};
def_mod.addRow(datacomp);
}
}


public void screenPosition() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
setLocation(x, y);
}
private DefaultTableModel def_mod;
private static int[] sort;
private static int min = 10;
private static int max = 100;
private static int[] ranNum;
private static int[] check;
private static int[] freq;
private static int classRange[][];
private static int classFreq[];
private static int batasClass[];
private static String batasKelas[];
private String numberRandom = "", numberSort = "", mean = "", median = "", modus = "", kuartil = "", desil = "", percentil = "";
private String rentanganKuartil = "", simpanganKuartil = "", simpanganRata = "", simpanganBaku = "", variasi = "";
private String banyakKelas = "", intervalKelas = "";
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JComboBox chartOption1;
private javax.swing.JComboBox chartOption2;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JButton randomNumber;
private javax.swing.JTextArea taData;
private javax.swing.JTextArea taStatistik;
private javax.swing.JTable tblHistogram;
private javax.swing.JTable tblHistogram2;
private javax.swing.JTextField txtDataValue;
// End of variables declaration//GEN-END:variables
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 komentar:

Poskan Komentar