Praktikum Terakhir PSBO

6 06 2010

Pada praktikum terakhir ini dibahas beberapa materi yang kurang atau belum tersampaikan di praktikum seperti Visitor Pattern dan Template Method yang bisa dilihat di postingan sebelumnya.

Sumber lain mengenai Design Pattern bisa dipelajari di http://sourcemaking.com.

Kemudian sedikit dikenalkan juga pembuatan project sederhana JSP (Java Server Page) menggunakan Netbeans 6.8.
Sourcecode bisa didownload di sini.





Praktikum ke Dua Belas: Strategy Pattern dan Template Method Pattern

6 06 2010

Contoh kode program untuk Strategy Pattern adalah sebagai berikut:

//StrategyExample.java
// The classes that implement a concrete strategy should implement this
 
// The context class uses this to call the concrete strategy
interface Strategy {
    int execute(int a, int b);
}
 
// Implements the algorithm using the strategy interface
class ConcreteStrategyAdd implements Strategy {
    public int execute(int a, int b) {
        System.out.println("Called ConcreteStrategyA's execute()");
        return a + b;  // Do an addition with a and b
    }
}
 
class ConcreteStrategySubtract implements Strategy {
    public int execute(int a, int b) {
        System.out.println("Called ConcreteStrategyB's execute()");
        return a - b;  // Do a subtraction with a and b
    }
}
 
class ConcreteStrategyMultiply implements Strategy {
    public int execute(int a, int b) {
        System.out.println("Called ConcreteStrategyC's execute()");
        return a  * b;   // Do a multiplication with a and b
    }
}

class ConcreteStrategyModulo implements Strategy {
    public int execute(int a, int b) {
        System.out.println("Called ConcreteStrategyD's execute()");
        return a  % b;   // Do a Modulo with a and b
    }
}
 
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
class Context {
    private Strategy strategy;
    // Constructor
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }
    public int executeStrategy(int a, int b) {
        return strategy.execute(a, b);
    }
}
class StrategyExample {
    public static void main(String[] args) {
        Context context;
        // Three contexts following different strategies
        context = new Context(new ConcreteStrategyAdd());
        int resultA = context.executeStrategy(3, 4);
 
        context = new Context(new ConcreteStrategySubtract());
        int resultB = context.executeStrategy(3, 4);
 
        context = new Context(new ConcreteStrategyMultiply());
        int resultC = context.executeStrategy(3, 4);
		
        context = new Context(new ConcreteStrategyModulo());
        int resultD = context.executeStrategy(10, 4);
		
		System.out.println("REsult A " + resultA);
		System.out.println("REsult B " + resultB);
		System.out.println("REsult C " + resultC);
		}
}

Read the rest of this entry »





Praktikum ke Sebelas: Builder Pattern dan Visitor Pattern

6 06 2010

Untuk contoh program dengan Builder Pattern dapat dilihat pada kode program berikut:

//BuilderExample.java
abstract class Composition
{
	private String type = "";
	Composition(String t)
	{
		this.type = t;
	}

	public String getType()
	{
		return type;
	}
}

class Dough extends Composition
{
	Dough(String t) 
	{
		super(t);
	}
}

class Sauce extends Composition
{
	Sauce(String t) {super(t);}
}

class Topping extends Composition
{
	Topping(String t) {super(t);}
}

class Pizza {
	private Dough dough;
	private Sauce sauce;
	private Topping topping;
 
	private boolean is_roasted = false;
	
	Pizza()
	{}
	public void setDough(Dough dough) {
		this.dough = dough;
	}
 
	public void setSauce(Sauce sauce) {
		this.sauce = sauce;
	}
 
	public void setTopping(Topping topping) {
		this.topping = topping;
	}
}
 
/** "Abstract Builder" */
abstract class PizzaBuilder {
	protected Pizza pizza;
 
	public Pizza getPizza() {
		return pizza;
	}
 
	public void createNewPizzaProduct() {
		pizza = new Pizza();
	}
	public abstract void buildDough();
	public abstract void buildSauce();
	public abstract void buildTopping();
	public abstract void extraTreatment();
}

/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder {
	private boolean is_roasted = false;
	
	public void buildDough() {
		pizza.setDough(new Dough("cross"));
	}
	public void buildSauce() {
		pizza.setSauce(new Sauce("mild"));
	}
	public void buildTopping() {
		pizza.setTopping(new Topping("ham+pineapple"));
	}
	public void roast()
	{
		is_roasted = true;
	}
	public void extraTreatment()
	{
		this.roast();
	}
}
 
/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder {
	public void buildDough() {
		pizza.setDough(new Dough("pan baked"));
	}
 
	public void buildSauce() {
		pizza.setSauce(new Sauce("hot"));
	}
 
	public void buildTopping() {
		pizza.setTopping(new Topping("pepperoni+salami"));
	}

	public void extraTreatment()
	{}
}

/** "Director" */
class Cook {
	private PizzaBuilder pizzaBuilder;

	public void setPizzaBuilder(PizzaBuilder pb) {
		pizzaBuilder = pb;
	}
	public Pizza getPizza() {
		return pizzaBuilder.getPizza();
	}
	public void constructPizza() {
		pizzaBuilder.createNewPizzaProduct();
		pizzaBuilder.buildDough();
		pizzaBuilder.buildSauce();
		pizzaBuilder.buildTopping();
		pizzaBuilder.extraTreatment();
	}
}
 
/** A given type of pizza being constructed. */
public class BuilderExample {
	public static void main(String[] args) {
		Cook cook = new Cook();
		PizzaBuilder hawaiianPizzaBuilder = new 			     HawaiianPizzaBuilder();
		PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
 
		cook.setPizzaBuilder(hawaiianPizzaBuilder);
		cook.constructPizza();
		Pizza pizza = cook.getPizza(); // hawaiian

		cook.setPizzaBuilder(spicyPizzaBuilder);
		cook.constructPizza();

		pizza = cook.getPizza(); // spicy

		Pizza[] menu = new Pizza[150];
		
		cook.setPizzaBuilder(hawaiianPizzaBuilder);
		int i;
		for(i=0; i<100; i++)
		{
			cook.constructPizza();
			menu[i] = cook.getPizza();
		}
		cook.setPizzaBuilder(spicyPizzaBuilder);
		for(i=100;i<150;i++)
		{
			cook.constructPizza();
			menu[i] = cook.getPizza();
		}
	}
}

Read the rest of this entry »





Praktikum ke Sepuluh: Java Swing & JDBC MySQL

6 06 2010

Bahan praktikum ini bisa di baca di HOL 10 Praktikum PSBO : Java Swing and JDBC Mysql.

Dan sourcecode lengkapnya bisa di download di sini.





Praktikum ke Sembilan: Java Library & Java Swing

6 06 2010

Membuat library sederhana dengan Java

  • Gunakan NetBeans untuk membuat project baru
  • Pilih kategori: Java>Java Class Library
  • Isi nama project: TestLib
  • Pada panel Projects, pilih TestLib, kemudian pilih source package
  • Ganti nama package default menjadi psbo
  • Klik kanan pada package psbo, klik New Class
  • Beri nama class yang baru: Parent
  • Isi Parent.java dengan isi berikut:
package psbo;


class Parent {
    protected int data;
    Parent(int data)
    {
        this.data = data;
    }
    protected void process()
    {
    }
    public void show()
    {
        process();
        System.out.println("Data = "+data);
    }
}

Read the rest of this entry »





Bahan Praktikum ke Delapan : Standard Template Library (STL) in C++

6 06 2010

Target dari praktikum ini adalah praktikan mempelajari STL Compenent dan STL Iterator.
Bahan praktikum ini bisa di download di sini.





Praktikum ke Tujuh: Template Programming in C++

6 06 2010
#include <iostream>
#include <string>
using namespace std;

// Swap yang di-template-kan
// dapat dipanggil dengan argumen bermacam-macam tipe
// int a,b, double x,y;
// e.g. swap<int>(a, b) 
// atau swap<double>(x,y)

template <class T>
void swap(T& a, T& b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}



// Stack yang ditemplate-kan..
// Stack<int> StackInt;
// StackInt.push(3); StackInt.push(2);
// Stack<char> StackChar;
// StackChar.push('a'); 

const int MAX = 100;

template <class T>
class Stack
{
    protected:
        int top;
        T arr[MAX];
    
    public:
        Stack()
        :top(0)
        {}
        
        void push(T a)
        {
            arr[top++] = a;
        }
        T pop()
        {
            T a = arr[--top];
            return a;
        }
};

// Ini sebagai class untuk percobaan template
// e.g. swap<Pecahan>(x,y);
// e.g. Stack<Pecahan> StackPecahan;
// dilengkapi dengan operator overloading = dan <
// agar dapat dilakukan sorting 


class Pecahan
{
    protected:
        int a;
        int b;
    public:
        Pecahan(int a_p, int b_p)
        :a(a_p), b(b_p)
        {
                //cout<<"konstruktor 2 arg"<<endl;
        }
        
        Pecahan()
        :a(0), b(1)
        {
                //cout<<"konstruktor tanpa arg"<<endl;
        }
        
        Pecahan(int n)
        :a(n), b(1)
        {
                //cout<<"konstruktor 1 arg"<<endl;
        }
        
        
        void tampil()
        {
                cout<<a<<"/"<<b;
        }
        
        void set_a(int a)
        {
                this->a = a;
        }
        void set_b(int b)
        {
                this->b = b;
        }
        
        int get_a() const
        {
                return a;
        }
        int get_b() const
        {
                return b;
        }
             
       Pecahan operator=(const Pecahan& y)
       {
              this->a=y.a;
              this->b=y.b;
              return (*this);
       }
       
       
       //
       Pecahan operator++()
       {
              this->a = this->a + this->b;
              return (*this);
       }
       
       bool operator<(const Pecahan& p) const
       {
              return (this->a*p.b < this->b*p.a);
       }
       
};


// Algoritma sorting (selection sort) yang tertemplate-kan
// sel_sort<TIPE>(array, number);


template <class T>
void sel_sort(T* a, unsigned n) // Selection Sort
{
    int i,j, min;
    for(i=0;i<n;i++)
    {
        min = i;
        for(j=i+1; j<n; j++)
        {
                if(a[j]<a[min])
                  min = j;
        }
        swap(a[min], a[i]);
    }
}


// Contoh main
int main()
{
    int a = 3, b = 2;
    swap<int>(a,b);
   
    cout<<"a = "<<a<<" b = "<<b<<endl;
   
    double x=2.1, y = 3.4;
    swap<double>(x,y);
    
    cout<<"x = "<<x<<" y = "<<y<<endl;
    
    Stack<int> S;
    S.push(3);
    S.push(4);
    a = S.pop();
    cout<<"a = "<<a<<endl;

    int data[3] = {2,1,5};
    sel_sort(data,3);
    int i;
    for(i=0; i<3; i++)
        cout<<data[i]<<endl;

    Pecahan pec[3];
    pec[0] = Pecahan(1,2);
    pec[1] = Pecahan(2,3);
    pec[2] = Pecahan(1,1);

    mysort<Pecahan>(pec,3);
    for(i=0; i<3; i++)
    {
        pec[i].tampil();
        cout<<endl;
    }
    
    return 0;
}

Read the rest of this entry »








Follow

Get every new post delivered to your Inbox.