Tugas Array Dan Inheritance Kelompok 14


Kelas : SI-40-03

Nama Anggota 14 :

1. Mohamad Azka Julda Suparman (1202164204)

2. Fadlil Muhammad (1202160019)

3. Briliant Marista Zakka Billy (1202160338)

16.

Soal :

Overload the method selectionShort in Listing 7.10 so that an array whose indiced range from first to last, where 0 <= first <= last, and last is less than the length of the array.

Penjelasan Soal :

Membuat sebuah metode selectionshort untuk mengurutkan angka di dalam array dari yang terkecil hingga yang terbesar, dengan menggunakan metode input oleh user

Script Code Java :

Overloading.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugaskelompok14;

/**

*

* @author M Azka

*/

public class Overloading {

public void selectionSort(int[] array){

for(int i=0; i<array.length – 1; i++){

int indexTerkecil = getIndexOfSmallest(i,array);

interchange(i, indexTerkecil, array);

}

}

public int getIndexOfSmallest(int startIndex, int[] a){

int min = a[startIndex];

int indexOfMin = startIndex;

for (int i = startIndex + 1; i < a.length; i++){

if (a[i] < min){

min = a[i];

indexOfMin = i;

}

}

return indexOfMin;

}

private void interchange(int i, int j, int[] a){

int temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

ApplikasiOverloading.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package tugaskelompok14;

import java.util.Scanner;

/**

*

* @author M Azka

*/

public class ApplikasiOverloading {

public static void display(int[] array, String kapan){

System.out.println("Nilai Array " + kapan + " diurutkan : ");

for(int i= 0; i < array.length; i++){

System.out.print(array[i] +" ");

}

System.out.println();

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Masukan Angka : ");

int F = input.nextInt();

System.out.print("Masukan Angka : ");

int A1 = input.nextInt();

System.out.print("Masukan Angka : ");

int D = input.nextInt();

System.out.print("Masukan Angka : ");

int L1 = input.nextInt();

System.out.print("Masukan Angka : ");

int I = input.nextInt();

System.out.print("Masukan Angka : ");

int L2 = input.nextInt();

Overloading o = new Overloading();

int[]a ={F,A1,D,L1,I,L2};

display(a, "sebelum");

o.selectionSort(a);

display(a, "setelah");

}

}

Hasil Output:



Leave a Reply