ETS SEM 2 -- STRUKTUR DATA F

ETS STRUKTUR DATA

Anggota kelompok:

1. Sarah Alissa Putri (5025201272)

2. Nabila Zakiyah Khansa’ Machrus (5025201139)

3. Adelia Hasna Surya Putri (5025201200)


Perbedaan Struktur Data Primif dan Non Primitif

1. Struktur Data Primitif

Struktur data primitif adalah tipe data yang telah terdefinisi(supported) di suatu bahasa pemrograman.

Contohnya pada java adalah byte, short, int, long, char, float , double dan boolean.

Source

/**
 * Write a description of class PrimitifType here.
 *
 * @author Adelia Hasna Surya Putri
 * @version 05/05/2021
 */ 

public class PrimitifType {

    public static void main(String[] args) {
        double b = 123.43555;
        char c = 'e';
        boolean d = true;
        System.out.println("Double: " + b);
        System.out.println("Character: " + c);
        System.out.println("Boolean: " + d);
    }
}

Output



2. Struktur Data Non Primitif

Non primitive data type adalah tipe data yang tidak terdefinisi secara default (supported) oleh suatu bahasa pemrograman melainkan didefinisikan sendiri oleh programmer tersebut atau biasannya juga disebut sebagai references object. 

Contoh dari non primitive data type ini adalah array, string, class, dan interface.

Source

import java.util.*;
/**
 * Write a description of class NonPrimitifType here.
 *
 * @author Adelia Hasna Surya Putri
 * @version 05/05/2021
 */ 

public class NonPrimitifType {

    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        System.out.println("Enter your first name");
        String f = s1.next();
        System.out.println("Enter your last name");
        String l = s1.next();
        System.out.println("Your name is " + f + " " + l);
    }
}

Output




Implementasi Infix to Postfix dengan Menggunakan Stack

Source

import java.util.Stack;
/**
 * Write a description of class InfixStack here.
 *
 * @author Adelia Hasna Surya Putri
 * @version 05/05/2021
 */ 
public class InfixStack
{
     
    // A utility function to return
    // precedence of a given operator
    // Higher returned value means
    // higher precedence
    static int Prec(char ch)
    {
        switch (ch)
        {
        case '+':
        case '-':
            return 1;
      
        case '*':
        case '/':
            return 2;
      
        case '^':
            return 3;
        }
        return -1;
    }
      
    // The main method that converts
    // given infix expression
    // to postfix expression.
    static String infixToPostfix(String exp)
    {
        // initializing empty String for result
        String result = new String("");
         
        // initializing empty stack
        Stack<Character> stack = new Stack<>();
         
        for (int i = 0; i<exp.length(); ++i)
        {
            char c = exp.charAt(i);
             
            // If the scanned character is an
            // operand, add it to output.
            if (Character.isLetterOrDigit(c))
                result += c;
              
            // If the scanned character is an '(',
            // push it to the stack.
            else if (c == '(')
                stack.push(c);
             
            //  If the scanned character is an ')',
            // pop and output from the stack
            // until an '(' is encountered.
            else if (c == ')')
            {
                while (!stack.isEmpty() &&
                        stack.peek() != '(')
                    result += stack.pop();
                 
                    stack.pop();
            }
            else // an operator is encountered
            {
                while (!stack.isEmpty() && Prec(c)
                         <= Prec(stack.peek())){
                   
                    result += stack.pop();
             }
                stack.push(c);
            }
      
        }
      
        // pop all the operators from the stack
        while (!stack.isEmpty()){
            if(stack.peek() == '(')
                return "Invalid Expression";
            result += stack.pop();
         }
        return result;
    }
   
    // Driver method
    public static void main(String[] args)
    {
        String exp = "A+B*C^D-E/F";
        System.out.println(infixToPostfix(exp));
    }
}

Output





Implementasi Program Java Aplikasi Antrian Bank

Pada program aplikasi antrian bank ini menggunakan struktur data Queue. Queue mengimplementasikan First in First Out (FIFO) dimana input yang pertama kali dimasukkan sama dengan output yang pertama kali dikeluarkan.

Untuk menambahkan data baru, kita menggunakan method enque. Pertama-tama kita harus mengambil nilai dari method input di class Bank. Lalu cek apakah isi queue nol atau tidak. Apabila nol maka set nilai yang baru diinput sebagai head. Apabila queue berisi, set nilai yang baru diinput ke tail.

Untuk menghapus data, kita menggunakan method dequeue. Pertama cek apakah isi queue nol atau tidak. Apabila nol maka tampilkan pesan no entry. Apabila queue tidak nol, eksekusi queue mulai dari head paling awal atau yang paling pertama diinput.



Source

import java.util.Date;
import java.util.Scanner;
/**
 * Write a description of class InfixStack here.
 *
 * @author Adelia Hasna Surya Putri
 * @version 09/05/2021
 */ 
public class Bank {
    String id;
    String perlu;
    Bank next;
    static Scanner str=new Scanner(System.in);
    public void input(){
        System.out.print("Masukkan nomor antrian    : ");
        id=str.next();
        System.out.print("Masukkan keperluan        : ");
        perlu=str.next();
        next=null;
    }
    public void read(){
        System.out.println("Nomor Antrian: "+id+" || Loket: "+perlu);
    }

    public static void main(String[] args){

        int menu=0;
        Linked que=new Linked();

        que.header();

        while(menu!=4){
            System.out.println("1. To add list\n2. To show current list\n3. To show whole list\n4. Exit");
            menu=str.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("- HAVE A GOOD DAY! -");
            else System.out.println("- WRONG COMMAND -");
        }
    }
}
class Linked{
    Bank head,tail;
    public Linked(){
        head=null;
        tail=null;
    }

    public void header(){
        Date waktu = new Date();
        System.out.println(waktu.toString());
        System.out.println("Info Kurs");
        System.out.println("1 USD = Rp13.500");
        System.out.println("1 RM = Rp3.000");
        System.out.println("1 SGD = Rp10.000");
        System.out.println("1 EURO= Rp20.000");
    }

    public void enque(){
        Bank baru=new Bank();
        baru.input();
        if(head==null)head=baru;
        else tail.next=baru;
        tail=baru;
    }
    public void deque(){
        if(head==null)System.out.println("NO ENTRY");
        else{
            System.out.println("Nomor Antrian : "+head.id);
            System.out.println("Loket         : "+head.perlu);
            head=head.next;
        }
    }
    public void view(){
        if(head==null)System.out.println("NO ENTRI");
        else{
            System.out.println("Antrian            || Loket \t");
            for(Bank a=head; a!=null; a=a.next) a.read();
        }
    }
}

Output



Demo Program

Berikut adalah demo program dari beberapa source code implementasi program diatas.



Adelia Hasna Surya Putri/5025201200


Comments

Popular posts from this blog

PENUGASAN 7 -- REKURSI

PENUGASAN 8 -- BST