Results 1 to 7 of 7

Thread: Convert C++ code into JAVA

  1. #1
    Join Date
    Nov 2009
    Posts
    47

    Convert C++ code into JAVA

    I have some problem with my JAVA program. After a lot of search on internet I found the solution but unfortunately it was in C++. I wanted to convert it into JAVA. Is it possible? Can I convert this line of code from C++ to Java:

    Code:
    typedef int (*td_fn_LoadingProgress ) (int , void *) ;

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Convert C++ code into JAVA

    typedef + pointer of function (if my memory does not deceive me)? It will not be possible directly. In the worst case we should cope with the creation of a new type but it will also change the code around:
    Code:
    public interface FuncCpp {
      public int td_fn_LoadingProgress(int a, Object b);
    }
    or a class like:
    Code:
    public class FuncCpp {
      public int td_fn_LoadingProgress(int a, Object b) {
         return 0; 
      }
    }
    Obviously with that closures would be simpler

  3. #3
    Join Date
    Nov 2009
    Posts
    47

    Re: Convert C++ code into JAVA

    Then how do I do in the case of pointer to array?
    Code:
    DcmData ** p_dcmdata;
     
    (*p_dcmdata)->DeleteAllElts();
    as DcmData is a class and a method DeleteAllElts is included in this class.

    in java at the moment there are no pointers how I'm doing? I already tried
    Code:
    DcmData [ ] p_dcmdata;
    but it is not working ?

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Convert C++ code into JAVA

    I had trouble understanding the question... With the Reflection API, you could perhaps get out of this situation. You use the object Method (which is a "kind of pointer" and point to a true method of a class). Then use invoke to realize the call.

    Code:
    public class DcmData
    {
    	public void DeleteAllElts(int a, Object b)
    	{
    		System.err.println("Erase the elements ?! "+a+" - "+b);
    	}
    }
    Code:
    public class main
    {
    	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
    	{
    		DcmData p_dcmdata = new DcmData();
    		p_dcmdata.DeleteAllElts(0, "foo");
    		
    		DcmData ptr_dcmdata = p_dcmdata;
    		Method method_pseudo_ptr = p_dcmdata.getClass().getMethod("DeleteAllElts", int.class, Object.class);
    		method_pseudo_ptr.invoke(p_dcmdata, 0, "FOO");
     
    	}
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    47

    Re: Convert C++ code into JAVA

    my question is:
    I said something in c++ this way:
    Code:
    DcmData ** p_dcmdata;
    the appeal of the object are of course so it is a pointer matrix, in particular lines
    Code:
    (*p_dcmdata)->DeleteAllElts();
    *p_dcmdata = new DcmData();
    I try to convert this to java and in java there is no concept of pointer

  6. #6
    Join Date
    Nov 2008
    Posts
    996

    Re: Convert C++ code into JAVA

    This is normal since you said it like a picture ...
    -You must call the method on one (or several) element(s) of the table.

    But more broadly, between the message and the other to release memory, I feel that's trying to code in C++, but Java.

    Basically you used to do "like that" in C++, and you want to do the same thing in Java.

    In my opinion it is a big mistake, because despite a relatively close syntax, concepts and notions one set up by the two languages are very different. And what is appropriate for one is not to another ...
    -You should not ask the question of how to "translate" the C++ with Java!

    You should really detail what you want to reach (what you want to achieve?) And see how you would do in Java.

  7. #7
    Join Date
    Nov 2011
    Posts
    1

    Re: Convert C++ code into JAVA

    PLZ CONVERT THIS C++ CODE INTO JAVA:
    /* bINARY tREE cODE*/
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    struct node
    {
    int info;
    struct node *left;
    struct node *right;
    };
    typedef struct node *NODEPTR;
    NODEPTR getnode(void)
    {
    NODEPTR p;
    p=(NODEPTR)malloc(sizeof(struct node));
    return(p);
    }
    NODEPTR maketree(int x)
    {
    NODEPTR p;
    p=getnode();
    p->info=x;
    p->left=NULL;
    p->right=NULL;
    return p;
    }
    void setleft(NODEPTR p,int x)
    {
    if(p==NULL)
    printf("void insertion\n");
    else if(p->left!=NULL)
    printf("invalid insertion\n");
    else
    p->left=maketree(x);
    }
    void setright(NODEPTR p,int x)
    {
    if(p==NULL)
    printf("void insertion\n");
    else if(p->right!=NULL)
    printf("invalid insertion\n");
    else
    p->right=maketree(x);
    }
    void pretrav(NODEPTR p)
    {
    if(p!=NULL)
    {
    printf("%d\n",p->info);
    pretrav(p->left);
    pretrav(p->right);
    }
    }
    void intrav(NODEPTR p)
    {
    if(p!=NULL)
    {
    intrav(p->left);
    printf("%d\n",p->info);
    intrav(p->right);
    }
    }
    void posttrav(NODEPTR p)
    {
    if(p!=NULL)
    {
    posttrav(p->left);
    posttrav(p->right);
    printf("%d\n",p->info);
    }
    }
    int main()
    {
    NODEPTR ptree;
    NODEPTR p,q;
    int number;
    printf("Enter the numbers to be inserted");
    scanf("%d",&number);
    ptree=maketree(number);
    while(scanf("%d",&number)!=EOF)
    {
    p=q=ptree;
    while(number!=p->info&&q!=NULL)
    {
    p=q;
    if(number<p->info)
    q=p->left;
    else
    q=p->right;
    }
    if(number==p->info)
    printf("duplicate");
    else if(number<p->info)
    setleft(p,number);
    else
    setright(p,number);
    }
    printf("preorder traversal");
    pretrav(ptree);
    printf("inorder traversal");
    intrav(ptree);
    printf("postorder traversal");
    posttrav(ptree);
    getch();
    return 0;
    }

Similar Threads

  1. Convert C++ Program to Java Code!! Please help
    By jessesaini in forum Software Development
    Replies: 1
    Last Post: 24-04-2012, 12:24 AM
  2. pls convert this c++ to java code
    By xSim21 in forum Software Development
    Replies: 4
    Last Post: 26-03-2012, 06:32 PM
  3. Need help to convert c++ to java code program
    By xSim21 in forum Software Development
    Replies: 2
    Last Post: 27-11-2010, 04:42 PM
  4. Convert a Java Bean Code
    By KALYAN23 in forum Software Development
    Replies: 5
    Last Post: 02-08-2010, 10:34 AM
  5. convert C++ program in Java code
    By vaibhavsri in forum Software Development
    Replies: 7
    Last Post: 16-07-2010, 10:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,481,685.94415 seconds with 17 queries