Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By JosAH

Thread: Convert a piece of C++ to Java

  1. #1
    kepler is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Question Convert a piece of C++ to Java

    Hello,

    I'm having some difficulty in converting this piece of C++ code to Java. Can someone help me? As far as I know, "There are no gotos or labels in Java".


    double splint(double xa[], double ya[], double y2a[], int n, double x)

    {
    int k, khi, klo;
    double h, a, b;

    klo = 1;
    khi = n;

    st: if (khi - klo > 1)
    {
    k = (int) ((khi + klo) / 2.0);
    if (xa[k] > x)
    khi = k;
    else
    klo = k;
    goto st;
    }
    h = xa[khi] - xa[klo];
    a = (xa[khi] - x) / h;
    b = (x - xa[klo]) / h;
    return (a * ya[klo] + b * ya[khi] + ((pow(a,3) - a) * y2a[klo] + (pow(b,3) - b) * y2a[khi]) * (pow(h,2)) / 6);

    }


    Kind regards,

    Kepler

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Convert a piece of C++ to Java

    Java has labels, but no goto's.

    This label/goto construct is likely convertable to a while-loop. The pow() function has a replacement in the Math class. Other than that, it's nearly Java compatible.
    kepler likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    kepler is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Re: Convert a piece of C++ to Java

    Hi,

    Could you help me out in the translation? ONLY if you have the time, of course.

    Kind regards,

    Kepler

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Convert a piece of C++ to Java

    Simply change this structure:

    Java Code:
    st: if ( ... ) {
       ...
       goto st;
    }
    to this:

    Java Code:
    while ( ... ) {
       ...
    }
    kind regards,

    Jos
    Last edited by Norm; 03-11-2015 at 03:28 PM. Reason: Added ] to code tag
    kepler likes this.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Convert a piece of C++ to Java

    Generally, you don't "translate" code directly from one language to another. You don't go line-by-line trying to figure out how to do each line one at a time.

    Instead, you take code in one language and figure out what it's doing, and then you start over and figure out how to do that in the other language.

    So, the first question you need to answer is: what does this code do?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  6. #6
    kepler is offline Member
    Join Date
    Mar 2015
    Posts
    7
    Rep Power
    0

    Default Re: Convert a piece of C++ to Java

    Thanks JosAH !!!
    It worked like a charm

    Kind regards,

    Kepler

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Convert a piece of C++ to Java

    Hm, strange: the forum software skips the right square bracket from the '[code]' tag (it is there) ... stupid forum software ...

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Convert a piece of C++ to Java

    The ] was missing when I edited it. I restored it and it's working for me now.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Convert a piece of C++ to Java

    Quote Originally Posted by Norm View Post
    The ] was missing when I edited it. I restored it and it's working for me now.
    Strange, I could and would've sworn I saw a right square bracket when I wanted to edit my reply; and I wasn't even boozing yet ;-) Thanks for fixing it.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Convert a piece of C++ to Java

    Quote Originally Posted by JosAH View Post
    ... and I wasn't even boozing yet ;-) Thanks for fixing it.

    kind regards,

    Jos
    You stop?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Convert a piece of C++ to Java

    Quote Originally Posted by Tolls View Post
    You stop?
    He switches between boozing and "sampling".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Convert a piece of C++ to Java

    Quote Originally Posted by Tolls View Post
    You stop?
    Yes, on a daily basis.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Convert a piece of C++ to Java

    /takes a swig of beer

    Cheers!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java Chess, piece moving several steps
    By Gatsu in forum New To Java
    Replies: 5
    Last Post: 01-29-2013, 08:17 PM
  2. I stuck my project from any piece
    By papyon in forum Java 2D
    Replies: 3
    Last Post: 12-05-2011, 08:06 PM
  3. Need help with a piece of code
    By sneeak in forum New To Java
    Replies: 13
    Last Post: 08-24-2011, 10:51 AM
  4. Java Chess piece Array
    By obious in forum New To Java
    Replies: 1
    Last Post: 05-04-2011, 01:20 PM
  5. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •