import java.util.Scanner;
class Korelasi
{
Scanner input = new Scanner(System.in);
double[] x;
double[] y;
double[] x2;
double[] y2;
double[] xy;
int n;
public Korelasi()
{
menu();
}
public void menu()
{
System.out.println("------------------");
System.out.println("| Menu Utama |");
System.out.println("| 1. Input |");
System.out.println("| 2. Korelasi |");
System.out.println("| 3. X-it |");
System.out.println("------------------");
System.out.print("Masukkan Pilihanmu : " );
int pil = input.nextInt();
switch(pil)
{
case 1:
clean();
try{
input();
}catch(IndexOutOfBoundsException ex){
System.out.println("Ada Kesalahan pada proses IO");
}
clean();
menu();
case 2:
clean();
hitungKorelasi();
menu();
case 3:
clean();
System.exit(0);
default:
System.exit(0);
}
}
public void clean()
{
for (int i=0;i<=20;i++)
{
System.out.println("\n");
}
}
public void input()
{
System.out.print("masukan jumlah data :");
n = input.nextInt();
x= new double[n];
y= new double[n];
x2 = new double[n];
y2=new double[n];
xy=new double[n];
System.out.print("\nmasukkan data x :\n");
for(int i=0;i<n;i++)
{
System.out.print("data ke-"+(i+1)+":");
x[i]= input.nextInt();
}
System.out.println("\nmasukkan data y :");
for(int i=0;i<n;i++)
{
System.out.print("data ke-"+(i+1)+":");
y[i]= input.nextInt();
}
for(int i=0 ; i<n;i++)
{
x2[i]=Math.pow(x[i],2);
y2[i]=Math.pow(y[i],2);
xy[i]= x[i] * y[i] ;
}
}
public double sigma(double k[])
{
double data = 0;
for(int i=0; i<n;i++)
{
data = data + k[i];
}
return(data);
}
public void hitungKorelasi()
{
//double hasil = ((n*sigma(xy)) - (sigma(x) * sigma(y))) / Math.sqrt((n*sigma(x2))- Math.pow(sigma(x),2)*(n*sigma(y2))-Math.pow(sigma(y),2));
double hasil = ((n*sigma(xy)) - (sigma(y) * sigma(x))) / ((n*sigma(y2))-Math.pow(sigma(y),2));
System.out.println("Koefisien korelasi(r) : "+hasil);
if(hasil > 0 ){
System.out.println("hubungan dua variabel adalah paralel");
} else if(hasil<0){
System.out.println("hubungan dua variabel bertolak belakang");
} else {
System.out.println("tidak ada hubungan linier antara dua variabel");
}
}
public static void main(String[] args)
{
new Korelasi();
}
}
SANTAP SIANG MURAH DI BELAKANG MASJID AL-AZHAR KALIMALANG :
BalasHapushttp://wawanhermawan74.blogspot.com/2010/11/santap-siang-murah-dibelakang-masjid-al.html