Pemrograman Median, Rata-rata, Modus dengan Java

Pemrograman di bawah ini dibuat menggunakan java, berupa latihan dasar pemahaman tentang pemrograman yang diantaranya berupa inisialisasi variabel, array, perulangan,tipe data, dan operator.
 package cobacoba;  
 /**  
  *  
  * @author AWM-SEAMOLEC  
  * Nama   : Al Bahri Maraxsa  
  * E-mail  : albahri_maraxsa@yahoo.co.id  
  * Blog   : albahrimaraxsa.wordpress.com  
  */  
 import java.util.Scanner;  
 public class tugasmbakdesti {  
   static int N, p, simpan;  
   static int mod;  
   static float jumlah = 0;  
   static int arrData[] = new int[1000];  
   static int arrModus[] = new int[1000];  
   static Scanner input = new Scanner(System.in);  
   static float z;  
   static float z1;  
   static float median2;</pre>  
 <!--more-->  
 <pre>  
   static void inputan() {  
     System.out.print("Masukkan banyak data : ");  
     N = input.nextInt();  
     /*Inputan*/  
     for (int a = 1; a       System.out.print("Masukkan Data-" + a + ": ");  
       arrData[a - 1] = input.nextInt();  
     }  
     System.out.println();  
     System.out.println("***********************************");  
   }  
   static void urutkan() {  
     /*PENGURUTAN DATA*/  
     for (int i = 1; i < N; i++) {  
       for (int j = i; j < N; j++) {         if (arrData[i - 1] > arrData[j]) {  
           int Var = arrData[i - 1];  
           arrData[i - 1] = arrData[j];  
           arrData[j] = Var;  
         }  
       }  
     }  
   }  
   static void sigmaN() {  
     /*PENJUMLAHAN DATA*/  
     for (int y = 0; y < N; y++) {  
       jumlah = jumlah + arrData[y];  
     }  
     System.out.println("");  
   }  
   static void median() {  
     if (N % 2 != 0) {  
       System.out.print(arrData[(N / 2)]);  
     } else {  
       z = arrData[((N - 1) / 2)];  
       z1 = arrData[((N - 1) / 2 + 1)];  
       median2 = (z + z1) / 2;  
       System.out.print(median2);  
     }  
   }  
   static void printX() {  
     System.out.print("Urutan Data = ");  
     for (int q = 0; q < N; q++) {  
       System.out.print(arrData[q] + "  ");  
     }  
     System.out.println("");  
     System.out.print("Median   = ");  
     median();  
     System.out.println("nRata-rata  = " + jumlah / N);  
     for (int k = 0; k < N; k++) {  
     }  
     System.out.println("n***********************************");  
     System.out.println("");  
 //    System.out.println("hasil: ");  
 //    for (p = 0; p < N; p++) {  
 //      System.out.println(arrData[p] + " " + arrModus[p]);  
 //    }  
   }  
   static void modus() {  
     for (int i = 0; i < N; i++) {  
       int hitung = 0;  
       if (arrModus[i] != 0) {  
         break;  
       }  
       for (int j = i; j < N; j++) {  
         if (arrData[i] == arrData[j]) {  
           ++hitung;  
         }  
       }  
       arrModus[i] = hitung;  
     }  
   }  
   static void tersering() {  
     /*TERSERING*/  
     for (int za = 0; za < N; za++) {  
       for (int zz = 0; zz < N; zz++) {         if (arrModus[za] > arrModus[zz] && arrModus[za] > arrModus[simpan]) {  
           simpan = za;  
         }  
       }  
     }  
     System.out.print("Modus = " + arrData[simpan] + "; ");  
     System.out.println("");  
   }  
   public static void main(String[] args) {  
     inputan();  
     urutkan();  
     sigmaN();  
     modus();  
     printX();  
     tersering();  
   }  
 }  
Previous
Next Post »