Evaluasi Tengah Semester Struktur Data 2021
Nama : Afira Rolobessy
NRP
: 5025201006
Kelas : F Struktur Data
Soal :
1. Jelaskan perbedaan struktur data primitif
dengan Non primitif, berikan contohnya dalam program sederhana.
Primitive data type adalah tipe data yang
telah terdefinisi(supported) di suatu bahasa pemrograman.
Tipe
data primitif :
1)
Logika
= boolean
2)
Karater=
char
3)
Bilangan
bulat = byte, short, int, long
4) Bilangan pecahan = float & double
/** | |
* Write a description of class primitive here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class primitive | |
{ | |
public static void main (String[] args) { | |
String nama = "Mahasiswa"; | |
int jumlah = nama.length(); | |
//tipe data Primitive | |
System.out.println("Nama:" + nama +" jumlah : " + jumlah); | |
//tampilkan output | |
} | |
} |
public class primitive | |
{ | |
public static void main (String[] args) { | |
String nama = "Mahasiswa"; | |
//tipe data Non primitif | |
System.out.println(nama.concat("mahasiswa")); | |
//tampilkan output | |
} | |
} |
import java.util.Stack; | |
class Test | |
{ | |
//check the operator's precedences | |
static int precedence(char ch) | |
{ | |
switch (ch) | |
{ | |
case '+': | |
case '-': | |
return 1; | |
case '*': | |
case '/': | |
return 2; | |
case '^': | |
return 3; | |
} | |
return -1; | |
} | |
//Converting Infix to Postfix expression | |
//Main Method | |
//String for infix in input parameter | |
//String for postfix fro output | |
static String infixToPostfix(String exp) | |
{ | |
/* Use Stack to store operators */ | |
//stack init | |
String result = new String(""); | |
Stack<Character> stack = new Stack<>(); | |
/* Use Queue to store answer */ | |
//queu init | |
//use input lenght to initialize queue max capacity | |
for (int i = 0; i<exp.length(); ++i) | |
{ | |
char c = exp.charAt(i); | |
//if scanned character is letter or digit | |
//add to queue | |
if (Character.isLetterOrDigit(c)) | |
result += c; | |
//if scanned character is '(' | |
//add to stack | |
else if (c == '(') | |
stack.push(c); | |
//if scanned character is ')' | |
//move out all the stack elements till '(' is encountered to queue | |
else if (c == ')') | |
{ | |
while (!stack.isEmpty() && | |
stack.peek() != '(') | |
result += stack.pop(); | |
stack.pop(); | |
} | |
else | |
{ | |
while (!stack.isEmpty() && precedence(c) | |
<= precedence(stack.peek())){ | |
result += stack.pop(); | |
} | |
stack.push(c); | |
} | |
} | |
//if all input string already processed | |
//move all elements from stack to queue | |
while (!stack.isEmpty()){ | |
if(stack.peek() == '(') | |
return "Invalid Expression"; | |
result += stack.pop(); | |
} | |
return result; | |
} | |
//output display | |
public static void main(String[] args) | |
{ | |
String exp = "A+B*C^D–E/F"; | |
System.out.println(infixToPostfix(exp)); | |
} | |
} |
Jadi dari gambar tersebut mengambarkan antrian nasabah dalam bentuk queue. Jadi pertama nasabah mengantri ,kemudian antrian nasabah di buat ,jadi data antri Nasabah di ambil dari ujung belakang yang paling bawah atau orang yang peratama antri ,hal tersebut Nasabah pertama kali masuk akan menjadi data yang pertama kali keluar.
/** | |
* pemograman Bank | |
* | |
* @author Afira Rolobessy | |
* @version 9 Mei 2021 | |
*/ | |
import java.util.Scanner; | |
public class bank{ | |
int id; | |
String nama,perlu; | |
bank next; | |
static Scanner in=new Scanner(System.in); | |
static Scanner str=new Scanner(System.in); | |
public void input(){ | |
//Memasukkan layanan yang diperlukan nasabah | |
System.out.print("Masukkan id : "); | |
id=in.nextInt(); | |
System.out.print("Masukkan nama : "); | |
nama=str.nextLine(); | |
System.out.print("Masukkan keperluan : "); | |
perlu=str.nextLine(); | |
next=null; | |
} | |
public void read(){ | |
System.out.println("|| "+id+" \t|| "+nama+" \t|| "+perlu+" \t||"); | |
} | |
public static void main(String[] args){ | |
int menu=0; | |
linked que=new linked(); | |
while(menu!=4){ | |
System.out.print("1.Enqueue\n2.Dequeue\n3.View\n4.Exit\n : "); | |
menu=in.nextInt(); | |
if(menu==1)que.enque(); | |
else if(menu==2)que.deque(); | |
else if(menu==3)que.view(); | |
else if(menu==4)System.out.println("- keluar -"); | |
else System.out.println("- Salah -"); | |
System.out.println(""); | |
} | |
} | |
} | |
class linked{ | |
bank kepala,ekor; | |
public linked(){ | |
kepala=null; | |
ekor=null; | |
} | |
//Buat objek baru dari class bank | |
public void enque(){ | |
bank baru=new bank(); | |
baru.input(); | |
if(kepala==null)kepala=baru; | |
else ekor.next=baru; | |
ekor=baru; | |
} | |
//Tampilkan kosong, lanjut | |
public void deque(){ | |
if(kepala==null)System.out.println("- Empty -"); | |
else{ | |
System.out.println("Keluar Data Dengan Id : "+kepala.id); | |
kepala=kepala.next; | |
} | |
} | |
//Panggil method read dari objek a | |
public void view(){ | |
if(kepala==null)System.out.println("-Empty -"); | |
else{ | |
System.out.println("|| Id \t|| Nama \t|| Keperluan \t||"); | |
for(bank a=kepala; a!=null; a=a.next) a.read(); | |
} | |
} | |
} |
Komentar
Posting Komentar