• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

java to C translation

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
eddyphung
Asked:
eddyphung
  • 5
  • 3
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
homework?
0
 
jhshuklaCommented:
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
 
jhshuklaCommented:
"you better use c++" is for eddy
0
Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

 
eddyphungAuthor Commented:
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
 
jhshuklaCommented:
that is a big challenge! could you give me a little more info about your project and runtime environment?
0
 
eddyphungAuthor Commented:
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
 
jhshuklaCommented:
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
 
eddyphungAuthor Commented:
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
 
jhshuklaCommented:
thanks
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now