Evaluasi Tengah Semester Struktur Data 2021
Soal :
1. Jelaskan perbedaan struktur data primitif dengan Non primitif, berikan contohnya dalam program sederhana.
2. Jika diketahui notasi infiks = “A + B * C ^ D – E / F” bagaimana bentuk notasi postfiks dari notasi infiks tersebut jika menggunakan operasi stack. Tuliskan dalam bentuk program , dan tampilkan screenshotnya
3. Pada sebuah Bank, setiap nasabah yang datang diminta untuk mengambil antrian. Antrian tersebut memuat urutan layanan nasabah, dan jenis layanan yang dibutuhkan, apakah CS atau Teller.
a. Untuk membuat aplikasinya, struktur data apa yang tepat.
b. Tuliskan dan gambarkan struktur data untuk memuat informasinya
c. Implementasikan aplikasi antrian tersebut. (Link Antrian Bank)
4. Buatlah dokumentasi dalam bentuk source code , screenshot hasil, dan video Demo Presentasi yang dipost ke Youtube , kemudian diembedded di Blog masing-masing. Pengerjaan bisa berkelompok maksimal 3 orang, terakhir dikumpul 9 Mei 2021
Jawab :
- Struktur data primtif merupakan tipe data yang paling mendasar pada java. Tipe data ,size, dan bentuknya telah di definisikan/di tentukan oleh java itu sendiri. Contohnya : byte, short, int, long, char, float, double dan boolean. Begitupula Non primitif yang dimana kita sendiri yang membuatnya tipe data, size, dan bentuknya. Contohnya adalah array dan strig.
Contoh progam sturktur data primitif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class primitif
{
public static void main (String[] args){
byte var1;
short var2;
int var3;
long var4;
var1 = 120;
var2 = 32000;
var3 = 1000000000;
var4 = 1000000000000000L;
System.out.println("var1 = "+var1);
System.out.println("var2 = "+var2);
System.out.println("var3 = "+var3);
System.out.println("var4 = "+var4);
}
}
Output
public class primitif | |
{ | |
public static void main (String[] args){ | |
byte var1; | |
short var2; | |
int var3; | |
long var4; | |
var1 = 120; | |
var2 = 32000; | |
var3 = 1000000000; | |
var4 = 1000000000000000L; | |
System.out.println("var1 = "+var1); | |
System.out.println("var2 = "+var2); | |
System.out.println("var3 = "+var3); | |
System.out.println("var4 = "+var4); | |
} | |
} |
Contoh progam sturktur data non primitif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList;
import java.util.List;
public class Stack {
private List<Object> list = new ArrayList<Object>();
private int currentIndex = -1;
public void push(Object object){
list.add(object);
currentIndex++;
}
public Object pop(){
Object object = list.remove(currentIndex);
currentIndex--;
return object;
}
public Object peek(){
return list.get(currentIndex);
}
public int count(){
return list.size();
}
public void clear(){
list.clear();
currentIndex = -1;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App
{
public static void main( String[] args ){
Stack stack =new Stack();
stack.push("https://ibnumalik12.blogspot.com");
stack.push("5025201232");
stack.push("Ahmad Ibnu Malik Rahman");
System.out.println("Jumlah Data Pada Stack : " + stack.count());
System.out.println("Data Teratas Pada Stack : " + stack.peek());
System.out.println("===================================");
System.out.println("Objek yang dikeluarkan (Pop) : " + stack.pop());
System.out.println("Objek yang dikeluarkan (Pop) : " + stack.pop());
System.out.println("Jumlah Data Pada Stack setelah Pop: " + stack.count());
System.out.println("Data teratas pada stack : " + stack.peek());
}
}
Output
import java.util.ArrayList; | |
import java.util.List; | |
public class Stack { | |
private List<Object> list = new ArrayList<Object>(); | |
private int currentIndex = -1; | |
public void push(Object object){ | |
list.add(object); | |
currentIndex++; | |
} | |
public Object pop(){ | |
Object object = list.remove(currentIndex); | |
currentIndex--; | |
return object; | |
} | |
public Object peek(){ | |
return list.get(currentIndex); | |
} | |
public int count(){ | |
return list.size(); | |
} | |
public void clear(){ | |
list.clear(); | |
currentIndex = -1; | |
} | |
} |
public class App | |
{ | |
public static void main( String[] args ){ | |
Stack stack =new Stack(); | |
stack.push("https://ibnumalik12.blogspot.com"); | |
stack.push("5025201232"); | |
stack.push("Ahmad Ibnu Malik Rahman"); | |
System.out.println("Jumlah Data Pada Stack : " + stack.count()); | |
System.out.println("Data Teratas Pada Stack : " + stack.peek()); | |
System.out.println("==================================="); | |
System.out.println("Objek yang dikeluarkan (Pop) : " + stack.pop()); | |
System.out.println("Objek yang dikeluarkan (Pop) : " + stack.pop()); | |
System.out.println("Jumlah Data Pada Stack setelah Pop: " + stack.count()); | |
System.out.println("Data teratas pada stack : " + stack.peek()); | |
} | |
} |
Berikut program untuk mengubah infikx menjadi postfiks dengan menggunakan stack :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList;
import java.util.List;
public class Stack {
private List<Object> list = new ArrayList<Object>();
private int currentIndex = -1;
public void push(Object object){
list.add(object);
currentIndex++;
}
public Object pop(){
Object object = list.remove(currentIndex);
currentIndex--;
return object;
}
public Object peek(){
return list.get(currentIndex);
}
public int count(){
return list.size();
}
public void clear(){
list.clear();
currentIndex = -1;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack;
public class InfixToPostFix {
static int precedence(char c){
switch (c){
case '+': case '-':
return 0;
case '*': case '/': case '%':
return 1;
case '^':
return 2;
}
return -1;
}
static String infixToPostFix(String expression){
String result = "";
Stack<Character> stack = new Stack<>();
for (int i = 0; i <expression.length() ; i++) {
char c = expression.charAt(i);
//check if char is operator
if(Character.isLetterOrDigit(c)){
result += c;
}else if(c=='('){
stack.push(c);
}else if(c==')'){
while(!stack.isEmpty() && !stack.peek().equals('('))
result += stack.pop();
stack.pop();
}else{
while(!stack.isEmpty() && precedence(stack.peek()) >= precedence(c))
result += stack.pop();
stack.push(c);
}
}
while(!stack.isEmpty() && !stack.peek().equals('('))
result += stack.pop();
return result;
}
public static void main(String[] args) {
String exp = "A+B*C^D–E/F";
System.out.println("Infix Expression: " + exp);
System.out.println("Postfix Expression: " + infixToPostFix(exp));
}
}
Output
import java.util.ArrayList; | |
import java.util.List; | |
public class Stack { | |
private List<Object> list = new ArrayList<Object>(); | |
private int currentIndex = -1; | |
public void push(Object object){ | |
list.add(object); | |
currentIndex++; | |
} | |
public Object pop(){ | |
Object object = list.remove(currentIndex); | |
currentIndex--; | |
return object; | |
} | |
public Object peek(){ | |
return list.get(currentIndex); | |
} | |
public int count(){ | |
return list.size(); | |
} | |
public void clear(){ | |
list.clear(); | |
currentIndex = -1; | |
} | |
} |
import java.util.Stack; | |
public class InfixToPostFix { | |
static int precedence(char c){ | |
switch (c){ | |
case '+': case '-': | |
return 0; | |
case '*': case '/': case '%': | |
return 1; | |
case '^': | |
return 2; | |
} | |
return -1; | |
} | |
static String infixToPostFix(String expression){ | |
String result = ""; | |
Stack<Character> stack = new Stack<>(); | |
for (int i = 0; i <expression.length() ; i++) { | |
char c = expression.charAt(i); | |
//check if char is operator | |
if(Character.isLetterOrDigit(c)){ | |
result += c; | |
}else if(c=='('){ | |
stack.push(c); | |
}else if(c==')'){ | |
while(!stack.isEmpty() && !stack.peek().equals('(')) | |
result += stack.pop(); | |
stack.pop(); | |
}else{ | |
while(!stack.isEmpty() && precedence(stack.peek()) >= precedence(c)) | |
result += stack.pop(); | |
stack.push(c); | |
} | |
} | |
while(!stack.isEmpty() && !stack.peek().equals('(')) | |
result += stack.pop(); | |
return result; | |
} | |
public static void main(String[] args) { | |
String exp = "A+B*C^D–E/F"; | |
System.out.println("Infix Expression: " + exp); | |
System.out.println("Postfix Expression: " + infixToPostFix(exp)); | |
} | |
} |
import java.util.Scanner; | |
public class AntrianBank | |
{ | |
public static void main(String[] args){ | |
Queue queueCS = new Queue(1000); | |
Queue queueTeller = new Queue(1000); | |
int layanan, keperluan, noAntrianCS = 1, noAntrianTeller = 1; | |
Scanner input = new Scanner(System.in); | |
while(true){ | |
System.out.println("\n===============Selamat Datang==============="); | |
System.out.println(); | |
System.out.println("Silahkan Pilih Layanan"); | |
System.out.println("1. Customer Service"); | |
System.out.println("2. Teller"); | |
System.out.println("3. Keluar"); | |
layanan = input.nextInt(); | |
if(layanan == 3){ | |
System.out.println("===============Terima Kasih==============="); | |
break; | |
} | |
switch(layanan){ | |
case 1: | |
System.out.println("Pilih Keperluan Anda"); | |
System.out.println("1. Buka rekening bank"); | |
System.out.println("2. Konsultasi masalah rekening"); | |
System.out.println("3. Informasi saldo dan mutasi nasabah"); | |
System.out.println("4. Administrasi buku cek dan tabungan"); | |
System.out.println("5. Kembali ke layanan"); | |
keperluan = input.nextInt(); | |
if(keperluan >= 1 && keperluan <=4){ | |
queueCS.enqueue(noAntrianCS); | |
System.out.println("Nomor antrian anda adalah: " + noAntrianCS); | |
System.out.print("Keperluan: " ); | |
noAntrianCS++; | |
if(keperluan == 1){ | |
System.out.println("Buka rekening bank"); | |
} | |
if(keperluan == 2){ | |
System.out.println("Konsultasi masalah rekening"); | |
} | |
if(keperluan == 3){ | |
System.out.println("Informasi saldo dan mutasi nasabah"); | |
} | |
if(keperluan == 4){ | |
System.out.println("Administrasi buku cek dan tabungan"); | |
} | |
} | |
else if(keperluan == 5){ | |
break; | |
} | |
else{ | |
System.out.println("Angka yang anda masukkan salah"); | |
} | |
break; | |
case 2: | |
System.out.println("Pilih Keperluan Anda"); | |
System.out.println("1. Penyetoran tabungan dan deposito"); | |
System.out.println("2. Pencatatan tabungan dan deposito"); | |
System.out.println("3. Pencatatan transaksi buku tabungan"); | |
System.out.println("4. Kembali ke layanan"); | |
keperluan = input.nextInt(); | |
if(keperluan >= 1 && keperluan <=3){ | |
queueTeller.enqueue(noAntrianTeller); | |
System.out.println("Nomor antrian anda adalah: " + noAntrianTeller); | |
System.out.print("Keperluan: " ); | |
noAntrianTeller++; | |
if(keperluan == 1){ | |
System.out.println("Penyetoran tabungan dan deposito"); | |
} | |
if(keperluan == 2){ | |
System.out.println("Pencatatan tabungan dan deposito"); | |
} | |
if(keperluan == 3){ | |
System.out.println("Pencatatan transaksi buku tabungan"); | |
} | |
} | |
else if(keperluan == 4){ | |
break; | |
} | |
else{ | |
System.out.println("Angka yang anda masukkan salah"); | |
} | |
break; | |
default: | |
System.out.println("Angka yang anda masukkan salah"); | |
} | |
} | |
} | |
} |
Komentar
Posting Komentar