Kelas Larik
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package soal2b;
/**
*
* @author admin
*/
public class Larik {
public static void selectionsortobj(Object x[]){
for (int iterasi = 0; iterasi <= x.length-2; iterasi ++){//Looping untuk iterasi = 0 sampai N-2
int minIndex = iterasi;//menentukan minIndex sama dengan iterasi;
for (int elemen=iterasi+1; elemen <= x.length-1; elemen++){//Looping untuk elemen = iterasi + 1 sampai N-1
if (((Comparable)x[elemen]).compareTo(x[minIndex])<0){
//mengetest apakah (Comparable)x[elemen]).compareTo(x[minIndex])<0)
minIndex = elemen;//jika iya minIndek sama dengan elemen
}
}
Object temp = x[iterasi];
x[iterasi]=x[minIndex];
x[minIndex]=temp;
//menukar nilai x[iterasi] dengan x[minIndex ]
}
}
}
Kelas Mahasiswa
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package soal2b;
/**
*
* @author admin
*/
public class Mahasiswa implements Comparable{
// comparable merupakan kelas interface dari kelas mahasiswa
private int nim;
private String nama;
private int nilai;
//mendeklarasikan variabel
public int getNim() {
return nim;
}
public void setNim(int nim) {
this.nim = nim;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
//method Nama dan method Nim
public int compareTo(Object o) {
if (nilai == ((Mahasiswa) o).nilai) {
return 0;
} else if (nilai > ((Mahasiswa) o).nilai) {
return 1;
} else {
return -1;
}//method compareTo digunakan untuk memanggil objek
}
/**
* @return the nilai
*/
public int getNilai() {
return nilai;
}
/**
* @param nilai the nilai to set
*/
public void setNilai(int nilai) {
this.nilai = nilai;
}
}
Kelas Main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package soal2b;
/**
*
* @author admin
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Mahasiswa[] mhs = new Mahasiswa[11];
//mengeset nim yang akan diurutkan
mhs[0] = new Mahasiswa();
mhs[0].setNim(105314180);
mhs[0].setNama("Dia Dari");
mhs[0].setNilai(70);
mhs[1] = new Mahasiswa();
mhs[1].setNim(105314107);
mhs[1].setNama("Joko Nirwanto");
mhs[1].setNilai(56);
mhs[2] = new Mahasiswa();
mhs[2].setNim(105314134);
mhs[2].setNama("Hari Mau");
mhs[2].setNilai(89);
mhs[3] = new Mahasiswa();
mhs[3].setNim(105314120);
mhs[3].setNama("Nani Suka");
mhs[3].setNilai(34);
mhs[4] = new Mahasiswa();
mhs[4].setNim(105314139);
mhs[4].setNama("Jero Sekali");
mhs[4].setNilai(78);
mhs[5] = new Mahasiswa();
mhs[5].setNim(105314145);
mhs[5].setNama("Gunung Bintara");
mhs[5].setNilai(67);
mhs[6] = new Mahasiswa();
mhs[6].setNim(105314189);
mhs[6].setNama("Bunga Indah");
mhs[6].setNilai(90);
mhs[7] = new Mahasiswa();
mhs[7].setNim(105314135);
mhs[7].setNama("Farah Dani");
mhs[7].setNilai(23);
mhs[8] = new Mahasiswa();
mhs[8].setNim(105314167);
mhs[8].setNama("Juariah");
mhs[8].setNilai(77);
mhs[9] = new Mahasiswa();
mhs[9].setNim(105314125);
mhs[9].setNama("Koko Terkoko");
mhs[9].setNilai(56);
mhs[10] = new Mahasiswa();
mhs[10].setNim(105314131);
mhs[10].setNama("Rani Surti");
mhs[10].setNilai(93);
//menampilkan nim yang belum diurutkan
System.out.println("Nilai sebelum diurutkan : ");
for (int i = 0; i < mhs.length; i++) {
System.out.print(mhs[i].getNilai() + "\n");
}
//menamplkan nim yang sudh diurutkan secara selectionsort
System.out.println("");
Larik.selectionsortobj(mhs);
System.out.println("\n\nNilai setelah diurutkan secara selectionsort ascending:");
for (int i = 0; i < mhs.length; i++) {
System.out.println("NAMA : " + mhs[i].getNama());
System.out.println("NILAI : " + mhs[i].getNilai()+"\n" );
}
}
}
Minggu, 30 Oktober 2011
Selection Sort Object Ascending
Label:
program java(netbeans)
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar