Praktikum Ke Lima PSBO

Pada praktikum kelima ini dijelaskan tentang “Strategy Pattern” dan materi ini , yaitu dijelaskan sedikit tentang design pattern. Pada pertemuan ini dibahas kembali tentang materi pertemuan sebelumnya, kemudian ditambah tentang penggunaan Exception Handling dan Trows.
Berikut adalah kode program Strategy Pattern :

Pertama kita buat dulu GoAlgorithm.java sebagai interface-nya dengan method go(), yang belum di implementasikan.

package StrategyPattern;

public interface GoAlgorithm {

    public void go();
}

Setelah itu kita buat class-class implemennya

package StrategyPattern;

public class GoByDrivingAlgorithm implements GoAlgorithm {

    public void go() {
        System.out.println("Now i'm Driving");
    }
}
package StrategyPattern;

public class GoByFlyingAlgorithm implements GoAlgorithm {

    public void go() {
        System.out.println("Now i'm Flying");

    }
}
package StrategyPattern;

public class GoByFlyingFastAlgorithm implements GoAlgorithm {

    public void go() {
        System.out.println("Now i'm Flying Fast");
    }
}
Setelah itu kita buat class-class turunannya, sbb:
package StrategyPattern;

public class FormulaOne extends Vehicle {

    public FormulaOne() {
        setGoAlgorithm(new GoByDrivingAlgorithm());
    }
}
package StrategyPattern;

public class Helicopter extends Vehicle {

    public Helicopter() {
        setGoAlgorithm(new GoByFlyingAlgorithm());
    }
}
package StrategyPattern;

public class Jet extends Vehicle {

    public Jet() {
        setGoAlgorithm(new GoByFlyingFastAlgorithm());
    }
}
package StrategyPattern;

public class StreetRacer extends Vehicle {

    public StreetRacer() {
        setGoAlgorithm(new GoByDrivingAlgorithm());
    }
}
package StrategyPattern;

public class Vehicle {

    private GoAlgorithm goAlgorithm;

    public Vehicle() {
    }

    public void setGoAlgorithm(GoAlgorithm algorithm) {
        this.goAlgorithm = algorithm;
    }

    public void go() {
        goAlgorithm.go();
    }
}

Lalu terakhir, fungsi main-nya ini :

package StrategyPattern;

import javax.swing.JOptionPane;

public class StartTheRacer {

    public static void main(String[] args) {

        StreetRacer streetRacer = new StreetRacer();
        FormulaOne formulaOne = new FormulaOne();
        Helicopter helicopter = new Helicopter();
        Jet jet = new Jet();
        
        streetRacer.go();
        formulaOne.go();
        helicopter.go();
        jet.go();
      
    }
}

Kemudian dijelaskan tentang Exception Handling, berikut kode programnya :

package ExceptionHandling;

public class DivByZero {
    public static void main(String[] args) {
        System.out.println(3 / 0);
        System.out.println("Cetak");
    }
}
package ExceptionHandling;

import javax.swing.JOptionPane;

public class DivByZeroWithTry {

    public static void main(String[] args) {
        try {
            System.out.println(3 / 0);
            System.out.println("Cetak");
        } catch (ArithmeticException e) {
            JOptionPane.showMessageDialog(null, "Error : Devided by Zero!",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
        } catch (ArrayIndexOutOfBoundsException a) {
            JOptionPane.showMessageDialog(null, "Missing Argument.",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
        }
        System.out.println("Setelah Exception.");
        System.exit(0);
    }
}

package ExceptionHandling;

public class FinallyDemo {

public static void main(String[] args) {
int i = 0;
String[] greeting = {
“hallo pagi”, “cagur”, “test lagi”
};
while (i < 4) { try { System.out.println(greeting[i]); i++; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("index value"); i++; //i=0; } finally { System.out.println("FINAL"); } } } } [/sourcecode] Kemudian terakhir adalah tentang penggunaan Throw : [sourcecode language='java'] package Trows; public class Throw { static void MyTrow() throws ClassNotFoundException { throw new ClassNotFoundException("just a demo"); } } [/sourcecode] [sourcecode language='java'] package Trows; public class ThrowDemo { public static void main(String[] args) { try { Throw.MyTrow(); } catch (ClassNotFoundException e) { System.out.println(e); } } } [/sourcecode]

About

Seseorang yang terus berusaha menjadi seorang programmer dan blogger sejati di jagad Raya ini.

Tagged with: , ,
Posted in Java Programming, Praktikum PSBO
One comment on “Praktikum Ke Lima PSBO
  1. ravis says:

    kalo bisa dikasih screenshot donk mas…😉 mksh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Community

INDC

MUGI

WSS-ID

SQLServer-Indo

Indonesia Student Portal

Pengunjung
  • 120,657 orang
Yg lg OL di sini
site statistics
%d bloggers like this: