/**
* Title : Pass by reference
* Deskripsi: Mendemonstrasikan cara melewatkan argumen secara acuan/reference ke method
*/
class PassByReference {
/** Main method */
public static void main(String[] arguments) {
int arrBilangan[] = {7,0,5,0,2,0,0,9};
PassByReference pBR = new PassByReference();
int jumlahNol;
System.out.print("Array bilangan : [ ");
for (int i =0; i < arrBilangan.length; i++) {
System.out.print(arrBilangan[i] + " ");
}
System.out.println("]");
jumlahNol = pBR.tukar(arrBilangan);
System.out.println("\nJumlah elemen '0' = " + jumlahNol);
System.out.print("\nArray bilangan yang baru : [ ");
for (int i = 0; i < arrBilangan.length; i++) {
System.out.print(arrBilangan[i] + " ");
}
System.out.println("]");
}
/** Method 'tukar' untuk menukar nol ke satu */
int tukar(int varArray[]) {
int jumlah = 0;
System.out.println("\nMethod menukar '0' menjadi '1' ");
System.out.println("-------------------------------");
for (int i = 0; i < varArray.length; i++) {
if (varArray[i] == 0) {
jumlah++;
System.out.println("Menukar elemen pada indeks :" + i);
varArray[i] = 1;
}
}
return jumlah;
}
}
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment