Sorting Array Angka Tertentu di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication31;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner data=new Scanner(System.in);
int angka=0;

// memasukkan jumlah angka
System.out.print(“Masukkan Jumlah Angka : “);
int[] array=new int[data.nextInt()];
System.out.println(“—————————–“);
// memasukkan angka
for(int i=0;i<array.length;i++){
System.out.print(“Masukkan Angka Ke-“+(i+1)+” : “);
array[i]=data.nextInt();
if(array[i]>40)
angka++;
}

// memindah angka >40 ke array baru
int[] array40=new int[angka];
int index=0;
for(int i=0;i<array.length;i++){
if(array[i]>40){
array40[index]=array[i];
index++;
}
}

// sorting bubble
int sementara;
for(int i=0;i<array40.length;i++){
for(int j=0;j<array40.length-1;j++){
if(array40[j+1]<array40[j]){
sementara=array40[j];
array40[j]=array40[j+1];
array40[j+1]=sementara;
}
}
}

// cetak array
System.out.println(“—————————–\nHasil Sorting Angka >40 :”);
for(int i=0;i<array40.length;i++){
System.out.print(array40[i]+” “);
}
System.out.println();

}

}

Sorting Angka Genap

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication30;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner data=new Scanner(System.in);
int genap=0;
int ganjil=0;

// memasukkan jumlah angka yang diInput
System.out.print(“Masukkan Jumlah Angka : “);
int[] angka=new int[data.nextInt()];
System.out.println(“—————————“);
// memasukkan angka sesuai jumlah angka
for(int i=0;i<angka.length;i++){
System.out.print(“Masukkan Angka Ke-“+(i+1)+” : “);
angka[i]=data.nextInt();
if(angka[i]%2==0){
genap++;
}
else{
ganjil++;
}
}

System.out.println(“—————————–“);
System.out.println(“Jumlah Angka Genap : “+genap);
System.out.println(“Jumlah Angka Ganjil : “+ganjil+”\n”);

if(genap!=0){
// memindahkan angka ke angkaGenap
int indexGenap=0;
int[] angkaGenap=new int[genap];
for(int i=0;i<angka.length;i++){
if(angka[i]%2==0){
angkaGenap[indexGenap]=angka[i];
indexGenap++;
}
}

// mencetak angkaGenap
System.out.println(“HASIL angka GENAP : “);
for(int i=0;i<angkaGenap.length;i++){
System.out.print(angkaGenap[i]+” “);
}

// sorting
int sementara;
for(int i=1;i<angkaGenap.length;i++){
sementara=angkaGenap[i];
int j=i-1;
while(j>=0 && angkaGenap[j]<sementara){
angkaGenap[j+1]=angkaGenap[j];
j–;
}
angkaGenap[j+1]=sementara;
}

// mencetak hasil sorting
System.out.println(“\nHASIL angka GENAP SETELAH DISORTING DESCCENDING: “);
for(int i=0;i<angkaGenap.length;i++){
System.out.print(angkaGenap[i]+” “);
}
System.out.println();
}
else
System.out.println(“SEMUA ANGKA TIDAK ADA YANG GENAP !!”);
}

}

Selection Sort

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package selectionsort;

/**
*
* @author Angga
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] array={-5,-8,-11,0,34,1,1,6,9,22,15};

int idxMin;
int tmp;
for(int j=0;j<array.length-1;j++){
idxMin=j;
for(int i=j+1;i<array.length;i++){
if(array[i]<array[idxMin])
idxMin=i;
}
if(idxMin!=j){
tmp=array[idxMin];
array[idxMin]=array[j];
array[j]=tmp;
}

}
for(int i=0;i<array.length;i++){
System.out.print(array[i]+” “);
}

}

}

Mengenal Scanner di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package scanner;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print(“Masukkan Nama : “);
Scanner data=new Scanner(System.in);
String aku = data.nextLine();
System.out.println(“Nama kamu : “+aku);

System.out.print(“Masukkan N.I.M : “);
long nim = data.nextLong();
System.out.println(“N.I.M “+aku+” : “+nim);
}

}

printf (Print Format) di Java

package printFormat;
public class printFormat {

public static void main(String[] args) {
int array[]={2,2,5555,1,3,5,1,1,2,8};
int jmlh=0;
System.out.printf(“%s%8s\n”, “index”, “value”);

for(int i=0;i<array.length;i++){
System.out.printf(“%4s%8s\n”, i, array[i]);
jmlh += array[i];
}
System.out.println(“\njumlah =  ” +jmlh);

}
}

Perkalian Matrix di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package pekalianmatrix;

/**
*
* @author ANGGA
*/
public class perkalianMatrix {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int matrix1[][] = {{5,2,1},{3,7,2}};
int matrix2[][] = {{2,3},{5,8},{1,6}};
int matrixHasil[][] = new int[2][2];

for( int i = 0; i<2; i++) {     // perulangan untuk jumlah baris matrixKali
for(int j = 0; j<2; j++) {      // perulangan untuk jumlah kolom matrixKali
for(int a=0; a<3; a++)     // perulangan untuk jumlah perkalian setiap nilai
matrixHasil[i][j] += (matrix1[i][a] * matrix2[a][j]);      // perkalian baris matrix awal dengan kolom matrix hasil transpose
System.out.print(matrixHasil[i][j] + ”  “);      // mencetak hasil kali

}
System.out.println();
}

}
}

Program Pasien Rumah Sakit Update

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package pasienarray;
import java.util.Scanner;
/**
*
* @author ANGGA
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner data=new Scanner(System.in);
int biayaPasien=0;
int biayaTotal=0;

System.out.println(“AYUK BEROBAT\n===============”);
System.out.print(“Berapa Pasiennya : “);
int pasien=data.nextInt();
String[][] arrayKeluhan=new String[pasien][];
int[][] arrayHarga=new int[pasien][];

for(int i=0;i<pasien;i++){
System.out.println(“——————————–\nPASIEN KE-“+(i+1));
System.out.print(“Berapa Keluhan Pasien “+(i+1)+” : “);
int jmlKeluhan=data.nextInt();
arrayHarga[i]=new int[jmlKeluhan];
arrayKeluhan[i]=new String[jmlKeluhan];
biayaPasien=0;
for(int j=0;j<jmlKeluhan;j++){
System.out.print(” – Keluhan ke “+(j+1)+” : “);
String keluhan=data.next();
arrayKeluhan[i][j]=keluhan;
System.out.print(”   *Bayar Dokter Sakit “+keluhan+” : “);
int bayar=data.nextInt();
arrayHarga[i][j]=bayar;

biayaPasien+=arrayHarga[i][j];
biayaTotal+=arrayHarga[i][j];
}
System.out.println(“Total Biaya Pasien “+(i+1)+” : “+biayaPasien);
}
System.out.println(“\n**Total Biaya Berobat Pasien : “+biayaTotal);

//hasil array yang disimpan////////////////////////////////////////////////////////////////
System.out.println(“\n=========================\nREKAP RUMAH SAKIT HARI INI\n=========================”);
System.out.println(“Hari ini ada “+pasien+” pasien\n————————-“);
for(int  i=0;i<arrayKeluhan.length;i++){
for(int j=0;j<arrayHarga[i].length;j++){
System.out.println(“Pasien ke “+(i+1)+” Sakit “+arrayKeluhan[i][j]+” Bayar “+arrayHarga[i][j]);
}
System.out.println(“TOTAL BAYAR PASIEN “+(i+1)+” : “+biayaTotal+”\n———————————–“);
System.out.println();
}
System.out.println(“TOTAL UANG RUMAH SAKIT “+biayaTotal);

}

}