Solved

java to C translation

Posted on 2004-09-23
9
177 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now