Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java to C translation

Posted on 2004-09-23
9
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

671 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