Anda di halaman 1dari 6

TUGAS

PEMROGRAMAM BERBASIS OBJEK

OLEH :
3120521002
DEATRI NARI RATIH
D3 TEKNIK INFROMATIKA PSDKU-LA
POLITEKNIK ELEKTRONIKA NEGERI SURABAYA
Latihan !

• Terdapat dua cara untuk menangani Exception yaitu dengan menangkap


Exception dan melempar Exception. Lakukan penanganan exception dengan
melempar Exception menggunakan throw. Berilah penjelasan (apakah program
termasuk unchecked exceptions atau checked exceptions) !
Jawab :
Input :
ReadFile2.java

package Percobaan;

import java.io.*;

public class ReadFile2 {


public static void notFound(File file) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("File tidak ditemukan");
}
}

public static void main(String args[]) {


File file = new File("Data.txt");
BufferedReader fileReader;
try {
notFound(file);
fileReader = new BufferedReader(new FileReader(file));
while (true) {
String line = fileReader.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

Output :
• Tugas !
Buatlah sebuah class Stack, FullStackException dan EmptyStackException. Class
Stack
ini menggambar Stack yang menerapkan konsep LIFO (Last In First Out).
Konsep LIFO ini, data yang terakhir masuk akan keluar pertama kali.
Class Stack mempunyai atribut:
• size : menentukan besar array untuk menyimpan data. Array berdimensi satu
dengan tipe Object.
• top : merupakan tanda indeks yang paling atas, yang belum terisi. Sehingga data
yang akan masuk akan dimasukkan pada indeks tersebut.
• Object[] elemen : untuk menyimpan data.

Class Stack mempunyai operasi:


• public Stack() : jika kita membuat object Stack dengan konstruktor Stack() maka
tentukan size = 5.
• public Stack(int s) : jika kita membuat object Stack dengan konstruktor Stack(int
s) maka tentukan size berdasarkan parameter s.
• public int getSize() : untuk mendapatkan besar array dari Stack.
• public int getTop() : untuk mendapatkan top dari Stack
• public void push(Object o) : untuk memasukkan data ke array pada Stack.
• public Object pop() : untuk mengambil data dari array.
Jawab :
Input :
Stack.java

package Percobaan;

public class Stack {


public int size;
public int top = 0;
public Object[] elemen;

public Stack() {
this.size = 5;
elemen = new Object[5];
}

public Stack(int s) {
this.size = s;
elemen = new Object[s];
}

public int getSize() {


return this.size;
}

public int getTop() {


return this.top;
}

public void push(Object o) {


if (this.elemen[this.top] == null) {
this.elemen[this.top] = o;
} else if (this.size - 1 == this.top) {
throw new FullStackException("Stack sudah penuh");
} else {
if (!(this.size - 1 == this.top)) {
this.top++;
}
this.elemen[this.top] = o;
}
}

public Object pop() throws EmptyStackException {


if (this.elemen[this.top] == null) {
throw new EmptyStackException("Stack Kosong");
} else {
Object hasil = this.elemen[this.top];
this.elemen[this.top] = null;
if (this.top > 0) {
this.top--;
}
return hasil;
}
}

public static void main(String[] args) {


Stack sta = new Stack();
sta.push("satu");
sta.push("dua");
sta.push("tiga");
sta.push("empat");
sta.push("lima");
System.out.println(sta.pop());
System.out.println(sta.pop());
System.out.println(sta.pop());
System.out.println(sta.pop());
System.out.println(sta.pop());
}
}

FullStackException.java

package Percobaan;

public class FullStackException extends RuntimeException {


public FullStackException() {
}

public FullStackException(String pesan) {


super(pesan);
}
}
EmptyStackException.java

package Percobaan;

public class EmptyStackException extends RuntimeException {


public EmptyStackException() {
}

public EmptyStackException(String message) {


super(message);
}
}

Output :

Anda mungkin juga menyukai