Category Archives: Algoritma dan Struktur Data 2

Tugas dan Referensi Mata Kuliah Algoritma dan Struktur Data 2

Method di Java

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

package nilaiArrayPerBaris;

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

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int array1[][] = {{1,2,3},{4,5,6}};
int array2[][] = {{1,2},{3},{4,5,6}};
System.out.println(“nilai array1 per baris :”);
cetakArray(array1); //displays array1 by row
System.out.println(“\nnilai array2 per baris :”);
cetakArray(array2); //displays array2 by row
} //end main

public static void cetakArray(int array[][] ){
//loop through array’s rows
for (int row = 0; row < array.length; row++){
//loop through columns of current row
for ( int column = 0; column < array[ row ].length; column++)
System.out.printf(“%2s” , array [ row ][ column ]);
System.out.println(); //start new line of output
}
}

}

Stack dengan Method di Java

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

package thestack;
import java.util.Scanner;
/**
*
* @author unej
*/
public class thisIsStack {
static Scanner input=new Scanner(System.in);
static int top=-1;
static Object[] dataStack=new Object[6];

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

push(36);
push(“test”);
push(“coba”);
push(“test lagi”);
peek();
push(“lho he ditest mane”);
push(“capek ah. . “);
push(“aduh pengen test lagi”);
push(false);

System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
//         System.out.println(“Data diPOP : “+pop());
int test =dataStack.length-1;
System.out.println(“panjang : “+test);
System.out.println(“Data diPEEK : “+peek());

}
///////////////////////////////////////////////////////////
public static void push(Object data){
if(top < dataStack.length-1)
dataStack[++top]=data;
else
System.out.println(“Data Sudah Penuh”);
}
///////////////////////////////////////////////////////////

public static Object peek(){
if(top>-1)
return dataStack[top];
else
return “Item Sudah Habis”;
}

///////////////////////////////////////////////////////////

public static Object pop(){
Object sementara;
if(top>-1)
sementara=dataStack[top--];
else
sementara=”Data Habis”;

return sementara;

}
///////////////////////////////////////////////////////////

public static void clear(){
top=-1;
}
}

Queue di Java

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

package thequeue;

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

static Object[] Queue=new Object[3];
static int front=0;
static int rear=-1;
static int item=0;

// push
public static void push(Object data){
if(rear<Queue.length-1){
Queue[++rear]=data;
item++;
}
else
System.out.println(“*Queue Penuh”);
}

// peekFront
public static Object peekFront(){
if(front==Queue.length){
front–;
Queue[front]=null;
}
return Queue[front];
}

//peekRear
public static Object peekRear(){
if(front==Queue.length){
Queue=new Object[3];
}
return Queue[rear];
}

// pop
public static Object pop(){
Object pop=0;
if(front<=Queue.length-1){
pop=Queue[front++];
item–;
}
else
System.out.println(“-Queue Sudah Kosong”);
return pop;
}

// cetak
public static void cetak(){
System.out.print(“[ ");
for(int i=front;i<=rear;i++){
System.out.print(Queue[i]+” “);
}
System.out.println(“]”);
}

// jumlahItem
public static int jumlahItem(){
return item;
}

// clear
public static void clear(){
front=0;
rear=-1;
item=0;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

// lakukan push
push(“Antrian 1″);
push(2);
push(“Ketiga”);
// cek jumlah item sekarang
System.out.println(“Jumlah Item : “+thisIsQueue.jumlahItem());
// hapus semua
clear();
// lakukan push
push(‘A’);
push(5);
push(666);
push(“Antrian Belakang”);
// cetak queue
cetak();
// lakukan peek
System.out.println(“Rear : “+peekRear());
System.out.println(“Front : “+peekFront());

// lakukan pop
System.out.println(“pop : “+pop());
System.out.println(“pop : “+pop());
System.out.println(“pop : “+pop());
pop();
// cek jumlah item
System.out.println(“Jumlah Item : “+jumlahItem());
System.out.println(peekRear());
System.out.println(peekFront());

}

}

Program Tahun Lahir Mahasiswa

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

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

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// memasukkan jumlah mahasiswa
System.out.print(“Masukkan Jumlah Mahasiswa : “);
Scanner read=new Scanner(System.in);
int jmlMahasiswa=read.nextInt();
int arrayTahun[]=new int [jmlMahasiswa];

// memasukkan tahun lahir mahasiswa
for(int i=0;i<jmlMahasiswa;i++){
System.out.print(“Masukkan Tahun Lahir Mahasiswa “+(i+1)+” : “);
int tahun=read.nextInt();
arrayTahun[i]=tahun;
}

// menentukan umur tertua dan termuda
int umurTua=arrayTahun[0];
int umurMuda=arrayTahun[0];
for(int i=0;i<arrayTahun.length;i++){
if(umurTua>arrayTahun[i]){
umurTua=arrayTahun[i];
}
if(umurMuda<arrayTahun[i]){
umurMuda=arrayTahun[i];
}
}
System.out.println(“Umur tertua : “+umurTua);
System.out.println(“Umur termuda : “+umurMuda);

// menentukan selisih umur setiap mahasiswa
int selisih;
for(int i=0;i<arrayTahun.length;i++){
selisih=umurMuda-arrayTahun[i];
System.out.println(“Selisih umur mahasiswa “+(i+1)+” : “+selisih);
}

}
}

Stack di Java

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

package stack;
import java.util.*;

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

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Stack dataStack=new Stack();

Scanner input=new Scanner(System.in);

System.out.println(“STACK : PUSH,POP,PEEK\n============================================”);
System.out.print(“Masukkan Panjang Stack : “);
int panjang=input.nextInt();
for(int i=0;i<panjang;i++){
System.out.print(“Masukkan Nilai Stack index-”+i+” : “);
dataStack.push(input.next());
}
// mencetak stack
System.out.println(“============================================\nStack = “+dataStack+”\n============================================”);

System.out.println(“PILIHAN :\n1.PUSH\n2.POP\n3.PEEK\n4.Cari Nilai Stack Element Terakhir (lastElement)\n5.Cari Nilai Stack Element Pertama (firstElement)\n6.Cari Nilai Stack Pada Index Tertentu (elementAt)\n7.Cari Tahu Panjang Stack (size)\n8.Hapus Nilai Stack Pada Index Tertentu (removeElementAt)\n9.Memasukkan Nilai Stack Pada Index Tertentu (insertElementAt)\n10.Menambahakan Element Stack (addElement)\n11.Hapus Seluruh Isi Stack (Clear)\n12.Exit”);
int pilih;
do{
System.out.print(“——————-\nPILIH :”);
pilih=input.nextInt();

if(pilih==1){
System.out.print(“PUSH : “);
dataStack.push(input.next());
System.out.println(“Setelah PUSH : “+dataStack);
}

if(pilih==2){
System.out.println(“POP : dilakukan”);
dataStack.pop();
System.out.println(“Setelah POP : “+dataStack);
}

if(pilih==3){
System.out.println(“PEEK (TOP): “+dataStack.peek());
}

if(pilih==4){
System.out.println(“Element Terakhir : “+dataStack.lastElement());
}
if(pilih==5){
System.out.println(“Element Pertama : “+dataStack.firstElement());
}

if(pilih==6){
System.out.print(“Masukkan nilai Index Stack : “);
int cari=input.nextInt();
// mencetak nilai stack pada index tertentu
System.out.println(“Element ke-”+cari+” = “+dataStack.elementAt(cari));
}
if(pilih==7){
// mencetak panjang index stack
System.out.println(“Panjang Stack = “+dataStack.size());
}

if(pilih==8){
System.out.print(“Masukkan Index Stack Yang Akan diHapus : “);
int hapus=input.nextInt();
// menghapus nilai stack pada index tertentu
dataStack.removeElementAt(hapus);
System.out.println(“Setelah remove data ke-”+hapus+” = “+dataStack);
}

if(pilih==9){
System.out.print(“Masukkan Index yang Akan diSisipkan Element : “);
int IndexSisip=input.nextInt();
System.out.print(“Masukkan Element Yang Akan diSisipkan : “);
// menambahkan nilai stack pada index tertentu
String Sisip=input.next();
dataStack.insertElementAt(Sisip, IndexSisip);
System.out.println(“Setelah Insert \””+Sisip+”\” di Index ke-”+IndexSisip+”, Stack = “+dataStack);
}
if(pilih==10){
System.out.print(“Masukkan Element Yang Akan diInsert-kan : “);
String Insert=input.next();
// menambahkan nilai stack pada index teratas
dataStack.addElement(Insert);
System.out.println(“Stack = “+dataStack);

}
if(pilih==11){
System.out.println(“Hapus Semua “);
dataStack.clear();
System.out.println(dataStack);

}
if(pilih==12){
System.out.println(“PROGRAM SELESAI”);
}
if(pilih>12 || pilih<1){
System.err.println(“PILIHAN SALAH !!”);
}

}

while(pilih!=12);

}

}

Sorting Pilihan Menggunakan Method

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

package sortingPilihan;
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
String ulang;
do{
Scanner data=new Scanner(System.in);

int[] array={5,8,4,3,6,9};
System.out.println(“PILIH JENIS SORTING”);
System.out.println(“1.bubble\n2.selection\n3.insertion”);
System.out.print(“Pilih : “);
int pilih=data.nextInt();

if(pilih==1){
bubble(array);
cetak(array);
}
if(pilih==2){
selection(array);
cetak(array);
}
if(pilih==3){
insertion(array);
cetak(array);
}
if(pilih!=1 && pilih!=2 && pilih!=3){
System.out.println(“Pilihan Tidak Ada – DATA TIDAK DI SORTING”);
}
System.out.print(“\n———————\nMau Sorting Lagi (y/n)”);
ulang=data.next();

}

while(ulang.equals(“y”));

}

public static void bubble(int array[]){
int sementara;
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length-1;j++){
if(array[j+1]<array[j]){
sementara=array[j];
array[j]=array[j+1];
array[j+1]=sementara;
}
}
}
System.out.println(“Anda menggunakan Bubble”);
}
public static void insertion(int array[]){
int sementara;
for(int i=1; i<array.length;i++){
sementara=array[i];
int j=i-1;
while(j>=0 && array[j]>sementara){
array[j+1]=array[j];
j–;
}
array[j+1]=sementara;
}
System.out.println(“Anda menggunakan Insertion”);
}
public static void selection(int array[]){
int sementara;
int min;
for(int i=0;i<array.length-1;i++){
min=i;
for(int j=i+1;j<array.length;j++){
if(array[j]<array[min]){
min=j;
}
}
if(min!=i){
sementara=array[min];
array[min]=array[i];
array[i]=sementara;
}
}
System.out.println(“Anda menggunakan Selection”);
}

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

Sorting Nama

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

package sortingNama;
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);

System.out.print(“Masukkan Jumlah Nama Yang Akan diSorting : “);
String[] nama=new String[data.nextInt()];

for(int i=0;i<nama.length;i++){
System.out.print("Masukkan Nama Ke-"+(i+1)+" : ");
nama[i]=data.next();
}

//sorting string
for(int i=0;i<nama.length;i++){
for(int j=0;j<nama.length-1;j++){
if(nama[j+1].compareTo(nama[j])<0){
String sementara=nama[j];
nama[j]=nama[j+1];
nama[j+1]=sementara;
}
}
}
//cetak
for(int i=0;i<nama.length;i++){
System.out.print(nama[i]+" ");

}
}
}

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();

}

}

Follow

Get every new post delivered to your Inbox.