Results 1 to 13 of 13
Thread: Convert a piece of C++ to Java
- 03-11-2015, 03:09 PM #1
Member
- Join Date
- Mar 2015
- Posts
- 7
- Rep Power
- 0
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
- 03-11-2015, 03:15 PM #2
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."It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 03-11-2015, 03:26 PM #3
Member
- Join Date
- Mar 2015
- Posts
- 7
- Rep Power
- 0
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
- 03-11-2015, 03:31 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Convert a piece of C++ to Java
Simply change this structure:
Java Code:st: if ( ... ) { ... goto st; }
Java Code:while ( ... ) { ... }
JosLast edited by Norm; 03-11-2015 at 04:28 PM. Reason: Added ] to code tag
Build a wall around Donald Trump; I'll pay for it.
- 03-11-2015, 04:01 PM #5
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!
- 03-11-2015, 04:06 PM #6
Member
- Join Date
- Mar 2015
- Posts
- 7
- Rep Power
- 0
Re: Convert a piece of C++ to Java
Thanks JosAH !!!
It worked like a charm
Kind regards,
Kepler
- 03-11-2015, 04:18 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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 ...
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-11-2015, 04:29 PM #8
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.
- 03-11-2015, 04:54 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 03-11-2015, 05:12 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 03-11-2015, 05:17 PM #11
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
- 03-11-2015, 05:22 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 03-11-2015, 05:32 PM #13
Similar Threads
-
Java Chess, piece moving several steps
By Gatsu in forum New To JavaReplies: 5Last Post: 01-29-2013, 09:17 PM -
I stuck my project from any piece
By papyon in forum Java 2DReplies: 3Last Post: 12-05-2011, 09:06 PM -
Need help with a piece of code
By sneeak in forum New To JavaReplies: 13Last Post: 08-24-2011, 11:51 AM -
Java Chess piece Array
By obious in forum New To JavaReplies: 1Last Post: 05-04-2011, 02:20 PM -
Working out chess piece location / piece name
By danborgir in forum New To JavaReplies: 5Last Post: 04-20-2011, 11:14 AM
Bookmarks