Evaluasi Tengah Semester Struktur Data 2021
1. Jelaskan perbedaan struktur data primitif dengan Non primitif, berikan contohnya dalam program sederhana.
Struktur data primitif merupakan tipe sederhana yang hanya memiliki sebuah value, tanpa memiliki properti dan method lainnya. Tipe data ini sudah built-in pada setiap bahasa pemrograman. Contoh struktur data primitif pada java yaitu :
- boolean
- byte
- char
- short
- int
- long
- float
- double
Sedangkan struktur data non primitif merupakan struktur data buatan yang memiliki beberapa properti dan method khusus, disamping adanya value itu sendiri. Contoh struktur data non primitif pada java yaitu
- String
- List
- LinkedList
- Stack
- Queue
Berikut ini merupakan contoh program implementasi kedua jenis tipe data tersebut.
package ets.dataStructure; | |
/** | |
* This class represents primitive data types demonstration | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class Primitive { | |
/** | |
* This is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
// BOOLEAN | |
int a = 10; | |
int b = 8; | |
boolean isTrue = a > b; | |
System.out.println(a + " > " + b + " is " + isTrue); | |
// BYTE | |
byte age = 20; | |
System.out.println("age : " + age); | |
// CHAR | |
char firstChar = 'A'; | |
System.out.println("First alphabet character : " + firstChar); | |
// SHORT | |
short height = 190; | |
System.out.println("Height : " + height); | |
// INT | |
int num = 276325; | |
System.out.println("Number : " + num); | |
// LONG | |
long population = 7155328792L; | |
System.out.println("Current population : " + population); | |
// FLOAT | |
float weight = 61.5F; | |
System.out.println("Weight (kg) : " + weight); | |
// DOUBLE | |
double netWorth = 251757488950.55; | |
System.out.println("Net worth : " + netWorth); | |
} | |
} |
package ets.dataStructure; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* This class represents non primitive data types demonstration | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class NonPrimitive { | |
/** | |
* This is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
// STRING | |
String name = "Fajar Zuhri Hadiyanto"; | |
System.out.println(name); | |
// LIST (ARRAY LIST) | |
List<Integer> scores = new ArrayList<>(); | |
scores.add(87); | |
scores.add(90); | |
scores.add(92); | |
scores.add(84); | |
System.out.println(scores.toString()); | |
// LINKED LIST | |
LinkedList<String> faculty = new LinkedList<>(); | |
faculty.add("ELECTICS"); | |
faculty.add("SCIENTICS"); | |
faculty.add("MARTECH"); | |
faculty.add("INDSYS"); | |
faculty.add("CIVPLAN"); | |
faculty.add("CREABIZ"); | |
faculty.add("VOCATION"); | |
System.out.println(faculty.toString()); | |
} | |
} |
Berikut ini merupakan hasil jika masing-masing file tersebut dijalankan.
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.
Berikut ini merupakan program untuk mengkonversi ekspresi infix menjadi ekspresi postfix.
package ets.expression; | |
import java.util.Stack; | |
/** | |
* This class represents expression converter tools | |
* between infix and postfix | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class ExpressionConverter { | |
/** | |
* This method is used to get the precedence level of the operator | |
* | |
* @param operator operator to be looked for the precedence level | |
* @return precedence level of the operator | |
* */ | |
private static int precedence(char operator){ | |
switch (operator) { | |
case '+': | |
case '-': | |
return 1; | |
case '*': | |
case '/': | |
case '%': | |
return 2; | |
case '^': | |
return 3; | |
default: | |
return -1; | |
} | |
} | |
/** | |
* This method is used to convert infix expression to postfix expression | |
* | |
* @param expression infix expression to be converted | |
* @return postfix expression | |
* */ | |
public static String infixToPostfix(String expression) { | |
// PREPARE NEW STACK FOR OPERATOR AND | |
// STRING FOR THE FINAL RESULT | |
Stack<Character> operator = new Stack<>(); | |
StringBuilder result = new StringBuilder(); | |
// REPEAT FOR EACH CHARACTER IN THE EXPRESSION | |
for (char a: expression.toCharArray()) { | |
// IF CHARACTER IS A LETTER OR A DIGIT ... | |
if(Character.isLetterOrDigit(a)) { | |
// ... JUST PUSH IT TO THE FINAL RESULT | |
result.append(a); | |
} | |
// IF CHARACTER IS AN OPENING PARENTHESES ... | |
else if(a == '(') { | |
// ... PUSH IT TO THE OPERATOR STACK | |
operator.push(a); | |
} | |
// IF CHARACTER IS A CLOSING PARENTHESES ... | |
else if(a == ')') { | |
// ... MOVE ALL OPERATORS BETWEEN THIS CLOSING AND | |
// NEAREST OPENING PARENTHESES TO THE FINAL RESULT ... | |
while(operator.size() != 0 && operator.peek() != '(') { | |
result.append(operator.pop()); | |
} | |
// ... AND ALSO THROW THE OPENING PARENTHESES | |
operator.pop(); | |
} | |
// IF CHARACTER IS AN ARITHMETIC OPERATOR ... | |
else { | |
// ... MOVE ALL OPERATORS IN STACK THAT HAVE | |
// PRECEDENCE LOWER OR EQUAL TO THIS OPERATOR ... | |
while(operator.size() != 0 && precedence(a) <= precedence(operator.peek())) { | |
result.append(operator.pop()); | |
} | |
// ... THEN, PUSH THE CHARACTER TO THE STACK | |
operator.push(a); | |
} | |
} | |
// MOVE THE REST OPERATORS TO THE QUEUE | |
while(operator.size() != 0) { | |
if(operator.peek() == '(') { | |
return "Invalid Expression"; | |
} | |
result.append(operator.pop()); | |
} | |
// RETURN THE RESULT IN STRING | |
return result.toString(); | |
} | |
} |
package ets.expression; | |
import java.util.Scanner; | |
/** | |
* This class represents console application for converting expression | |
* from infix expression to postfix expression | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class ExpressionConverterApp { | |
/** | |
* This is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
// PREPARATION | |
String input, output; | |
Scanner scanner = new Scanner(System.in); | |
// GET THE INFIX EXPRESSION | |
System.out.print("Input the infix expression : "); | |
input = scanner.nextLine(); | |
// CONVERT INFIX TO POSTFIX EXPRESSION | |
output = ExpressionConverter.infixToPostfix(input); | |
// DISPLAY BOTH INFIX AND POSTFIX EXPRESSION | |
System.out.println("Infix Expression : " + input); | |
System.out.println("Postfix Expression : " + output); | |
} | |
} |
Jika program tersebut dijalankan, maka hasilnya akan menjadi seperti berikut.
Dari hasil diatas, terlihat jika notasi infix-nya "A + B * C ^ D – E / F", maka notasi postfixnya yaitu "ABCD^*+EF/-".
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.
Sistem antrian pada bank menggunakan implementasi struktur data queue.
b. Tuliskan dan gambarkan struktur data untuk memuat informasinya
Pada gambar di atas, terlihat kalau di bank, terdapat beberapa line/konter untuk teller, maupun customer service, maka dari itu, untuk menyimpan data teller, digunakan tipe data array of queue, karena antrian terbagi menjadi beberapa line/konter untuk teller. Begitu pula dengan customer service yang juga menggunakan tipe data array of queue, karena antrian customer service juga terbagi menjadi beberapa line/konter. Masing-masing queue menyimpan data customer, dimana objek customer ini menyimpan beberapa data, yaitu :
- urutan antrian customer itu sendiri, 1, 2, 3, dan seterusnya
- jenis antrian customer, dapat berupa teller atau customer service
- tiket customer, biasanya berupa kode unik yang diikuti dengan nomor antrian (contoh T001, C001, dan sebagainya)
c. Implementasikan aplikasi antrian tersebut.
Berikut ini merupakan implementasi dari sistem antrian bank dalam bentuk aplikasi konsol.
package ets.bank; | |
import java.util.*; | |
/** | |
* This class represents console application of bank queue machine | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class BankQueueApp { | |
/** Field to contain number of teller queue line/counter. */ | |
private static int nTellerLine; | |
/** Field to contain current teller line/counter to be added by new customer. */ | |
private static int currentTellerLine; | |
/** Field to contain current teller line/counter to do a service. */ | |
private static int currentTellerServiceLine; | |
/** Field to contain current order of teller queue. */ | |
private static int currentTellerQueueNumber; | |
/** Field to contain list of teller queue*/ | |
private static Queue<Customer>[] tellerQueue; | |
/** Field to contain number of customer service queue line/counter. */ | |
private static int nCSLine; | |
/** Field to contain current customer service line/counter to be added by new customer. */ | |
private static int currentCSLine; | |
/** Field to contain current customer service line/counter to do a service. */ | |
private static int currentCSServiceLine; | |
/** Field to contain current order of customer service queue. */ | |
private static int currentCSQueueNumber; | |
/** Field to contain list of customer service queue*/ | |
private static Queue<Customer>[] CSQueue; | |
/** | |
* Static field to contain all function with {@link Integer} key | |
* to represent function order in the app menu, then {@link Function} | |
* as a value | |
* */ | |
private static HashMap<Integer, BankQueueApp.Function> functions; | |
/** | |
* This static class represents function object | |
* */ | |
static class Function { | |
/** | |
* Field to contain string representation of function as | |
* function name to be shown in the app menu | |
* */ | |
private final String name; | |
/** Field to contain function to be executed */ | |
private final Runnable function; | |
/** | |
* This construction is used to initialize {@link Function} object | |
* | |
* @param name {@link String} representation of the function object | |
* @param function {@link Runnable} object of function to be executed | |
* */ | |
Function(String name, Runnable function) { | |
this.name = name; | |
this.function = function; | |
} | |
/** | |
* This method is used to get the name of the function | |
* | |
* @return field {@link #name} | |
* */ | |
public String getName() { | |
return this.name; | |
} | |
/** | |
* This method is used to run the {@link #function} field | |
* */ | |
void run() { | |
this.function.run(); | |
} | |
} | |
/** | |
* This method is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
initApp(); | |
initQueue(); | |
appMenu(); | |
} | |
/** | |
* This is a utility method to get a new {@link Scanner} object | |
* | |
* @return new {@link Scanner} object | |
* */ | |
private static Scanner scanner() { | |
return new Scanner(System.in); | |
} | |
/** | |
* This method is used to initialize the application by setting the app menu | |
* */ | |
private static void initApp() { | |
// INITIATE THE APP MENU | |
functions = new HashMap<>(); | |
// ADD MENU TO THE APP | |
functions.put(1, new Function("Add Teller Queue", BankQueueApp::tellerQueue)); | |
functions.put(2, new Function("Add Customer Service Queue", BankQueueApp::csQueue)); | |
functions.put(3, new Function("Do Teller Service", BankQueueApp::tellerService)); | |
functions.put(4, new Function("Do CS Service", BankQueueApp::csService)); | |
functions.put(5, new Function("Display Queue", BankQueueApp::display)); | |
functions.put(6, new Function("Reset", BankQueueApp::initQueue)); | |
functions.put(7, new Function("Exit", BankQueueApp::exit)); | |
} | |
/** | |
* This method is used to initiate the queue in the bank by | |
* setting how many teller line/counter and how many customer | |
* service line/counter | |
* */ | |
private static void initQueue() { | |
// INSERT HOW MANY TELLER QUEUE AND CS QUEUE | |
System.out.print("How many teller line : "); | |
nTellerLine = scanner().nextInt(); | |
System.out.print("How many customer service line : "); | |
nCSLine = scanner().nextInt(); | |
// INITIATE TELLER QUEUE | |
tellerQueue = new Queue[nTellerLine]; | |
for (int i = 0; i < tellerQueue.length; i++) { | |
tellerQueue[i] = new Queue<>(); | |
} | |
currentTellerLine = 0; | |
currentTellerServiceLine = 0; | |
currentTellerQueueNumber = 1; | |
// INITIATE CUSTOMER SERVICE QUEUE | |
CSQueue = new Queue[nCSLine]; | |
for (int i = 0; i < CSQueue.length; i++) { | |
CSQueue[i] = new Queue<>(); | |
} | |
currentCSLine = 0; | |
currentCSServiceLine = 0; | |
currentCSQueueNumber = 1; | |
} | |
/** | |
* This method is used to print all menu of the app to be selected by user. | |
* */ | |
private static void appMenu() { | |
System.out.println("==========| PROGRAM MENU |=========="); | |
for (Map.Entry<Integer, BankQueueApp.Function> function: functions.entrySet()) { | |
System.out.println(function.getKey() + ". " + function.getValue().getName()); | |
} | |
programSwitcher(); | |
} | |
/** | |
* This method is used to handling user selection of the app menu | |
* */ | |
private static void programSwitcher() { | |
System.out.print("Choose Menu [1 - 7]: "); | |
int indexMenu = scanner().nextInt(); | |
if (1 <= indexMenu && indexMenu <= 7) | |
functions.get(indexMenu).run(); | |
appMenu(); | |
} | |
/** | |
* This method is used to handle the first menu of the app, | |
* which is add customer to a teller queue. | |
* */ | |
private static void tellerQueue() { | |
Customer newCustomer = new Customer(currentTellerQueueNumber++, 1); | |
tellerQueue[currentTellerLine++].enqueue(newCustomer); | |
System.out.println("Add new Customer with ticket " + newCustomer + " at Teller " + currentTellerLine); | |
currentTellerLine %= nTellerLine; | |
scanner().nextLine(); | |
} | |
/** | |
* This method is used to handle the second menu of the app, | |
* which is add customer to a customer service queue. | |
* */ | |
private static void csQueue() { | |
Customer newCustomer = new Customer(currentCSQueueNumber++, 2); | |
CSQueue[currentCSLine++].enqueue(newCustomer); | |
System.out.println("Add new customer with ticket " + newCustomer + " at customer service " + currentCSLine); | |
currentCSLine %= nCSLine; | |
scanner().nextLine(); | |
} | |
/** | |
* This method is used to handle the third menu of the app, | |
* which is do a service in a teller line/counter | |
* */ | |
private static void tellerService() { | |
if (!tellerQueue[currentTellerServiceLine].isEmpty()) { | |
System.out.println("Please " + tellerQueue[currentTellerServiceLine].head() + " go to teller " + (currentTellerServiceLine + 1)); | |
tellerQueue[currentTellerServiceLine++].dequeue(); | |
} else { | |
System.out.println("Teller " + ++currentTellerServiceLine + " is empty"); | |
} | |
currentTellerServiceLine %= nTellerLine; | |
scanner().nextLine(); | |
} | |
/** | |
* This method is used to handle the fourth menu of the app, | |
* which is do a service in a customer service line/counter | |
* */ | |
private static void csService() { | |
if (!CSQueue[currentCSServiceLine].isEmpty()) { | |
System.out.println("Please " + CSQueue[currentCSServiceLine].head() + " go to customer service " + (currentCSServiceLine + 1)); | |
CSQueue[currentCSServiceLine++].dequeue(); | |
} else { | |
System.out.println("Teller " + ++currentCSServiceLine + " is empty"); | |
} | |
currentCSServiceLine %= nCSLine; | |
scanner().nextLine(); | |
} | |
/** | |
* This method is used to handle the fifth menu of the app, | |
* which is display the next customer to be serviced by | |
* corresponding line/counter | |
* */ | |
private static void display() { | |
// DISPLAY TELLER QUEUE | |
System.out.println("==========| TELLER NEXT SERVICE |=========="); | |
for (int i = 1; i <= nTellerLine; i++) System.out.print(".----------"); | |
System.out.println("."); | |
for (int i = 1; i <= nTellerLine; i++) System.out.print("| Teller " + i + " "); | |
System.out.println("|"); | |
for (int i = 1; i <= nTellerLine; i++) System.out.print("|----------"); | |
System.out.println("|"); | |
for (int i = 0; i < nTellerLine; i++) { | |
if (tellerQueue[i].head() != null) System.out.print("| " + tellerQueue[i].head() + " "); | |
else System.out.print("| "); | |
} | |
System.out.println("|"); | |
for (int i = 0; i < nTellerLine; i++) System.out.print("'----------"); | |
System.out.println("'\n"); | |
// DISPLAY CUSTOMER SERVICE QUEUE | |
System.out.println("==========| CS NEXT SERVICE |=========="); | |
for (int i = 1; i <= nCSLine; i++) System.out.print(".------"); | |
System.out.println("."); | |
for (int i = 1; i <= nCSLine; i++) System.out.print("| CS " + i + " "); | |
System.out.println("|"); | |
for (int i = 1; i <= nCSLine; i++) System.out.print("|------"); | |
System.out.println("|"); | |
for (int i = 0; i < nCSLine; i++) { | |
if (CSQueue[i].head() != null) System.out.print("| " + CSQueue[i].head() + " "); | |
else System.out.print("| "); | |
} | |
System.out.println("|"); | |
for (int i = 0; i < nCSLine; i++) System.out.print("'------"); | |
System.out.println("'"); | |
// Enter to go to the main menu | |
scanner().nextLine(); | |
} | |
/** | |
* This method is used to handle the seventh menu of the app, | |
* which is exit from the app. | |
* */ | |
private static void exit() { | |
System.exit(0); | |
} | |
} |
package ets.bank; | |
/** | |
* This class represents customer as an object of queue | |
* consist of queue number and queue type (either Teller | |
* or Customer Service) | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* */ | |
public class Customer { | |
/** Field to contain order in the queue */ | |
private final int queueNumber; | |
/** | |
* Field to contain integer representation of queue type. | |
* <ul> | |
* <li>1 for teller</li> | |
* <li>2 for customer service</li> | |
* </ul> | |
* */ | |
private final int queueType; | |
/** | |
* Field to contain formatted ticket value based on the | |
* queue type and the queue number. | |
* */ | |
private String queueTicket; | |
/** | |
* This constructor will set the {@link #queueNumber} and {@link #queueType} | |
* directly from the given argument. This will also set the {@link #queueTicket} | |
* from method {@link #setQueueTicket()} | |
* | |
* @param queueNumber order in the queue | |
* @param queueType integer representation of queue type | |
* */ | |
public Customer(int queueNumber, int queueType) { | |
this.queueNumber = queueNumber; | |
this.queueType = queueType; | |
this.setQueueTicket(); | |
} | |
/** | |
* This method will set the {@link #queueTicket} by, first depends on the queue type | |
* <ul> | |
* <li>if queue type is 1 (teller), then queue ticket will have prefix 'T'</li> | |
* <li>if queue type is 2 (customer service), then queue ticket will have prefix 'C'</li> | |
* </ul> | |
* Then followed by 3 digit formatted queue order (001, 027, 173, etc.) | |
* */ | |
private void setQueueTicket() { | |
StringBuilder queue = new StringBuilder(); | |
if (this.queueType == 1) queue.append("T"); | |
else queue.append("C"); | |
queue.append(String.format("%03d", this.queueNumber)); | |
this.queueTicket = queue.toString(); | |
} | |
/** | |
* This method will get the string representation of customer object | |
* by return field {@link #queueTicket}. | |
* | |
* @return string representation of customer object | |
* */ | |
@Override | |
public String toString() { | |
return this.queueTicket; | |
} | |
} |
package ets.bank; | |
/** | |
* This class represents queue data structure with specific type of each data. | |
* This class implement behavior of linked list. | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since May 7th 2021 | |
* | |
* @param <E> type of each data in the queue object | |
* */ | |
public class Queue<E> { | |
/** Field to contain size of the queue, or the number of data in the queue. */ | |
private int size; | |
/** Field to contain head node of the queue. */ | |
private Node head; | |
/** Field to contain tail node of the queue. */ | |
private Node tail; | |
/** | |
* This inner class represents node that contains data of the queue. | |
* */ | |
class Node { | |
/** Field to contain main data of the node. */ | |
private final E data; | |
/** Field to contain reference to next node of the queue. */ | |
private Node next; | |
/** | |
* This constructor will initiate new node by a certain value for the new node object. | |
* @param data {@link E} type data to be set as value of the new node. | |
* */ | |
public Node(E data) { | |
this.data = data; | |
} | |
/** | |
* This method will get the string representation of node object | |
* by return field {@link #data}. | |
* | |
* @return string representation of node object | |
* */ | |
@Override | |
public String toString() { | |
return this.data.toString(); | |
} | |
} | |
/** | |
* This constructor will initiate new queue object | |
* by set the size to 0, head and tail to null | |
* */ | |
public Queue() { | |
this.size = 0; | |
this.head = this.tail = null; | |
} | |
/** | |
* This method is used to check if the queue is empty (does not have any data) or not | |
* @return true if the size is 0, else false | |
* */ | |
public boolean isEmpty() { | |
return this.size == 0; | |
} | |
/** | |
* This method is used to add new data to the queue. | |
* @param data {@link E} type data to be added to the queue | |
* */ | |
public void enqueue(E data) { | |
Node newNode = new Node(data); | |
if (this.isEmpty()) this.head = this.tail = newNode; | |
else { | |
this.tail.next = newNode; | |
this.tail = newNode; | |
} | |
this.size++; | |
} | |
/** | |
* This method is used to remove the front data of the queue. | |
* if the queue is already empty, then it will throw an error | |
* @throws IllegalStateException when the queue is already empty as program try to run this method | |
* */ | |
public void dequeue() throws IllegalStateException { | |
if (this.isEmpty()) { | |
throw new IllegalStateException("The queue is already empty !"); | |
} else { | |
this.head = this.head.next; | |
if (this.head == null) this.tail = null; | |
} | |
this.size--; | |
} | |
/** | |
* This method is used to get the head data of the queue. | |
* if the queue is empty, then return null. | |
* | |
* @return {@link E} type of head data of the queue if the queue is not empty, else null | |
* */ | |
public E head() { | |
return !this.isEmpty() ? this.head.data : null; | |
} | |
} |
ketika aplikasi tersebut dijalankan, maka hasilnya akan menjadi seperti berikut.
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
Source code dari semua program di atas dapat diakses di sini
Comments
Post a Comment