Results 1 to 9 of 9
  1. #1
    jcardozo is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default How to create class from a C structure

    Hello all,
    I have a C program that I need to convert to Java but dont know how. Can someone kindly provide me some pointers on how to go about it.
    Here is the C code:

    /** How to "classify" this code into Java?? **/
    int GetTranVal(char *Field)
    {
    printf("input field=%s\n", Field);
    return 20;
    }

    int GetTermId(char *Field)
    {
    printf("input field=%s\n", Field);
    return 123456789;
    }

    struct _TNFrequest{
    char *field;
    int value;
    (int)(*CalFieldVal)(char *Field);
    } TNFrequest[] = {
    {"TranCode", 70, GetTranVal},
    {"TerminalID", 999, GetTermId},
    };

    int main(int argc, char* argv[])
    {
    int i;
    for (i=0; i<sizeof(TNFrequest)/sizeof(struct _TNFrequest); i++)
    {
    TNFrequest[i].value = TNFrequest[i].CalFieldVal(TNFrequest[i].field);
    printf("getval=%d\n", TNFrequest[i].value);
    }
    }

    Much appreciated,
    Jack

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Do you mean something to automatically convert it? C and Java are VERY similar(since Java is written in C). You could easy just go through and change all the printf statements to System.out.println statements.

    Its been a while for me but the char* and *Field mean your using pointers and references correct? I don't believe java uses pointers in the same sense as C so those would need to be changed to objects I believe(I'm sure someone else with more experience could be of more use, cough nicholas_jordon, fubarable, fishtoprecords, db, any pretty much anyone else with over 100 posts)

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Interesting...

    I'll try to see if I can help a little (very rusty in C)...
    • GetTranVal & GetTermId are methods and are almost coded the same way in Java.
    • printf ("/n") = System.out.println ... like xcallmejudasx indicated
    • Java doesn't have pointers, therefore Field would be a String type (if it's representing a string).
    • Structures don't exist in Java either... I've been told that the equivalent is a class but I've never tried to implement a struct as a class (I just might try it :-).
    • Arrays (TNFrequest[]) are similiar in Java. In this cases it looks like you have an array of structs or in java in would be an array of classes (which can be done).
    • The main method is pretty much the same only java only has one array for the method arguments (String [] args).
    • The for loop is exactly the same
    • Depending on the class, length or length(), is used to find out the size/length of an object


    I haven't been in C for a very long time (not so hot with Java either :-)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default What is CalFieldVal ?

    jcardozo... question: What does the following mean?:
    Java Code:
    (int)(*CalFieldVal)(char *Field);
    • Is it a pointer to a function?
    • What is CalFieldVal?

    Thanks,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    jcardozo is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    jcardozo... question: What does the following mean?:
    Java Code:
    (int)(*CalFieldVal)(char *Field);
    • Is it a pointer to a function?
      Yes
    • What is CalFieldVal?

    Thanks,
    CJSL
    CalFieldVal is defined as a pointer to a function taking a pointer arg and returning an int value.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by jcardozo View Post
    CalFieldVal is defined as a pointer to a function taking a pointer arg and returning an int value.
    So that would work the same as getting an attribute of an instance of an object? Say you have a Person class that creates person objects and each object has a name and age attribute. Your essentially pointing to one of the person objects and returning their age?

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post Cay S. Horstmann

    Quote Originally Posted by jcardozo View Post
    I have a C program that I need to convert to Java but dont know how. Can someone kindly provide me some pointers on how to go about it.
    Gladly,... see comments in code:
    Java Code:
    import java.lang.util.*;// Wildcard import of useful bag of tools.
    /**
     * This code is a discussion in a technical forum.
     * The code is known to be incomplete and may contain errors.
     * You may study the code, you may attempt to implement parts
     * in your own work provided you make that code roubust.
     */
    
    public class Classify extends Object
    {
        /** To start off with, everything is in a class in Java **/
        private TNFrequest[] TNFrequestArray;//
        // default constructor
        public Classify()
        {
            //
            TNFrequestArray = new TNFrequest[15];// or some number,...
        }
        // constructor taking an int
        public Classify(int size)
        {
            // 
            TNFrequestArray = new TNFrequest[size];// or some number,...
        }
        // http://java.sun.com/developer/technicalArticles/Programming/sprintf/
        int GetTranVal(String Field)
        {
            System.out.println(Field);
            return Field.length();
        }
        //
        int GetTermId(String Field)
        {
            // I am going to dummy this, we have a printf in Java 
            // but I do not know where it is or how to use it. 
            // Just about everything can do printing of strings,
            // Formatting is probably in 
            // C:\src_jdk1.5.0_12\java\text\DecimalFormat.java
            // or something in the java.lang.util.*;  
            // printf("input field=%s\n", Field);
            return 123456789;
        }
        // This is called a nested class, I don't know how to do them but 
        // you have put several things in one place so in Java, that's a class.
        private class TNFrequest // make it private for design reasons.
        {
            private final String TranCode = "TranCode";
            private final String TerminalID = "TerminalID";//
            String transCode;
            String terminalID;
            TNFrequest(String transCode, String terminalID)
            {
                this.transCode = transCode;
                this.terminalID = terminalID;
            }
        }
        // I am going to skip out on main right at the moment as we
        // have some design work to do before we get sensible main in scope...
    }
    Here's a stack trace in Java:
    Java Code:
    java.lang.ClassNotFoundException: printfapplet.class
        at sun.applet.AppletClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.applet.AppletClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.applet.AppletClassLoader.loadCode(Unknown Source)
        at sun.applet.AppletPanel.createApplet(Unknown Source)
        at sun.plugin.AppletViewer.createApplet(Unknown Source)
        at sun.applet.AppletPanel.runLoader(Unknown Source)
        at sun.applet.AppletPanel.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.io.IOException: open HTTP connection failed.
        at sun.applet.AppletClassLoader.getBytes(Unknown Source)
        at sun.applet.AppletClassLoader.access$100(Unknown Source)
        at sun.applet.AppletClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        ... 10 more
    Very handy, use them in development, take them out for shipping and replace with a Log File, print very abbreviated message to user to contact system administration and leave enough in the log so they can discover the general area.

    An actual failure in java may be a few lines away due to the nature of being an adjunct rather than a total OS / tight code.

    Java Objects are essentially a reference, but we won't see a lot of talk about that - the camp style is to focus on the design.

    There are many masters at that, you will have to dig somewhat to find the actual workings. Most of the headers ( C style headers ) are available readily in just about any JDK installation:
    Java Code:
    C:\Program Files\Java\jdk1.6.0\include\jvmti.h
    I suggest Cay S. Horstmann: ISBN: 978-0132354769 and 978-0132354790 as this is someone who is speaking the Good Bean from a cs informed position....imports are closer to namespaces than #include in C/C++ there is a pre-processor but we get into some rather interesting crossfire discussing it short of advanced issues. See the Quiz post in advanced to get some hot potatoes on your main burner......
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    jcardozo is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    I appreciate you taking the time to actually construct the class which is what I was having trouble with.
    I'm beginning to appreciate Java a little better, having come from a diehard "C" background.
    Thanks again!

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Red face your help wanted

    I could see that, there are five or six things to cover actually. No void anything anywhere, const is "final" and totally skips the "constant keyword binds to the left" just like in C/C++

    Correctly, a constant would be

    char const * const aRealCharacter = 'a';// an unmodifiyable ...

    and what we have in Java is a reducto ad absurdum - final designates a class that cannot be extended. Best use of the word is to eliminate vftable switching being placed in the code needlessly - we have true static finals in java, they can be initialized with static{.....} which I forget about sometimes but find extremely handy when I get warnings about final var not to be assigned in constructor.

    [] has full object status, and may be used something like a pointer but do not use the word pointer in front of Java OO'ers as they will gang up on you and tell your student that there is no such thing. Kind of like late night tv or something.

    Everything, absolutely everything, has a type in Java. Trying to get unsigned behaviour from a byte[] is better studied in the crypto libs, BC is the tool of choice for crypto in Java. There is class BigInteger and class BigDecimal which brings power and utility for financial and data protection that normally can be gained in C/C++ by purchase of proporietary libs. Java's libs put STL to shame and leaves them in the cold, dark grassland of November chill winds.

    Sedgewick and Wayne along with Horstmann should be the author(s) of choice for someone with skills already in funamental cs. I find the study guides ( by any author ) to be consistently effective in skipping the toy code, ....

    I'm full of it, I have plenty of advice to give ~ but one thing I do not have is time in a real code shop. I met a cs at ( retailer ) who says writing code is a pain in the archives. I would appreciate opinions on the matter of what I see to be an ineffective coding style in commercial shops vis-a-vis the typical cs coder liking the sytax of c/cpp/java rather than cobol or rpg. I am trying to bring to market an ai product and know from life experience that I have some blunderously weak ideas easily snapped by rat-traps.

    Writing code - a'la Java - is fun and fulfilling. If it is that easy, then profitable work in the area will be fiercely protected by mythology and socialization.

    Any thoughts on that? We have a joke area where I raised the issue of students not gain valuable experience. I thought that was pretty close to the fire.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. How to create a class in java?
    By pawankumarom in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 07:47 AM
  2. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  3. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM
  4. Replies: 1
    Last Post: 04-09-2008, 02:15 AM
  5. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •