Solved

java to C translation

Posted on 2004-09-23
9
179 Views
Last Modified: 2010-04-15
Hi Experts,

Can you help me convert the following codes (3 seperate files) from Java to C?

main.java
======
class main {
    public static void main(String args[]) {
        MAC mac = new MAC();
        System.out.println("mac.getValue()="+mac.getValue());
    }
}

mac.java
======
class MAC {
    public static int staticIntValue;
    public MAC() {
        renderer = new Renderer();
        start();
    }
    // Fill in staticIntValue with an integer.
    public void start() {
        isStarted = true;
        int intValue = 1234;
        staticIntValue = intValue;
        renderer.start();
    }
    // this will return the integer value stored in staticIntValue
    public int getValue() {
        return staticIntValue;
    }
    private boolean isStarted = false;
    public Renderer renderer;
};

renderer.java
=========
class Renderer {
    public Renderer() {
    }
    // Adjust MAC.staticIntValue
    public void start() {
        isStarted = true;
        MAC.staticIntValue++;
    }
    private boolean isStarted = false;
};

Compile these files in Java: javac *.java
Run: java main
Result: mac.getValue()=1235

Thanks,
Eddy
0
Comment
Question by:eddyphung
  • 5
  • 3
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12139060
homework?
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12143827
I don't think so. but to be on safe side i will only insert enough comments/hints so that the author can look up appropriate fxn. you better use c++.

import SOME_JAVA_CRAP.io.*;
changes to
#include <stdio.h> //in C
#include <iostream> in C++
use #include <cstdio> if you want to use c-style functions

main.cpp
=====
int main(){ //no need to use wrapper for main
  MAC mac;
  /*
   in java you get a handle (c equivalent: pointer) by this declaration bu in c you get the actual object. so no need to use new to create a new object of class MAC
  */
  //System.out.println() changes to printf();
  printf( DO_IT_YOURSELF );

  return 0;
}

mac.h
======
remove all public/private keywords/qualifiers from variable declarations. change to:
public:
  type1 fxn1();
  //this is only a prototype, good practice is to not define functions here. just tell the user & compiler how to use the function
  //but if you still want to define it here, nothing can stop you. if you define it here it would look like this
  type1 fxn2( PARAMS_LIST ){
    //your code
  }

private:
  type2 var2;
etc.

you would then create mac.cpp that contains function definitions
mac.cpp
=======
#include "mac.h" //this is not optional!

type1 mac::fxn1(){
  code;
}

for renderer class
==============
similarly create two files renderer.h and renderer.cpp for this class.

private boolean isStarted = false; // ERROR!!! can NOT do this
must only declare the variable here and initialize it in the constructor [ Renderer(){...} ]

further questions?

jaydutt
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12143839
"you better use c++" is for eddy
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:eddyphung
ID: 12145028
Hi Jaydutt,

I am working on a project of converting Java code to ANSI C.  I already have the macro to compile these codes in either Java or C++.  
The real challenge of this project is to get these codes to run in both Java and C environments.

.eddy
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12149461
that is a big challenge! could you give me a little more info about your project and runtime environment?
0
 

Author Comment

by:eddyphung
ID: 12163017
Yes, this is a very challenging project.  I want to find a way retarget the Java code to platform-specific machine code.  This platform will be using an ANSI C cross-compiler.
Most likely, I will have to rewrite this code in C due to OOP.  I was thinking of a Java to C translator but this would take a lot of work.  Is there any available tool out there to do such that? Or any suggestion to go about?  More points added if neccessary.

.eddy
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 125 total points
ID: 12179096
so what is it that you want to do? do you want to re-write the code (with macros inserted) that will compile with either Java or ANSI C without any trouble?
0
 

Author Comment

by:eddyphung
ID: 12181711
Hi Jaydutt,

Yes and I've found the solution.  I will rewrite the original Java code using macros and hints, then write some scripts to translate the codes to either Java or C based on those hints.  I think this should work.
Thanks for your input.  You deserve the points.  

Regards,
.eddy
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12182307
thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question