Rabu, 22 Mei 2013

Listing Program Perhitungan Statistika


import java.util.Scanner;


class statistika{
// Scanner Program
Scanner input = new Scanner(System.in);
// Variabel untuk probabilitas
public int j, k;
// Variabel untuk regresi
double[] x,y,x2,y2,xy;
int n;
double a1,a0,b1,b0;
// Variabel untuk median & mean
int banyak;
int data[];
int temp;
statistika(){menu();}



public void menu(){
System.out.println("==========================");
System.out.println("|   PROGRAM STATISTIKA  |");
System.out.println("==========================");
System.out.println("| 1. Probabilitas        |");
System.out.println("| 2. Regresi             |");
System.out.println("| 3. Mean & Median       |");
System.out.println("| 4. Exit                |");
System.out.println("==========================");
System.out.print(" Masukan pilhan Anda : ");
int pil = input.nextInt();
switch(pil){
case 1: clean(); prob(); break;
case 2: clean(); reg(); break;
case 3: clean(); medimean(); break;
case 4: System.exit(0);
default: clean(); menu();
}
}



// UNTUK JARAK MENU SATU DENGAN MENU LAINNYA
public void clean(){
for (int i=0;i<=0.5;i++){
System.out.println("\n");}}



// MENU PROBABILITAS
public void prob(){
System.out.println("==========================");
System.out.println("|   Menu Probabilitas  |");
System.out.println("==========================");
System.out.println("| 1. Input Nilai         |");
System.out.println("| 2. Permutasi           |");
System.out.println("| 3. Kombinasi           |");
System.out.println("| 4. Menu Utama          |");
System.out.println("==========================");
System.out.print(" Masukan pilhan Anda : ");
int pil = input.nextInt();
switch(pil){
case 1: clean(); inputData(); clean(); prob();
case 2: clean(); per(); clean(); prob();
case 3: clean(); kom(); clean(); prob();
case 4: clean(); menu();
default: clean(); prob();
}
}
// MENU REGRESI
public void reg() {
System.out.println("==========================");
System.out.println("|      Menu Regresi      |");
System.out.println("==========================");
System.out.println("| 1. Input Nilai         |");
System.out.println("| 2. Regresi Y kepada X  |");
System.out.println("| 3. Regresi X kepada Y  |");
System.out.println("| 4. Menu Utama          |");
System.out.println("==========================");
System.out.print(" Masukan pilhan Anda : ");
int pil = input.nextInt();
switch(pil) {
case 1: clean();
try{ input(); }
catch(IndexOutOfBoundsException ex){ System.out.println("Ada Kesalahan pada proses IO"); }
clean(); reg();    
case 2: clean(); regresiYX(); clean(); reg();        
case 3: clean(); regresiXY(); clean(); reg();
case 4: clean(); menu();                  
default: clean(); reg(); } }



// MENU MEDIAN & MEAN
public void medimean() {
System.out.println("==========================");
System.out.println("|   Menu Median & Mean   |");
System.out.println("==========================");
System.out.println("| 1. Input Nilai         |");
System.out.println("| 2. Median              |");
System.out.println("| 3. Mean                |");
System.out.println("| 4. Menu Utama          |");
System.out.println("==========================");
System.out.print(" Masukan pilhan Anda : ");
int pil = input.nextInt();
    switch(pil){
    case 1: clean();
    try{ prosesinput();}
catch(IndexOutOfBoundsException ex){ System.out.println("Ada Kesalahan pada proses IO!!!"); }
clean(); medimean();
     case 2: clean(); median(); clean(); medimean();
     case 3: clean(); mean(); clean(); medimean();
     case 4: clean(); menu();
default : clean(); medimean(); } }
// INI ADALAH KUMPULAN METHOD YANG DIGUNAKAN MENU PROBABILITAS
public void inputData(){
System.out.print("Masukan Nilai n : ");
j = input.nextInt();
System.out.print("Masukan Nilai k : ");
k = input.nextInt();
prob();
}



public int fak(int j){
int jumlah = 1;
for(int i=1;i<=j;i++){
jumlah = jumlah * i;
}
return jumlah;
}



public void per(){
if(j<k){System.out.println("\nMATH ERROR - n<k");}
else{
System.out.println("\nP("+j+","+k+")"+"= "+(fak(j)/fak(j-k)));
}
prob();
}



public void kom(){
if(j<k){System.out.println("\nMATH ERROR - n<k");}
else{
System.out.println("\nC("+j+","+k+")"+"= "+(fak(j)/(fak(j-k)*fak(k))));
}
prob();
}




// INI ADALAH KUMPULAN METHOD YANG DIGUNAKAN MENU REGRESI
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 regresiYX()    {
a1 = ((n*sigma(xy)) - (sigma(x) * sigma(y))) /
((n*sigma(x2))-Math.pow(sigma(x),2));
a0 = ((sigma(y) * sigma(x2)) - (sigma(x) * sigma(xy))) /
((n*sigma(x2))-Math.pow(sigma(x),2));
System.out.println("Persamaan Regrsi Y terhadap X");
if (a1> 0)
System.out.println("Y = "+a0+" + "+a1+"x");
else
System.out.println("Y = "+a0+a1+ "x");    }
    
public void regresiXY()    {
b1 = ((n*sigma(xy)) - (sigma(y) * sigma(x))) /
((n*sigma(y2))-Math.pow(sigma(y),2));
b0 = ((sigma(x) * sigma(y2)) - sigma(y) * sigma(xy)) /
(n* sigma(y2)-Math.pow(sigma(y),2));
System.out.println("Persamaan Regresi X terhadap Y");     
if (b1>0)
System.out.println("X = "+b0+" + "+b1+"y");
else
System.out.println("X = "+b0+b1+"y");    }




// INI ADALAH KUMPULAN METHOD YANG DIGUNAKAN MENU MEDIAN & MEAN



   
public void prosesinput(){
    System.out.print("Banyaknya Data : ");
    banyak = input.nextInt();
    data = new int[banyak];
    for(int i=0;i<=banyak-1;i++){
    System.out.print("Data ke-"+(i+1)+" : ");
    data[i]= input.nextInt();
    }
    }
   
    public void urut(){
    for(int i=0;i<=banyak-1;i++){
    for(int j=i+1;j<=banyak-1;j++){
    if(data[i] > data[j]){
    temp = data[i];
    data[i] = data[j];
    data[j] = temp;
    }
    }
    }
    System.out.print("Data yang sudah diurut : ");
    for(int i=0;i<=banyak-1;i++){
    System.out.print(data[i]+" ");
    }
    }
    public void mean(){
    int jumlah = 0;
    System.out.print("Data sudah diurut : ");
    for(int i=0; i<=banyak-1; i++){
    System.out.print(data[i]+" ");
    jumlah = jumlah + data[i];
    }
    System.out.println("\nMean = "+(jumlah/banyak));
    }
   
    public void median(){
    urut();
    int bil = banyak % 2;
    if(bil==0){
    int posisi = banyak / 2;
    System.out.println("\nDatanya Genap");
    double median = ((data[posisi-1] + data[posisi+1]) / 2);
    System.out.printf("mediannya = %.2f",median);
    }
    else{
    int posisi = (banyak + 1) / 2;
    System.out.println("Datanya Ganjil");
    double median = data[posisi-1];
    System.out.printf("\nMediannya = %.2f",median);
    }
    }



public static void main(String[] args){
new statistika();
}



}


Gambar Outputnya : 








Created by : Heriyanto, Muhammad Syahrul & Randi Mubarok (Kelas 2IA21)
dan terima kasih kami ucapkan kepada dosen PBO kami : Ibu Suryaningrum

Tidak ada komentar:

Poskan Komentar