Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Convert C++ to Java ?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Convert C++ to Java ?

    Hello,
    The teacher asked us to convert this C++ program to java. I only know how to write a java program
    so could you please tell me what is the concept of this C++ program so I can convert it
    // function finds largest element in array
    #include <iostream>
    using namespace std;
     
    int maxint(int[], int);            //declaration of an array
    //--------------------------------------------------------------
    int main()
       {
       const int SIZE = 100;           //maximum array elements
       int intarray[SIZE];             //array of ints
       int count = 0;                  //number of ints in array
       int intval;
     
       while(true)
          {
          cout << count 		//cout is used to print like System.out
               << ". Enter an integer value (0 to end): ";
          cin >> intval; 		//cin is used to get inputs in a variable
          if(intval==0) break;	//if-condition
          intarray[count++] = intval;  //set array element to value
          }
                                       //find largest element
       int bigdex = maxint(intarray, count);
       cout << "\nLargest element is number " << bigdex;
       cout << ", which contains " << intarray[bigdex] << endl;
       return 0;
       }
    //--------------------------------------------------------------
    int maxint(int ia[], int c)        //returns largest element
       {
       int bigdex = 0;                 //assume it's element 0
       for(int j=1; j<c; j++)          //if another is larger,
          if( ia[j] > ia[bigdex] )     //put its index in bigdex
             bigdex = j;
       return bigdex;
       }
    Thank you ^^


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert C++ to Java ?

    Hmm, there's only one part of that C++ program I can see you having troubles with:
    cout is the way C++ programs print to the console. This is done using the << operator.
    So, for example:
    cout << "hello world!";
    is equivalent in Java as:
    System.out.print("hello world!");

    endl is a simple way to print out a newline.
    So:
    int age = 5;
    cout << "hello world! I am " << age << " years old." << endl;
    would be the same as:
    int age = 5;
    System.out.println("hello world! I am " + age + " years old.");

    cin is the way C++ programs read input from the console. This is done using the >> operator.

    Also, you can completely ignore the first two lines of the program (they just include the iostream library and use the standard namespace, which are irrelevant in Java). Remember that in Java you must wrap everything in a class, as well as declare certain functions to be static. Lastly, Java programs declare main to return void while in C++ the main function generally returns an int.

Similar Threads

  1. How do you convert a jar file into a java file, how ?
    By auto78900 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2010, 12:32 AM
  2. Convert C/C++ code to Java
    By cristianll in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 14th, 2009, 02:43 AM
  3. Convert from 'C' to "java" (switch)
    By chronoz13 in forum Loops & Control Statements
    Replies: 7
    Last Post: October 6th, 2009, 08:20 PM
  4. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  5. convert GSM to PCM (wav)
    By cilang in forum Java Theory & Questions
    Replies: 4
    Last Post: August 7th, 2009, 03:46 AM