ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!
Home
Upgrade
Credits
Help
Search
Awards
Achievements
 5157

Help covert this code to Java

by DeathWishHacks - 03-10-2017 - 07:37 AM
#1
Can anyone help me covert this C++ code to java?

#include <iostream>
#include <string>
#include <cmath>

bool IsPrime(int Num) {
if (Num < 2) return false;
double Sqr = std::sqrt(Num);
for (int x = 2; x <= (int)Sqr; x++)
if (Num % x == 0)
return false;
return true;
}

bool IsPalindrome(std::string Text) {
for (int x = 0, y = Text.length() - 1; x < Text.length(); x++, y--)
if (Text.at(x) != Text.at(y))
return false;
return true;
}

int main() {
for (int i = 1; i < 20000; i++)
if (IsPrime(i) && IsPalindrome(std::to_string(i)))
std::cout << i << std::endl;
std::cout << "Done.";
std::cin.ignore();
return 0;
}
-ᗪᗴᗩ♈ᖺᙡᓮᔕᖺ,

"Time to get my hack on" - Sum Cool Haxor
Reply
#2
Good ole Java... been awhile since I messed with it. I stick around C, Python, Css/HTML ... hope someone can help you bud.
[Image: giphy.gif]
Reply
#3
To start off with, you'll need to put it into a class. Given this code seems to be looking for prime palindromes, I'll call the class 'PrimePalindrome'.

Starting with the 'IsPrime' method, 'bool' is 'boolean' in Java, and 'std::sqrt' becomes 'Math.sqrt'. Most things from 'cmath' can be found as static methods of the 'Math' class. Additionally, since this method is completely irrelevant of class instance, we'll add the 'static' keyword to it. The method becomes:

Code:
public static boolean IsPrime(int Num) {
   if (Num < 2) return false;
   double Sqr = Math.sqrt(Num);
   for (int x = 2; x <= (int)Sqr; x++)
   if (Num % x == 0)
       return false;
   return true;
}

Applying similar translations for 'IsPalindrome' also works. 'std::string' becomes the 'String' type for Java. Note that primitive such as 'int' are lower cased where classes such as 'String' are upper cased. 'String' does not have an 'at' method, but it does have a 'charAt' method that does the same thing as 'std::string' 'at' method.

Code:
public static boolean IsPalindrome(String Text) {
   for (int x = 0, y = Text.length() - 1; x < Text.length(); x++, y--)
       if (Text.charAt(x) != Text.charAt(y))
           return false;
   return true;
}

Printing in Java is done via the 'System.out' object which includes things such as 'print', 'println', and 'printf'. Java unfortunately does not have an analogue to 'std::cin.ignore()', but you can emulate it with repeated calls to 'System.in.skip(<length>)'. There is no 'std::to_string' and each individual type has a 'toString' method. In this case, 'Integer.toString' converts the integer to a string. Additionally, Java's 'main' method does not let you return an int (you must instead use 'System.exit' if you want to give a return code other than 0) and it must take 'String[] args' for arguments. We must also declare the 'main' might throw an 'IOException'. This is mostly a formailty and tells Java that the program might die if input or output hits an error case.

Code:
public static void main(String[] args) throws Exception {
   for (int i = 1; i < 20000; i++)
       if (IsPrime(i) && IsPalindrome(Integer.toString(i)))
           System.out.println(i);
   System.out.println("Done");
   while (System.in.available() != 0)
       System.in.skip(65535);
}

Putting it all together, it becomes:

Code:
public class PrimePalindrome {
   public static boolean IsPrime(int Num) {
       if (Num < 2) return false;
       double Sqr = Math.sqrt(Num);
       for (int x = 2; x <= (int)Sqr; x++)
       if (Num % x == 0)
           return false;
       return true;
   }

   public static boolean IsPalindrome(String Text) {
       for (int x = 0, y = Text.length() - 1; x < Text.length(); x++, y--)
           if (Text.charAt(x) != Text.charAt(y))
               return false;
       return true;
   }

   public static void main(String[] args) throws Exception {
       for (int i = 1; i < 20000; i++)
           if (IsPrime(i) && IsPalindrome(Integer.toString(i)))
               System.out.println(i);
       System.out.println("Done");
       while (System.in.available() != 0)
           System.in.skip(65535);
   }
}

I've compiled and tested this code with 'javac PrimePalindrome.java && java PrimePalindrome'.

These translations are rough and do not give you much insight into the Java language. See a Java tutorial such as https://www.tutorialspoint.com/java/index.htm for more good information on Java.
Reply
#4
Have you attempted to consider using JNI instead of going through all of this translation shit?
Reply

Users browsing: 3 Guest(s)