c++ convert to java

converting c++ to java

Page 1 of 1

2 Replies - 16464 Views - Last Post: 28 July 2008 - 04:29 AM Rate Topic: *---- 1 Votes

#1 dms02x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-February 08

c++ convert to java

Posted 27 July 2008 - 10:20 PM

i am in c++ and i was wondering if u guys can convert this program to java
i just wanna know the difference between the 2 witch is better or easier...

TY :)


int cycle(int m)
/* return number of elements in the 3n+1 cycle for m */	 
{
  int i = 1;

  while (m != 1){
	if (m % 2 == 0){
	  m = m/2;
	}
	else{
	  m = 3*m+1;
	}
	i++;
  }
  return i;
}


int main()
{
  int m,n,max,temp;
  int i;

  while (scanf("%d %d\n",&m,&n)==2){
	
	printf("%d %d ",m,n);

	/* swap if out of order */
	if (m > n){
	  temp = m;
	  m = n;
	  n = temp;
	}
	
	max = cycle(m);
	for(i=m+1;i<=n;i++) {
	  temp = cycle(i);
	  if (temp > max)
	max = temp;
	}
	printf("%d\n",max);
  }

  return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: c++ convert to java

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4312
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Re: c++ convert to java

Posted 27 July 2008 - 11:11 PM

This isn't exactly equivalent but it is pretty damn close.

import java.io.*;
import java.util.StringTokenizer;


public class cycle {
	public static void main(String args[]) {
		int m,n,max,temp;
		int i;

		String input = "";
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   
		try {
			while ((input = in.readLine()) != null){
				StringTokenizer st = new StringTokenizer(input);
				if (st.countTokens() == 2) {
					m = Integer.parseInt(st.nextToken());
					n = Integer.parseInt(st.nextToken());
				

					System.out.println(m + " " + n);
					//printf("%d %d ",m,n);

					/* swap if out of order */
					if (m > n){
						temp = m;
						m = n;
						n = temp;
					}
		    
					max = cycle(m);
					for(i=m+1;i<=n;i++) {
						temp = cycle(i);
						if (temp > max)
							max = temp;
					}
					//printf("%d\n",max);
					System.out.println(max);
				}
			}
		}
		catch (IOException e) { }
	
	}
	
	private static int cycle(int m) {
		int i = 1;

		while (m != 1){
			if (m % 2 == 0){
				m = m/2;
			}
			else{
				m = 3*m+1;
			}
			i++;
		}
		return i;
	}
}



Keep in mind that Java came out of C++ so most of the code is interchangeable. The only part that is not absolutely exact is that Java doesn't include a scanf, so here we have setup a bufferedreader to read the input, break the string, and parse them into integers for use with the rest of the program.

Both programs will run forever due to the way you have setup the code.

You might find more useful info if you had just asked which is better. Length of code or code differences isn't always going to give you the most realistic comparison of which is better. Especially between two similar languages like this. C++ and Java are about equivalent and depends on your problem and comfort level as to which is better for you.

Enjoy!

"At DIC we be language comparing code ninjas... we also compare apples and oranges a lot." :snap:
Was This Post Helpful? 1
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: c++ convert to java

Posted 28 July 2008 - 04:29 AM

You can use some tools to convert from C/C++ to java but I think you will not like the result.
C2J Converter
Jazillian
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1