Click here to Skip to main content
15,890,506 members
Home / Discussions / Java
   

Java

 
QuestionContent Based Image Retrieval with java Pin
hendrik.sps22-May-11 0:18
hendrik.sps22-May-11 0:18 
AnswerRe: Content Based Image Retrieval with java Pin
Richard MacCutchan22-May-11 1:41
mveRichard MacCutchan22-May-11 1:41 
AnswerRe: Content Based Image Retrieval with java Pin
TorstenH.23-May-11 5:00
TorstenH.23-May-11 5:00 
Questionrecord audio and video by java Pin
lolostar21-May-11 15:51
lolostar21-May-11 15:51 
AnswerRe: record audio and video by java Pin
Richard MacCutchan21-May-11 21:24
mveRichard MacCutchan21-May-11 21:24 
AnswerRe: record audio and video by java Pin
twseitex22-May-11 10:23
twseitex22-May-11 10:23 
AnswerRe: record audio and video by java Pin
san-shiro{701}4-Jun-11 3:43
san-shiro{701}4-Jun-11 3:43 
Questionjava source to c++ (help me) Pin
DominoBoy21-May-11 11:12
DominoBoy21-May-11 11:12 
hi i coded a java class, now i need to change it to c++ code, but i am not expert in c++. can any body change the code to c++ for me?
plz i need it!!
public class Polynomial {
    private int[] coef;  // coefficients
    private int deg;     // degree of polynomial (0 for the zero polynomial)

    // a * x^b
    public Polynomial(int a, int b) {
        coef = new int[b+1];
        coef[b] = a;
        deg = degree();
    }

    // return the degree of this polynomial (0 for the zero polynomial)
    public int degree() {
        int d = 0;
        for (int i = 0; i < coef.length; i++)
            if (coef[i] != 0) d = i;
        return d;
    }

    // return c = a + b
    public Polynomial plus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a - b)
    public Polynomial minus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a * b)
    public Polynomial times(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, a.deg + b.deg);
        for (int i = 0; i <= a.deg; i++)
            for (int j = 0; j <= b.deg; j++)
                c.coef[i+j] += (a.coef[i] * b.coef[j]);
        c.deg = c.degree();
        return c;
    }

    // return a(b(x))  - compute using Horner's method
    public Polynomial compose(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, 0);
        for (int i = a.deg; i >= 0; i--) {
            Polynomial term = new Polynomial(a.coef[i], 0);
            c = term.plus(b.times(c));
        }
        return c;
    }


    // do a and b represent the same polynomial?
    public boolean eq(Polynomial b) {
        Polynomial a = this;
        if (a.deg != b.deg) return false;
        for (int i = a.deg; i >= 0; i--)
            if (a.coef[i] != b.coef[i]) return false;
        return true;
    }


    // use Horner's method to compute and return the polynomial evaluated at x
    public int evaluate(int x) {
        int p = 0;
        for (int i = deg; i >= 0; i--)
            p = coef[i] + (x * p);
        return p;
    }

    // differentiate this polynomial and return it
    public Polynomial differentiate() {
        if (deg == 0) return new Polynomial(0, 0);
        Polynomial deriv = new Polynomial(0, deg - 1);
        deriv.deg = deg - 1;
        for (int i = 0; i < deg; i++)
            deriv.coef[i] = (i + 1) * coef[i + 1];
        return deriv;
    }

    // convert to string representation
    public String toString() {
        if (deg ==  0) return "" + coef[0];
        if (deg ==  1) return coef[1] + "x + " + coef[0];
        String s = coef[deg] + "x^" + deg;
        for (int i = deg-1; i >= 0; i--) {
            if      (coef[i] == 0) continue;
            else if (coef[i]  > 0) s = s + " + " + ( coef[i]);
            else if (coef[i]  < 0) s = s + " - " + (-coef[i]);
            if      (i == 1) s = s + "x";
            else if (i >  1) s = s + "x^" + i;
        }
        return s;
    }

    // test client
    public static void main(String[] args) { 
        Polynomial zero = new Polynomial(0, 0);

        Polynomial p1   = new Polynomial(4, 3);
        Polynomial p2   = new Polynomial(3, 2);
        Polynomial p3   = new Polynomial(1, 0);
        Polynomial p4   = new Polynomial(2, 1);
        Polynomial p    = p1.plus(p2).plus(p3).plus(p4);   // 4x^3 + 3x^2 + 1

        Polynomial q1   = new Polynomial(3, 2);
        Polynomial q2   = new Polynomial(5, 0);
        Polynomial q    = q1.plus(q2);                     // 3x^2 + 5


        Polynomial r    = p.plus(q);
        Polynomial s    = p.times(q);
        Polynomial t    = p.compose(q);

        System.out.println("zero(x) =     " + zero);
        System.out.println("p(x) =        " + p);
        System.out.println("q(x) =        " + q);
        System.out.println("p(x) + q(x) = " + r);
        System.out.println("p(x) * q(x) = " + s);
        System.out.println("p(q(x))     = " + t);
        System.out.println("0 - p(x)    = " + zero.minus(p));
        System.out.println("p(3)        = " + p.evaluate(3));
        System.out.println("p'(x)       = " + p.differentiate());
        System.out.println("p''(x)      = " + p.differentiate().differentiate());
   }

}

AnswerRe: java source to c++ (help me) Pin
Richard MacCutchan21-May-11 21:20
mveRichard MacCutchan21-May-11 21:20 
QuestionSOAP Error on Sending with SAAJ0537 Pin
omlac20-May-11 3:38
omlac20-May-11 3:38 
AnswerRe: SOAP Error on Sending with SAAJ0537 Pin
Nagy Vilmos20-May-11 5:44
professionalNagy Vilmos20-May-11 5:44 
GeneralRe: SOAP Error on Sending with SAAJ0537 Pin
omlac22-May-11 2:43
omlac22-May-11 2:43 
GeneralRe: SOAP Error on Sending with SAAJ0537 Pin
omlac22-May-11 3:08
omlac22-May-11 3:08 
AnswerRe: SOAP Error on Sending with SAAJ0537 Pin
all_in_flames8-Jun-11 7:41
professionalall_in_flames8-Jun-11 7:41 
QuestionHow to build a online game being like "Hearts" of Microsoft Windows??? Pin
nhutanh1818-May-11 17:50
nhutanh1818-May-11 17:50 
AnswerRe: How to build a online game being like "Hearts" of Microsoft Windows??? Pin
TorstenH.18-May-11 19:36
TorstenH.18-May-11 19:36 
AnswerRe: How to build a online game being like "Hearts" of Microsoft Windows??? Pin
jschell19-May-11 8:23
jschell19-May-11 8:23 
GeneralRe: How to build a online game being like "Hearts" of Microsoft Windows??? Pin
TorstenH.19-May-11 21:25
TorstenH.19-May-11 21:25 
QuestionDisplacement Filter in Java Pin
uzairkamal17-May-11 8:23
uzairkamal17-May-11 8:23 
AnswerRe: Displacement Filter in Java Pin
TorstenH.17-May-11 23:38
TorstenH.17-May-11 23:38 
QuestionHow to get row and column count with POI? Pin
leejs41316-May-11 20:54
leejs41316-May-11 20:54 
AnswerRe: How to get row and column count with POI? Pin
TorstenH.17-May-11 23:34
TorstenH.17-May-11 23:34 
AnswerRe: How to get row and column count with POI? Pin
JP_Rocks29-May-11 22:32
JP_Rocks29-May-11 22:32 
QuestionTime in Java Pin
williamroma16-May-11 11:26
williamroma16-May-11 11:26 
AnswerRe: Time in Java Pin
Richard MacCutchan16-May-11 12:47
mveRichard MacCutchan16-May-11 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.