วันจันทร์ที่ 26 กรกฎาคม พ.ศ. 2553

Style การเขียนโปรแกรมเมื่อใช้ &

โปรแกรมเมอร์ C++ บางคนชอบเขียนเครื่องหมาย & ต่อท้ายชนิดข้อมูลมากกว่าที่จะเขียนไว้หน้าตัวแปร เช่น

void swap(int& a,int& b);

และนอกจากนี้บางคนยังนิยมเขียนวิธีนี้กับ Pointer operator ด้วยเช่น

float* p;

จุดประสงค์เพื่อจะให้มีความแตกต่างว่าเป็นข้อมูลชนิดpointer แต่วิธีการนี้อาจก่อให้เกิดความสับสนกับการประกาศเพราะ & และ * ใช้กับลิสต์ของตัวแปรไม่ได้เช่น ถ้าต้องการประกาศตัวแปร Integer 2ตัว เป็นpointer

int* a,b;

จะทำไม่ได้เพราะ b จะเป็นตัวแปรแบบ Integer ธรรมดาไม่ใช้ตัวแปรแบบ Pointer เพราะ & และ * จะใช้ได้กับตัวแปรทีละตัวเท่านั้น

สำหรับการเขียน &,* นั้นจะเขียนแบบไหนก็ได้เพราะ compiler จะตีความหมายเหมือนกัน

วันอาทิตย์ที่ 25 กรกฎาคม พ.ศ. 2553

การคืนค่า Function แบบ References

     นอกจากจะใช้พารามิเตอร์ Reference ได้แล้ว ยังสามารถใช้วิธีการนี้กับการคืนค่าของfunctionได้ เราสามารถทำได้โดยใช้เครื่องหมาย & นำหน้าชื่อ function เช่น prototype ของ f() ที่คืนค่า Integer แบบ Refernce

int &f();

     ในfunctionที่คืนค่าแบบ Reference นั้น compiler จะรู้เองว่าต้องคืนค่าที่เป็น address ของ object แทนที่จะเป็นค่าของมัน เช่น

int &f(){

  int i;

  cin >> i;     // get a value for i

  return i;     // automaticity returns reference to i

วันจันทร์ที่ 19 กรกฎาคม พ.ศ. 2553

จัดไป1เพลง กับวันเหง๊าาาา….เหงา

 

 

 

 

 

 

 

 

 

 

 

ในยามที่เราได้พบกัน ฉันจะเปรียบเธอเป็นเช่นดอกไม้
ในยามที่เราได้รักกัน ฉันจะปลูกต้นรักไปทั้งใจ
เวลาที่มีหยาดฝนโปรยปราย ฉันจะปรากฏกายข้างเธอไม่ห่างไปไหน
เวลาที่มีเรื่องร้อนใจ ฉันจะปัดและเป่าให้เธอจนชื่นใจ

ถึงแม้วันใดไม่มีฉันอยู่ ให้เธอรู้ความรักคงอยู่ไม่ห่างไปไหน
ต้นรักที่ฉันปลูกไว้ข้างใน หากเธอคงรักนั้นไว้ คอยดูแลรักฉันไว้... โว๊ะ โอ โอะ..

ความรักที่มีให้กัน ถ้าเธอคอยรักและอยู่เคียงข้างกัน
แค่ให้ความอบอุ่น และรักที่มีนั้นช่วยพาเราไป โว๊ะ โอ โอะ..
ความรักส่งไปให้เธอ แด่เธอที่รัก มั่นคงไว้เสมอ
ด้วยความรักที่มีแค่เรา ข้างกันและกัน

แค่เพียงจับมือสบสายตา รักจะพาให้เราล่องลอยสู่ความฝัน
ไม่มีสิ่งใดที่ล่ำค่า มากไปกว่าต้นรักที่เรามีให้กัน

วันเสาร์ที่ 17 กรกฎาคม พ.ศ. 2553

Newton – Raphson Method

การแก้ปัญหาทางคณิตศาสตร์ วิทยาศาสตร์ บางครั้งจะต้องแก้หาค่า x จากสมการ f(x)=0 ถ้าหากเป็นfunction กำลังสองทั่วไปอาจใช้ สูตรมาแก้สมการได้ แต่ถ้าเป็นfunctionที่มีกำลังสูงขึ้นจะไม่มีสูตรที่แน่นอน จึงมีวิธีการทางการวิเคราะห์เชิงตัวเลข(Numerical Analysis) มาแก้ปัญหาสำหรับเทคนิคที่นิยมใช้กันจะเป็นวิธีของนิวตัน หรือ Newton – Raphson Method ซึ่งจะใช้เส้นสัมผัสเส้นโค้ง y=f(x) ที่จุด f เข้าใกล้ศูนย์มาช่วยประมาณค่า

สมการของเส้นสัมผัสของกราฟ y=f(x) ที่จุด x=a หรือเส้นสัมผัสที่ผ่านจุด(a,f(a)) จะมีสมการเป็น
                        L(x) = f(a) – f‘(a) (x-a)

image

 

 

จากภาพ เป็นfunction y = x^2
และเส้นสัมผัสของ f(x) ที่จุด x=5

 

 


 

 

ขั้นตอนของ Newton – Raphson

การประมาณค่ารากของ function จากสมการ f(x) = 0 จะทำโดยอาศัยเส้นสัมผัส เส้นโค้ง y = f(x) ถ้าหากเริ่มต้นประมาณค่า x ด้วย x0 ให้ลากเส้นสัมผัสที่จุด (x0,f(x0)) เส้นสัมผัสจะตัดแกน x1 ซึ่ง x1 นี้จะมีค่าใกล้เคียงกับรากของสมการด้วย เรามาดูตัวอย่างในรูปกันคับ

image
















จากนั้นให้ใช้ x1 ประมาณค่าต่อไปโดยลากเส้นสัมผัสที่จุด (x1,f(x1)) ทำให้เส้นสัมผัสมาตัดแกน x ที่จุด x2 และใช้ x2 นี้ประมาณค่าต่อไปอีก ซึ่งจะทำให้ค่า x ที่ได้ใกล้เคียงคำตอบมากขึ้นเรื่อยๆ ดังนั้นถ้าทำขั้นตอนเหล่านี้ n+1 ครั้ง จะได้ค่าประมาณเป็น x ที่ n+1 ซึ่งเกิดจากการประมาณค่าจากจุด x ที่ n โดยสมการเส้นสัมผัสที่จุด (xn,f(xn)) คือ

                              y – f(xn) = f’(xn) (x-xn)

เนื่องจากเส้นสัมผัสที่ตัดแกน x นั้นค่า y จะเท่ากับศูนย์อย่างในรูป ดังนั้น
                             0 – f(xn) = f’(xn) (x-xn)
                                - f(xn) = f‘(xn) (x-xn)
                                - f(xn) = f’(xn) * x – f’(xn) * xn
                             f(xn) * x = f’(xn) * xn – f(xn)
                                       x = xn – f(xn) / f’(xn)

Overriding Method

Methods defined in a superclass can be overridden by methods defined in a subclass. Here is the definition:

Definition Overiding: Overriding refers to the introduction of an instance method in a subclass that has the same name,signature,and return type of a method in the superclass. Implementation of the method in the subclass replaces the implementation of the method in the superclass.

Overriding is different from overloading.Overriding is concerned with methods of different classes that have an inheritance relationship.In overriding,methods share the same name,signature,and return type.In contrast,overloaded methods are part of the same class,but have different signatures.

We can illustrate the distinction between overloading and overriding in the following manner. In

class A{
        public void m1(){
            //statement
        }
        public void m1(int i){
            //statement
        }
    }

class A contain two overloaded methods.An instance of A can access both methods,depending on the arguments of the method invocation,so

A a = new A();
a.m1();            //invoke m1()
a.m1(1);          //invoke m1(int)

Now,let us assume that we have the two classes

class B{
    public void m2(){
        //statement
    }
}
class C{
    public void m2(){
        //statement
    }
}

Implementation of method m2() in class B is overridden by another implementation of method m2() in class C. For a given object,one but not both of the implementations of method m2() is available, depending on the class of the object.

B b = new B();
C c = new C();
b.m2(); //invoke the m2() in class B
c.m2(); //invoke the m2() in class C

Overriding a method with another method of different signature or return type is not allowed.For example,the following code segment will cause a compilation error:

class B{
    public void m3(int i){
    }
}
class C{
    public void m3(char c){
    }
}

วันศุกร์ที่ 16 กรกฎาคม พ.ศ. 2553

Overloading Method

Definition Overloading: Overloading refers to the ability to allow different methods of constructors of a class to share the same name. The name is said to be overloaded with multiple implementations.

The legality of overloading depends on the signatures of the methods or constructors being overloaded. The signature of a methods or constructor consists of the name of  the method and a list of the types of its parameters.Note that the return type,parameter names and final designations of parameters are not part of the signature. Parameter order,however,is significant. The following are some examples of methods and their signatures.

Method Signature
String toString() toString()
void move(int dx,int dy) move(int,int)
void move(final int dx,final int dy) move(int,int)
void paint(Graphics g) paint(Graphics)

The Rule of Overloading: Two methods of constructors in the same class can be overloaded,i.e.,sharing the same name,if they have either different numbers of parameters or the same number of parameters but of different types.In other words,no two methods or constructors in the same class may have identical signatures.

The following class declaration illustrates overloaded constructors and overloaded methods:

public class Point {
    protected double x,y;
    public Point(){
        x = 0.0;y = 0.0;
    }
    public Point(double x,double y){
        this.x = x;this.y = y;
    }
    /** calculate the distance between this point and the other point */
    public double distance(Point other){
        double dx = this.x - other.x;
        double dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy *dy);
    }
    /** calculate the distance between this point and(x,y) */
    public double distance(double x,double y){
        double dx = this.x - x;
        double dy = this.y - y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    /** calculate the distance between this point and(x,y) */
    public double distance(int x,int y){
        double dx = this.x - (double) x;
        double dy = this.y - (double) y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    /** calculate the distance between this point and the origin */
    public double distance() {
        return Math.sqrt(x * x + y * y);
    }
    // other method
}

When an overloaded method is called, the number and the types of the arguments are used to determine the signature of the method that will be invoked.Overloading is resolved at compile time,as in the following code segment:

Point p1 = new Point();                               //invoke Point()
Point p2 = new Point(20.0,30.0);                 //invoke Point(double,double)
p2.distance(p1);                                         //invoke distance(Point)
p2.distance(50.0,60.0);                               //invoke distance(double,double)
p2.distance(50,60);                                    //invoke distance(int,int)
p2.distance();                                            //invoke distance()

วันอาทิตย์ที่ 11 กรกฎาคม พ.ศ. 2553

การสร้าง Package ใน Java

1.ตั้งชื่อ package
สมมุติให้ชื่อว่า mypackage.homework
2. package ที่เราสร้างต้องอยู่ในโฟลเดอร์หนึ่งๆ
ในที่นี้ให้อนู่ใน E:\user
3.เราต้องให้ java รู้ว่านอกจาก stardard java class แล้วยังมี package อื่นอีก
เราต้องไปกำหนดค่า base ใน classpath
วิธีการกำหนดค่าใน classpath
Start --> Settings --> Control Panel --> Systems --> Advance
--> Enviroment Variable --> หาตัวที่เราเก็บ java ไว้โดยมากจะชื่อว่า
path หรือไม่ ก็ class path เลือกแล้วกด Edit --> แล้วใส่ที่อยู่ของ package
ในที่นี้ คือ E:\user แล้วใน semi colon ต่อท้าย --> Ok
4.เราจะไปสร้างโฟลเดอร์ต่อจากที่เราเก็บที่อยู่ คือไปสร้างต่อที่ E:\user
ในที่นี้ให้ชื่อว่า mypackage
5.แล้วเราไปสร้างโฟลเดอร์ต่อจาก mypackage ให้ชื่อว่า homework
6.เสร็จแล้วเราของเอา class ที่เราเขียนมาใส่ในโปลเดอร์ homework
7.วิธีการสร้าง package เราจะใช้ keyword คำว่า package ในการขึ้นต้น
เพื่อทำให้ java ทราบว่าเรากำลังจะสร้าง package ใหม่
package mypackage.homework; //ชื่อ package กับ keyword ว่า package
public class BankAccount { //ชื่อ class ที่จะสร้างไว้เป็น package
//instance variable
//constructor
//method
}
แล้ว compile ให้ได้ไฟล์ .class เอาไว้ที่ E:\user\mypackage\homework\ ด้วยนะครับ
8.วิธีการใช้เราต้อง import เข้ามา
import mypackage.homework.BankAccount ;
public class BankAccountTest {
BankAccount A_NagativesAcc = new BankAccount(500.00);
}
credit : citec.us

วันอังคารที่ 6 กรกฎาคม พ.ศ. 2553

BiSection Method By MathLAB

อันนี้เป็นการหาrootของfunction โดยใช้วิธี BiSection Methodในการหาroot และทำการตรวจสอบfunctionที่ไม่เป็น continuous function ด้วย ซึ่งnot continuous function ก็จะมีลักษณะอย่างในภาพตัวอย่าง ซึ่งจะทำให้เราหาrootของfunctionไม่ได้นะคับ

image1 image2

ลักษณะของ function ที่เราสามารถหาrootได้จะต้องเป็น continuous function เท่านั้น โดยเราจะต้องกำหนด boundary เอาไว้ด้วยตัวอย่างเช่น

image3

เราสามารถหา root ของfunction ได้ โดยทำการกำหนด boundary เอาไว้ด้วย มาดูโปรแกรมตรวจสอบกันคับ (อันนี้ผมเขียนกับ MathLAB)

clear all;
close all;
clc;
f = inline('x^3+x^2+x-1');
a = -0.5;
b = 1;
tol1 = 0.0001;
tol2 = 0.0001;
flags = false;
max1=1+round(log(a-b)-log(tol1)/log(2));
fm = zeros(1,abs(max1));
if (f(a)*f(b) == 0)
    if (f(a) == 0)
        root = a;
    else
        root = b;
    end
else
    lamda = abs((a+b)/20);
    if f(a)<f(b)
       if f(a)<f(a+lamda)
          disp ('Continuous function')
          flags = true;
       else
          disp ('Not continuous function')
       end
    elseif f(b)<f(a)
       if f(b)<f(b-lamda)
          disp ('Continuous function')
          flags = true;
       else
           disp ('Not continuous function')
       end
    end
    count = 0;
end
if(flags)
   for j = 1:max1
      m = (a+b)/2;
      fm(j) = f(m);
      [j a b m f(m)]
      if(f(a)*f(m) > 0)
          a = m;
      else
          b = m;
      end
      if (b-a)/2<tol1
          flags = false;
          root = m;
      end
   end
   fm_greater_than_zero = [fm(find(fm>0))]
   fm_less_than_zero = fm(find(fm<0))
end