Final Project REport
Final Project REport
TABLE OF CONTENTS
FRONTPAGE
CERTIFICATE
ACKNOWLEDGEMENT
ABSTRACT I
LIST OF FIGURES II
Chapter 1. INTRODUCTION 1
1.1 Project objective 4
1
C++ to java code converter
CHAPTER 1
INTRODUCTION
C++ to Java Converter software is designed for those persons who are aware
from C++.Java is easy to develop as compare to C++.Java shows feature of platform
independent and portable while C++ doesn’t. Java is true object oriented language while
C++ is basically C with object oriented extension.
In this project, we have two ways first is ‘straight forword way ‘ and another is a
complex approach. In complex way we required language translator, grammer from
theory of computation and the compiler design concept. While in straight forword
approach we simply applying the conversions by making the class files and other
functions related to C++ and Java.
Java is strongly associated with the internet because the fact that first application
program written in java a web browser to run applet on internet.This fascility is not
provide by C++. Java has replace destructor function with a finalize function.
The invetors of Java wanted to design a language which put offer solution to some of the
problems encountered in modern programming they waned the language to be not only
reliable, portable and distributed but also simple, complex compact and interactive.
Sun microsystems officially describes java with following attributes
- compiled and interpreted
- platform independent and portable and distributed
- robust and secure
C++ to Java Converter is the easiest way to generate Java code from C++ code. Our
conversion results speak for themselves. Although C++ to Java Converter cannot do all
the conversion work for you, using C++ to Java Converter is the best first step you can
take to migrate code from C++ to Java.
Folder conversion: Entire folders of C++ code files are converted to Java code
files.
Code snippet and file conversion: Our snippet conversion accuracy is outstanding
and does not require you to insert entire methods or classes. Heuristics are used to
convert code fragments with missing declarations.
2
C++ to java code converter
Options: You can specify numerous conversion options via our Options dialog.
Code formatting options, global function replacements, type & member replacements,
custom string replacements, header file options, and miscellaneous options.
Handles C, C++, Managed C++, and C++/CLI: C++ to Java Converter converts
from C, native C++, Managed C++ (VC++ 2003), and C++/CLI (VC++ 2005 and
above).
Use of 'helper classes' for C++ functions not directly convertible to Java: Some
C++ functions, such as strtok and rand, do not have direct equivalents in Java. Also,
Java has no natural support for 'pass by reference' parameters. To ease the transition to
Java, C++ to Java Converter inserts helper classes in the converted code to provide the
Java equivalent for these.
File comparison feature: After converting a folder, you can compare original and
converted files side-by-side. Comparing converted code to original code.
Running from the command line: C++ to Java Converter can be launched directly
for a specific folder or file conversion from the command line.
Most C/C++ library function calls: Some common C/C++ function calls are
replaced, but most are left unconverted and will not compile in the converted Java code.
Also, MFC and ATL resources are not converted.
See Common post-conversion adjustments for more items that are not converted.
3
C++ to java code converter
4
C++ to java code converter
2. Literature Survey
C++ to Java Converter may generate the following To Do tasks which are inserted as
comments during conversion. To Do tasks require your attention and will require you to
make some adjustment:
C++/CLI pin_ptr variables are not converted (rewrite using the Java 'fixed'
keyword): This code will have to be rewritten using the fixed keyword in Java.
Instead of '#using', in Java add a project reference: The way to reference another
assembly in Java is via the project references list.
Java does not allow bit fields: Java does not have an elegant equivalent to bit
fields.
Java does not allow declaring types within methods: Types cannot be declared
within methods in Java.
Java has no concept of a 'friend' class: friend classes are not available in Java.
Java has no concept of a 'friend' function: friend functions are not available in
Java.
Java has no concept of 'private' inheritance: Java has just one type of inheritance -
public.
Java has no concept of 'protected' inheritance: Java has just one type of inheritance
- public.
5
C++ to java code converter
Only the namespaces at the beginning of the file can be converted to the Java
'package' for this file: Nested packages are not allowed in Java.
Pointer arithmetic is detected on this variable, so pointers on this variable are left
unchanged: Although C++ to Java Converter can remove most pointer usage, if pointer
arithmetic (e.g., p++) is detected, the pointer indirections on that variable are left alone.
The C++/CLI 'gcnew()' constraint has no equivalent in Java: The C++/CLI generic
gcnew() constraint has no Java equivalent.
The C++/CLI 'ref class' and 'value class' generic constraints have no meaning in
Java: The C++/CLI generic ref class and value class constraints have no Java
equivalent.
The 'cout.fill' function is not converted by C++ to Java Converter: This function is
not converted by C++ to Java Converter.
The #define macro <...> was defined within a preprocessor conditional and cannot
be replaced in-line: If a #define macro was defined in a preprocessor conditional, then
it cannot be replaced in the subsequent line of code.
The 'far' modifier is not used in Java: This modifier is not available in Java.
The 'interrupt' modifier is not used in Java: This modifier is not available in Java.
The following #define constant was defined in alternate ways: #define constants
(e.g., #define FOO 2.5) are converted to constants, but if a #define constant was
defined in alternate ways in the original C++ code, you'll obtain this message.
The following typedef was ignored by C++ to Java Converter: Most typedef's can
be converted, but if a typdef cannot be converted, you will obtain this message.
The implementation of the following method could not be found: You will receive
this message when a method declaration is found, but no implementation is found.
The implementation of the following type could not be found: You will receive
this message when a type implementation cannot be found.
The MC++ __pin keyword is not converted (rewrite using the Java 'fixed'
keyword): This code will have to be rewritten using the fixed keyword in Java.
6
C++ to java code converter
The original C++ constructor header had calls to multiple base class constructors:
Since Java doesn't allow multiple inheritance, calls to multiple base class constructors
will also have to be reworked as part of the refactoring.
The original C++ template specifier was replaced with a Java generic specifier,
which may not produce the same behavior: C++ templates are replaced with Java
generics, but in most cases you'll need to refactor to achieve the same behavior.
The 'protected private' C++ modifier does not translate to any Java access
modifier: There is no equivalent in Java to protected private, so C++ to Java
Converter converts these to protected.
The typedef <...> was defined within a preprocessor conditional and cannot be
replaced in-line: If a typedef was defined in a preprocessor conditional, then it cannot
be replaced in the subsequent line of code.
There are no simple equivalents to delegates in Java: There are a few cumbersome
equivalents to delegates in Java, but these must be refactored after conversion.
There are no simple equivalents to events in Java: There are a few cumbersome
equivalents to events in Java, but these must be refactored after conversion.
There are no simple equivalents to function pointers in Java: Since Java lacks
delegates, there are no elegant equivalents to function pointers.
There is no Java equivalent to the classic C++ 'typeid' operator: Although the C+
+/CLI typeid property can be converted to Java, the classic C++ typeid function has no
direct equivalent in Java.
There is no direct equivalent in Java to the C++ <...> macro: Certain C++ built-in
macros, such as __LINE__, __FILE__, and __FUNCTION__ have no direct equivalent
in Java.
There is no equivalent to escaping the '?' character in Java: The escape sequence \?
is not available in Java.
There is no equivalent to most C++ 'pragma' directives in Java: Only a few of the
C++ pragma directives have Java equivalents.
7
C++ to java code converter
There is no Java equivalent to 'sizeof': This statement must be rewritten. In the case of a
ratio of two sizeof calls, C++ to Java Converter can convert this to use the length field
of an array.
Unions are not supported in Java: Unions are not available in Java.
C++ to Java Converter modifies both the method declaration and calls to
methods with 'ref' parameters to simulate the pass by reference behavior of the
original C++ code. It does this by inserting a helper generic class 'RefObject' into
the converted code. The 'ref' parameter type is changed to a generic 'RefObject'
type and all calls to the method use a temporary variable of the 'RefObject' type
which gets re-assigned to the original argument after the method call.
C++ to Java Converter uses heuristics to remove most of the pointer and
address references from the converted code. However, we cannot guarantee that
you won't have post-conversion cleanup to perform, especially if the original C++
code was performing direct pointer manipulation.
8
C++ to java code converter
3.System Development:
Model Development:
Folder conversion: Entire folders of C++ code files are converted to Java code files.
Code snippet and file conversion: Our snippet conversion accuracy is outstanding and
does not require you to insert entire methods or classes. Heuristics are used to convert code
fragments with missing declarations.
Options: You can specify numerous conversion options via our Options dialog. Code
formatting options, global function replacements, type & member replacements, custom string
replacements, header file options, and miscellaneous options.
Handles C, C++, Managed C++, and C++/CLI: C++ to Java Converter converts from C,
native C++, Managed C++ (VC++ 2003), and C++/CLI (VC++ 2005 and above).
Use of 'helper classes' for C++ functions not directly convertible to Java: Some C++
functions, such as strtok and rand, do not have direct equivalents in Java. Also, Java has no
natural support for 'pass by reference' parameters. To ease the transition to Java, C++ to Java
Converter inserts helper classes in the converted code to provide the Java equivalent for these.
File comparison feature: After converting a folder, you can compare original and
converted files side-by-side. Comparing converted code to original code.
Running from the command line: C++ to Java Converter can be launched directly for a
specific folder or file conversion from the command line. Command line.
Most C/C++ library function calls: Some common C/C++ function calls are replaced, but
most are left unconverted and will not compile in the converted Java code. Also, MFC and ATL
resources are not converted.
See Common post-conversion adjustments for more items that are not converted.
9
C++ to java code converter
4.Performance analysis:
Placement of Braces: You can choose whether to place opening braces on the next
line after the construct header (default) or at the end of the same line. You can choose
whether to have braces vertically aligned with the construct (default) or indented.
Renaming token: C++ to Java Converter must occasionally rename class members
to avoid conflicts. You can choose your own renaming token and whether to place it at
the beginning or end of the original name
Indentation: You can choose whether the converted code uses spaces or tabs for
the leading white space. If you choose spaces for leading white space, then additional
lines of code inserted by the converter will use 'indentation length in spaces' when
additional indentation is required. If you choose tabs for leading white space, then
leading spaces in the original code of that length are replaced by tabs. Remaining
leading spaces less than that length will remain in the converted code.
Spacing: You can choose the degree to which C++ to Java Converter inserts spaces in
the converted code. By default, binary operators are padded with spaces and control
structures have a space inserted after the keyword, but you can switch off this behavior
or insert spaces in other situations.
C++ to Java Converter is primarily a syntax converter, ignoring most global C or C++
functions. However, you can specify your own custom replacements of global
function calls. These replacements will occur at the end of the conversion process.
You specify the new method name, the expected number of arguments, and whether
to transform the function call to an instance method call (using one of the arguments
as the instance variable), or a static method call (where you specify a class name in
addition to the new method name).
C++ to Java Converter allows you to specify your own custom replacements in the
converted Java code. This can be useful for cases where you wish to specify a
replacement for a C++ library data type that isn't handle by C++ to Java Converter.
For each replacement string you can specify the regex option, when to perform the
replacement (pre or post conversion), whether or not to match case, whether to match
whole words only, and whether comments or string literals should be modified. The
whole words and string literals options do not apply if the regex option is selected.
10
C++ to java code converter
The "Active" checkbox can be unchecked if you wish to temporarily bypass the
replacement.
For an example of the regex option, you could do a custom replacement of "Foo(x)"
with "Bar(x)", where "x" is any argument, by specifying the "Find" field as
"Foo(([\sa-zA-Z_$]*))" and the "Replace" field as "Bar$1". Note that there are
differences in various regex engines - the regex engine used by the converter is .NET
System.Text.RegularExpressions.Regex.
Additional C++ include folders: C++ to Java Converter will begin searching for
#include files in the last selected source code folder, but you can also specify additional
include base paths to search.
Header files missing from converted folder: By default, C++ to Java Converter
will prompt for the location of header files referenced in #include statements. If you
would prefer to have the converter ignore references to header files not found in the
current source code folder or in the 'Additional C++ include folders', you can check the
'Omit from conversion' option.
C++ to Java Converter allows you to specify your own custom type and member
replacements in the converted code. This can be useful for cases where you wish
to specify replacements for C++ library member calls of a specific type that are
not handled by C++ to Java Converter.
For each external class, you can specify an optional type replacement and multiple
member replacements. For each member replacement, you specify the original
member name, whether the member is static or not, and whether the member is a
method or a field. You also specify the number of arguments you expect. To
replace C++ operator invocations, specify the original method as an instance
method and use "operator+", "operator-", etc. for the original method name.
For the replacement member, you specify whether the replacement is static or not,
and whether the replacement is a method or a field. You also specify the new list
of arguments and can add literal arguments. For replacement of static methods
with instance members, you also specify the argument position which corresponds
11
C++ to java code converter
to the object instance for the new instance member. For replacement of instance
members with static members, you specify the new class name used to call the
static member (for static methods, you also specify the position in the argument
list to insert the previous instance qualifier, using the string "instance" (quotes not
included) at the appropriate position in the "new argument list" field). For
example, if you want to replace all instance calls to the 'Foo' type method 'Bar'
where you want the instance qualifier to be the new first argument, you would
specify "instance, a1" (quotes not included) for the "new argument list" field,
assuming that there was one argument in the original method calls.
Arrays
Java has single dimension arrays and jagged arrays. Classic C++ does not have
jagged arrays, but instead has rectangular multi-dimensional arrays. Since the C++
syntax for multi-dimensional arrays is identical to the Java syntax for jagged
arrays, and since C++ multi-dimensional arrays can be simulated with Java jagged
arrays, this array syntax can remain mostly unchanged (except that we follow the
standard of having the Java array specifier appear immediately following the type
in array declarations).
C++/CLI and Managed C++ (VC++ 2003) array syntax is also converted to Java
array syntax.
Since Java is a much simpler language than C++, there are naturally many features in C+
+ that have no Java equivalent. However, Java also lacks many common features of
modern programming languages; such as delegates, a preprocessor, value types, and
operator overloading.
Most C/C++ library function calls are left unconverted. C++ to Java Converter
converts a few that have direct equivalents, but most will remain in the converted code
and will require refactoring.
Some pointer and address manipulation may remain in the converted code.
There are no simple equivalents to C++/CLI delegates and events in Java, so these
must be refactored.
Operator overloading is not available in Java. These are marked with a comment.
Function pointers cannot be converted to Java since Java lacks true delegates.
12
C++ to java code converter
C++ templates are converted to, but are only roughly equivalent to, Java generics.
These will likely require further attention.
C++ typedef directives are processed as replacements (similar to the approach that a
C++ compiler might take). There is no elegant Java equivalent to the C++ typedef.
C++ constant style #define directives are replaced with constants defined in an inserted
DefineConstants class. C++ macro style #define directive references are replaced in-line
(similar to the way that a C++ compiler might perform its preprocessing step).
#define macros defined in alternative ways and typedef's defined in alternative ways
often cannot be converted to Java, since the state under which the definition was made
may not be the same as the state under which it is applied.
Note that since Java has no preprocessor, C++ code with preprocessor directives will
require refactoring to achieve a similar result in Java. You can avoid this if you choose to
target the NetBeans preprocessor .
Destructors
C++ to Java Converter converts C++ destructors to methods named Dispose. Since
C++ destructors execute immediately upon calling the delete operator on a variable, calls
to delete are converted to calls to the converted Dispose method. This best simulates the
same behavior as the original C++ code. If the type of the variable being operated on by
delete does not exist in the converted code, then the variable is just set to null rather than
calling Dispose.
The closest equivalent to C++ local static variables are Java class-level private
variables. C++ to Java Converter creates new class-level variables for each local static
variable found. The static local variables found in static methods are converted to class-
level static variables, while the static local variables found in instance methods are
converted to class-level instance variables.
C++ to Java Converter converts C++ templates to Java generics. Since Java generics
are only roughly equivalent to C++ templates, you will have adjustments or refactoring to
perform after the conversion.
C++/CLI generics are directly and accurately converted to their very close Java
equivalents.
13
C++ to java code converter
Declaring Variables
Java
C / C++ Java C/C++ explanation
explanation
int i; int i; Declare an integer Declare an integer
Declare a long
Declare a long (allows bigger numbers
long l; long i; than an int usually)
(allows bigger
numbers than an int)
double d; double d; Declare a double Declare a double
char c; char c; Declare a char (one character string). Declare a char
char mystring[100]; String Declare string of 100 chars big (but use the Declare a string.
mystring malloc method instead for super-large Java handles all the
arrays). This is the way to get a string in c. difficult work at the
14
C++ to java code converter
cost of speed.
String[]
char array2d[20] stringArray = Declare 20 strings of 100 length (but use
the malloc method instead for super-large Declare 20 strings.
[100]; new arrays or if you want to pass to a function)
String[20];
Create a new 'structure' to contain sub-
variables (similar to an object in an object-
orientated language like Java). In this Java doesn't support
struct newtype { int example, each variable of type "newtype" structs. Use
--- will contain the subvariables 'o' (an classes/objects
o; char p[5]; };
integer), and 'p' (an array of chars). All instead.
structs must be above (and outside) any
functions.
Using the struct created above, declare a
newtype ntype; --- variable of type newtype. This goes into a ---
function (eg. main () ).
C/C++
Java
C / C++ Java explanatio
explanation
n
System.out.print("Hello\nWorld Prints stuff Prints stuff ("\n"
printf("Hello\nWorld"); ("\n" means
means newline)
"); newline)
Prints char
array
printf("%s",mystring); System.out.print(mystring); (mystring Prints string
var), eg., a
string.
Print a char
Print a char from
from the char
printf("%c",c); System.out.print(c); declared
the char declared
above
above
Print a char
Print a char
followed by
followed by
printf("%c hello System.out.print(c+" "hello",
"hello", followed
%s",c,mystring); hello"+mystring); followed by
by the mystring
the mystring
variable.
variable.
Print last char
from the Print last char from
System.out.print(mystring.charAt(99))
printf("%c",mystring[99]); ;
string the string declared
declared above
above
printf("%s",array2d[19]); System.out.print(array2d[19]); Print last Print last string
string from from the string
the string array (declared
15
C++ to java code converter
array
(declared
above under title
above under
"Declaring
title
Variables").
"Declaring
Variables").
Print last char
Print last char from
printf("%c",array2d[19] System.out.print( array2d[19].charAt( from the last
the last string in
[99]); 99) ); string in the
the string array.
string array.
Change last
character in No direct char
mystring[99] = 'z' --- mystring editing in string
string to 'z'
Obvious, but
illegal! You
Unlike in C, this is
can't directly
fine, and just
assign a string
mystring = "hello" mystring = "hello" to a char
assigns "hello" to
the mystring
array. Use
String.
strcpy
instead.
Change last
character in No direct char
array2d[19][99] = 'y' --- last string to editing in a string
'y'
Copy "test" to
the
Assigns word
"mystring"
strcpy(mystring, "hello"); mystring = "hello" string as
"hello" to the
mystring String.
declared
earlier.
Copy "test" to
Copy "test" to the
the 5th string
5th string in
strcpy(array2d[5], "hello"); stringArray[5] = "hello" in "array2d"
"stringArray" as
as declared
declared earlier.
earlier.
Use strcpy
instead
Use substring
usually. This
strncpy(array2d[5], "hello", stringArray[5] = (truncate) to define
is used if you
5); "hello".substring(0, 5) a limited section of
want to define
the string.
the number of
chars to copy.
Takes a
substring of
mystring Takes a substring
(start from of mystring (start
strncpy(array2d[5], stringArray[5] = 10th char and from 10th char and
mystring+10, 50); mystring.substring(10, 60) do 50 of do 50 of them),
them), and and puts it into the
puts it into array2d[5] string.
the array2d[5]
string.
strcat(mystring,"hello"); mystring = mystring + "hello" Concatenate Concatenate
(add/append) (add/append) "test"
16
C++ to java code converter
to the string.
"test" to the However, for large
string, along amounts of
with the concatenations, use
escape code Java's
'\0' to signify StringBuffer()
the end method, as it is
much quicker.
Compare two Compare two
strings. strings. Returns
Returns zero zero if same, Less
strcmp(a,b); a.compareToIgnoreCase(b) if same, -1 if than zero if a is
a is less than less than b, and
b, and 1 if a is more than zero if a
more than b. is more than b.
Case sensitive
Case sensitive
strcmpi(a,b); a.compareTo(b) version of
version of above
above
Number of
chars so far
stored in the Number of chars
strlen(mystring); mystring.length() string so far stored in the
(everything string.
up to escape
code '\0')
Convert char
to int, Convert char to int,
providing the providing the char
int i = '7'-48; int i = '7'-48; char is a is a number from
number from 0-9.
0-9.
Convert char Convert char to int
int i = 'a'; int i = 'a'; to int code code
Convert int to
Convert int to char,
char,
providing the
char c = 7 +48; char c = 7 +48; providing the
number is from 0-
number is
9.
from 0-9.
Convert int
code to char
Convert int code to
('a' in this
char ('a' in this
case). If you
case). If you don't
don't want to
char c = 97; char c = 97; declare a new
want to declare a
new char, then use
char, then use
the wrapper type
the wrapper
"(char)" next to 97.
type "(char)"
next to 97.
int i = atoi("456"); int i = Integer.parseInt("456") Convert Convert string to
string to int. int. Use
Use 'atof' to Double.parseDoubl
convert string e to convert string
to to double, and
double/float, Long.parseLong
and 'atol' for for converting
converting string to long.
17
C++ to java code converter
string to long.
Math functions:
C / C++ C/C++
Java Java explanation
explanation
Calculate the Calculate the
absolute value (turns absolute value (turns
int i = abs(-20); int i = Math.abs(-20); negative numbers to negative numbers to
positive. positive.
double d = Calculate 3 to the Calculate 3 to the
double d = Math.pow(3, 7.5); power of 7.5 power of 7.5
pow(3.0, 7.5);
Calculate the sine of Calculate the sine of
double d = 50 (in radians). Use 50 (in radians). Use
double d = Math.sin(50); 50*pi/180 instead to 50*pi/180 instead to
sin(50.0);
get degrees. get degrees.
Calculate the sine of Calculate the sine of
double d = 50 (in radians). Use 50 (in radians). Use
double d = Math.cos(50); 50*pi/180 instead to 50*pi/180 instead to
cos(50.0);
get degrees. get degrees.
double d = Calculate the square Calculate the square
double d = Math.sqrt(50); root root
sqrt(50.0);
double d =
double d = Math.tan(50); Calculate the tangent Calculate the tangent
tan(50.0);
double d = Calculate the inverse Calculate the inverse
double d = Math.atan(50); tangent tangent
atan(50.0);
Calculate the base 2 Calculate the base 2
double d = double d =
logarithm of 32 logarithm of 32
log(32.0)/log(2.0); Math.log(32.0)/Math.log(2.0); (which is 5). (which is 5).
5.REQUIREMENT ANALYSIS
18
C++ to java code converter
The project named “C++ to java code converter ” is specially designed for
conversion of code. This software provides fascility to convert any code frome c++ to
java.
Most often problem using C++ is Security of confidential data. This software
provides a significant threat to information security and confidentiality. Our project
mainly focuses on conversion of codes, forms and syntaxes from C++ to java.
Main requirement of project:
Automatic code generation
Optimal utilization of resources
Quick execution
19
C++ to java code converter
• Conversion of code.
• Keep the record of converted code.
• Can execute the converted java code.
APPLICATIONS:
Our project is commercial and so need not for any sponsorship. Generally helpful
in Education Institutes, Software Companies, IT Industries, Research Departments ,Small
to Mid-size Organizations ,Corporate Organizations etc. This project helps Administrator
or any person to keep track of programming attachments on there PC’s.
20
C++ to java code converter
Chapter 6
SYSTEM DESIGN
6.1 Architectural Diagram
Generate
output
Compile
java code
output
21
C++ to java code converter
22
C++ to java code converter
23
C++ to java code converter
User editor
Java code
Conv-
rter
Db operati-
on
Open file
24
C++ to java code converter
UML DIAGRAM:
25
C++ to java code converter
26
C++ to java code converter
start
Read
character
Check is it ;,space,\n
false Store
character
true
Write into
file
27
C++ to java code converter
Chapter 7
CONCLUSION
The following conclusion and scope can be derived the following key Benefits
from our project.
Key Benefits
The task of converting C++ to Java just got a lot easier. C++ to Java Converter will save
you countless hours of painstaking work. The Java code produced is superior to the code
produced by any other C++ to Java converter.
28
C++ to java code converter
REFERENCES:-
WEB
1) www.wikipaedia.com
2) www.java.sun.com
3) www.crazyengineer.com
4)http://codeguru.org
Appendices:-
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication8;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
29
C++ to java code converter
import java.io.DataInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.swing.JOptionPane;
/**
*
* @author Bharat
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
30
C++ to java code converter
jMenuItem1.setText("jMenuItem1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 255, 0));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jTextArea1.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(204, 0, 0)));
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jTextArea2.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(204, 0, 0)));
jScrollPane2.setViewportView(jTextArea2);
31
C++ to java code converter
32
C++ to java code converter
jButton4ActionPerformed(evt);
}
});
setJMenuBar(jMenuBar1);
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 369,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE,
143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,
143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE,
143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
33
C++ to java code converter
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, 143,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE,
142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 8, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE,
false)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE,
40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
371, Short.MAX_VALUE)
34
C++ to java code converter
.addComponent(jScrollPane2))
.addGap(0, 20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
FileOutputStream fout;
try {
fout = new FileOutputStream("c:\\test1.txt");
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fout));
br.write(jTextArea1.getText());
br.close();
} catch (Exception ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
35
C++ to java code converter
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("#include<math.h>")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("#include<iostream.h>")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
jTextArea2.append(str);
flag = true;
}
36
C++ to java code converter
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("#include<stdio.h>")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("using namespace std;")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("getch();")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
37
C++ to java code converter
flag = true;
}
if ((i = str.indexOf("cout<<")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf("};")) > -1) {
// StringBuffer sb=new StringBuffer(str);
38
C++ to java code converter
jTextArea2.append(str);
flag = true;
}
if ((i = str.indexOf(": public ")) > -1) {
// StringBuffer sb=new StringBuffer(str);
jTextArea2.append(str);
flag = true;
}
if (flag == false) {
jTextArea2.append(str);
}
jTextArea2.append("\n");
flag = false;
}
br.close();
String st = jTextArea2.getText();
39
C++ to java code converter
i2 = s.indexOf("{", i1 + 1);
i3 = s.lastIndexOf("}");
String temp = s.substring(i2 + 1, i3);
System.out.println(temp);
s = sb.toString();
s = s.replaceAll("cout<<", "System.out.println(");
s = s.replaceAll("cin>>","system.in(");
System.out.println(s);
finals += "}\n}";
System.out.println(finals);
jTextArea2.append(finals);
40
C++ to java code converter
try {
String s="";
while((s=br.readLine())!=null)
{
jTextArea1.append(s);
jTextArea1.append("\n");
}
br.close();
} catch (Exception e) {
}
}
41
C++ to java code converter
try {
FileOutputStream fout = new FileOutputStream("C:\\Program
Files\\Java\\jdk1.6.0\\bin\\"+s1);
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(fout));
bout.write(s);
bout.close();
JOptionPane.showMessageDialog(rootPane, "file Saved");
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "" +e);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional)
">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
42
C++ to java code converter
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
}
//</editor-fold>
43
C++ to java code converter
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
44