Konsep exception digunakan untuk menghindari terjadinya program mati/ berhenti mendadak saat sedang dijalankan. Hal ini biasanya disebabkan isian dari user yang tidak sesuai, atau ada proses di dalam program yang menyebabkan error di tengah jalan. Struktur coding-nya adalah berupa blok try {} catch(){} finally{}. Berikut ini adalah contoh sederhananya.
import java.io.*;
class ExceptionGenerator {
public void divideByZero () {
// Generate an ArithmeticException - runtime
int y = 6, x = 0;
int z = y / x;
System.out.println ("z = " + z);
}
public void arrayOutOfBounds () {
// Generate an ArrayIndexOutOfBoundsException - runtime
int [] data = new int [10];
data[10] = 16;
System.out.println ("data[10] = " + data[10]);
}
Contoh Program Queue Memakai Array/ Larik dalam Bahasa Java
class queue{
int antrian[];
int depan, belakang;
int jumlah, ukuran;
queue(int n){
antrian = new int[n]; ukuran=n;
jumlah=depan=belakang=0;
}
void insert(int d){
if(jumlah<ukuran){
antrian[belakang]=d;
jumlah++; belakang++;
}
}
void remove(){
for(int i=0; i<jumlah-1 ;i++){
antrian[i]=antrian[i+1];
}
jumlah--;
belakang--;
}
void display(){
System.out.print("\nIsi Antrian : ");
for(int i=0; i<jumlah; i++){
System.out.print(antrian[i]);
}
}
}
public class queuearray {
public static void main(String s[]){
queue q = new queue(5);
q.insert(9); q.display(); q.insert(5); q.display();
q.insert(7); q.display(); q.insert(3); q.display();
q.insert(8); q.display();
q.remove(); q.display();
q.remove(); q.display();
q.insert(6); q.display();
}
}
int antrian[];
int depan, belakang;
int jumlah, ukuran;
queue(int n){
antrian = new int[n]; ukuran=n;
jumlah=depan=belakang=0;
}
void insert(int d){
if(jumlah<ukuran){
antrian[belakang]=d;
jumlah++; belakang++;
}
}
void remove(){
for(int i=0; i<jumlah-1 ;i++){
antrian[i]=antrian[i+1];
}
jumlah--;
belakang--;
}
void display(){
System.out.print("\nIsi Antrian : ");
for(int i=0; i<jumlah; i++){
System.out.print(antrian[i]);
}
}
}
public class queuearray {
public static void main(String s[]){
queue q = new queue(5);
q.insert(9); q.display(); q.insert(5); q.display();
q.insert(7); q.display(); q.insert(3); q.display();
q.insert(8); q.display();
q.remove(); q.display();
q.remove(); q.display();
q.insert(6); q.display();
}
}
Contoh Program Queue Memakai Linked List dalam Bahasa Java
class simpulq{
int data;
simpulq prev, next;
simpulq(int d){
data=d;
}
int getdata(){ return data; }
}
class linkedlistq{
simpulq head, tail;
linkedlistq(){
head=tail=null;
}
void insert(int d){
simpulq baru = new simpulq(d);
if (head==null){ head=tail=baru;
}
else{
tail.next = baru;
baru.prev = tail;
tail = baru;
}
}
void remove(){
if (head.next==null){ head=null; tail=null;
}
else{
head=head.next;
head.prev=null;
}
}
void display(){
System.out.print("\nIsi Antrian: ");
for(simpulq t=head; t!=null; t=t.next){
System.out.print("["+t.getdata()+"]");
}
}
}
public class MyQueue {
public static void main(String j[]){
linkedlistq queue = new linkedlistq();
queue.insert(8); queue.display();
queue.insert(5); queue.display();
queue.insert(4); queue.display();
queue.insert(6); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
}
}
int data;
simpulq prev, next;
simpulq(int d){
data=d;
}
int getdata(){ return data; }
}
class linkedlistq{
simpulq head, tail;
linkedlistq(){
head=tail=null;
}
void insert(int d){
simpulq baru = new simpulq(d);
if (head==null){ head=tail=baru;
}
else{
tail.next = baru;
baru.prev = tail;
tail = baru;
}
}
void remove(){
if (head.next==null){ head=null; tail=null;
}
else{
head=head.next;
head.prev=null;
}
}
void display(){
System.out.print("\nIsi Antrian: ");
for(simpulq t=head; t!=null; t=t.next){
System.out.print("["+t.getdata()+"]");
}
}
}
public class MyQueue {
public static void main(String j[]){
linkedlistq queue = new linkedlistq();
queue.insert(8); queue.display();
queue.insert(5); queue.display();
queue.insert(4); queue.display();
queue.insert(6); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
queue.remove(); queue.display();
}
}
Subscribe to:
Posts (Atom)