• Post Reply Bookmark Topic Watch Topic
  • New Topic

Translationg Java to pseudocode  RSS feed

 
djamel Ait Amrane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I want to understand this code :, please help to convert it to pseudoCode.
Thank you
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand about it?
Where did you get this code from (--> QuoteYourSources)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


are you familiar with the ternary operator?

My personal opinion is that this is poorly formatted code. I'd have written it like this:

actually, I probably wouldn't. I don't like the ternary operator, but there is nothing wrong with it. I think it's an unjust bias I have.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's almost pseudo code already. Two things may be tripping you up:

1) φ2 is just a variable name, like x.

2) There is a construct in Java that looks like this: cond ? trueStatement : falseStatement; It's equivalent to an if statement except that it returns the value of trueStatement or falseStatement.

So with this information, what do you think the pseudo code would look like?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had written that code I would either have written it using the ternary operator twice, or using if-then-else twice. I wouldn't have mixed the two.

(And then I would have noticed that the version with two nested ternary operators was horribly unreadable and changed it to two nested if's.)

But anyway, so far we don't know whether Djamel has a problem understanding the Java code or a problem understanding the underlying trigonometry. So yes, clarification would be helpful.
 
djamel Ait Amrane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do tanslate it ?

if (Math.abs(φ2) > Math.PI/2) φ2 = φ2>0 ? Math.PI-φ2 : -Math.PI-φ2;

If Abs(phi2) > pi/2) understood.
Phi2 = phi2>0 not understand.
how to convert ternary operator: using True or False , the variable must be Boolean.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, I see the hard-to-understand part now. Try reading the ternary operator part like this:



This is how the compiler sees it (and how it should have been written to make it more readable).

Does that help?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless OP understands the purpose of this particular line - technical part is irrelevant I think.
Including me, as i'm not sure 100%, if i'm not wrong, it is about precision comparing floating point numbers.
Am i right?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You might do well to see the conditional operator in a simpler context. The random.nextInt() call returns a pseudo-random number either 0 or 1, so you get different outputs depending on the value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!