+ 1

Does anyone know how can I implement the same Python code in Java?

https://code.sololearn.com/cxi5YxKVhLTe/?ref=app

20th Jun 2020, 7:06 AM
Blue!!
Blue!! - avatar
6 Respostas
+ 2
There is a project called Jython, an implementation of Python in written in Java. (See https://www.jython.org). Hope that helps.
21st Jun 2020, 3:22 AM
SapphireBlue
SapphireBlue - avatar
0
Just copy it and replace the Python keywords by the corresponding Java keywords. E.g. "def name" by "private static <return type> name" and so on. It's not too hard if you've already coded something in Java. Everything outside the def-section is moved into the main-method (here, in this small scope).
20th Jun 2020, 5:45 PM
Sandra Meyer
Sandra Meyer - avatar
0
But in Java I can't create function inside a function. Note that: when car is calling cons(3, 4) which returns left side(3) and when cdr calling the same cons(3, 4) which returns right side(4).
20th Jun 2020, 6:46 PM
Blue!!
Blue!! - avatar
0
As I said, you move the content of the functions into methods and the rest into the main method. Then you finally have 4 methods: 1 main and your 3 functions.
20th Jun 2020, 6:48 PM
Sandra Meyer
Sandra Meyer - avatar
0
I don't get it.. would you like to code it for me..
20th Jun 2020, 7:40 PM
Blue!!
Blue!! - avatar
0
The problem is creating method inside a method. I need to call a method cdr() which will take another method cons(3, 4) and return the right value, like this cdr(cons(3, 4)). But when car method is called in the same way, i.e. car(cons(3, 4)) it should return left value. Note that Python is very good at functional programming and data manipulation, where Java is object oriented and always need a return type. I tried to return the values in class but that ended up returning only the reference of that data.
21st Jun 2020, 11:03 PM
Blue!!
Blue!! - avatar