C++ to Java Conversion Help!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2081 Views - Last Post: 11 March 2012 - 12:11 AM Rate Topic: -----

#1 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

C++ to Java Conversion Help!

Posted 09 March 2012 - 08:47 PM

Can someone please help me translate this c++ code to Java. Below the C++ code, is my Java code that I have so far. Thanks in advance :-)
C++ code:
 
    #include <string>  
    #include <iostream>  
    using namespace std;  
      
    string numberToBarcode(int);  
      
    int main() {  
      
        int zip;  
        int num1, num2, num3, num4, num5, checkNum;  
        int tempNum;  
        int checkTotal;  
        string barcode = "|";   // initialized with a front framing bar  
      
        cout << "Please enter a 5 digit zip code.\n"  
            << " --> ";  
        cin >> zip; // prompt for zip code  
      
        // check if valid zip code  
        if (zip >= 100000 || zip < 0) {  
            cout << "Error: Not a zip code.\n";  
            return 0;  
        }  
      
        tempNum = zip;  
        //parsing each digit to determine check value  
        // the parsing is done in reverse order, so the digits are stored in reverse order  
        num5 = tempNum % 10;  
        tempNum = tempNum / 10;  
        num4 = tempNum % 10;  
        tempNum = tempNum / 10;  
        num3 = tempNum % 10;  
        tempNum = tempNum / 10;  
        num2 = tempNum % 10;  
        tempNum = tempNum / 10;  
        num1 = tempNum % 10;  
        tempNum = tempNum / 10;  
      
        // generate check num  
        checkTotal = num1 + num2 + num3 + num4 + num5;  
        checkNum = (10-(checkTotal % 10))%10;   // the last %10 is to deal with cases where checkTotal%10 == 0  
      
        // turn the zip into a barcode, digit by digit  
        barcode += numberToBarcode(num1);  
        barcode += numberToBarcode(num2);  
        barcode += numberToBarcode(num3);  
        barcode += numberToBarcode(num4);  
        barcode += numberToBarcode(num5);  
      
        barcode += numberToBarcode(checkNum);  
      
        // final framing bar  
        barcode += "|";  
      
        cout << "\nYour zip code's barcode is:\n"  
            << " " << barcode << "\n";  
      
        return 0;  
    }  
      
    string numberToBarcode(int num) {  
      
        string barcode = "";  
        int dig;    // the single digit being converted  
        int tempNum;    // temporary variable  
        int /*bc7=0, bc4=0, bc2=0, bc1=0, bc0=0,*/ bcTotal=0;  
      
        tempNum = num;  
      
        if(tempNum >= 10) {  
            // if the digit is actually multiple digits, this function will  
            // recursively determine the multiple barcode sections they  
            // represent and return them together  
      
            // this strips the last digit...  
            dig = tempNum%10;  
            tempNum /= 10;  
            // then recursively strips each digit and adds their barcodes together  
            barcode = numberToBarcode(tempNum);  
        } else {  
            dig = tempNum;  
        }  
      
        // reusing the temp variable  
        tempNum = dig;  
      
        if (dig == 0) { // 0 is a special case  
            barcode += "||:::";  
        } else {    // otherwise...  
            if (tempNum / 7 == 1 && bcTotal < 2) {   // check for 1st bar - 7s digit  
                barcode += "|"; // if true, full bar  
                tempNum -= 7;  
                bcTotal++;  
            } else  
                barcode += ":"; // if false, half bar  
            if (tempNum / 4 == 1 && bcTotal < 2) {   // check for 2nd bar - 4s digit  
                barcode += "|";  
                tempNum -= 4;  
                bcTotal++;  
            } else  
                barcode += ":";  
            if (tempNum / 2 == 1 && bcTotal < 2) {   // check for 3rd bar - 2s digit  
                barcode += "|";  
                tempNum -= 2;  
                bcTotal++;  
            } else  
                barcode += ":";  
            if (tempNum / 1 == 1 && bcTotal < 2) {   // check for 4th bar - 1s digit  
                barcode += "|";  
                tempNum -= 1;  
                bcTotal++;  
            } else  
                barcode += ":";  
            if (bcTotal < 2) {   // check for last bar - 0s digit - This one just takes up slack  
                barcode += "|";  
                bcTotal++;  
            } else  
                barcode += ":";  
        }  
      
    /* 
        if (dig == 0) { // 0 is a special case 
            bc7 = 1; 
            bc4 = 1; 
            bcTotal = 2; 
        } else {    // otherwise... 
            if (tempNum / 7 == 1 && bcTotal < 2) {   // check for 1st bar - 7s digit 
                bc7 = 1; 
                tempNum -= 7; 
                bcTotal++; 
            } 
            if (tempNum / 4 == 1 && bcTotal < 2) {   // check for 2nd bar - 4s digit 
                bc4 = 1; 
                tempNum -= 4; 
                bcTotal++; 
            } 
            if (tempNum / 2 == 1 && bcTotal < 2) {   // check for 3rd bar - 2s digit 
                bc2 = 1; 
                tempNum -= 2; 
                bcTotal++; 
            } 
            if (tempNum / 1 == 1 && bcTotal < 2) {   // check for 4th bar - 1s digit 
                bc1 = 1; 
                tempNum -= 1; 
                bcTotal++; 
            } 
            if (bcTotal < 2) {   // check for last bar - 0s digit - This one just takes up slack 
                bc0 = 1; 
                bcTotal++; 
            } 
        } 
    */    
      
        return barcode;  
    }  
    </iostream></string>  


Java Code:
import java.util.Scanner;

public class zipBar {
	String numberToBarcode(int n1){
		return null;
	}
	public static void main(String[] args) {
		int z;
		int n1, n2, n3, n4, n5, cN;
		int tN;
		int cT;
		String barcode = "|";
		
		System.out.println("Enter zip code: ");
		Scanner zip = new Scanner(System.in);
		z = zip.nextInt(); 
		
		if (z >= 10000 || z < 0)
		{
			System.out.println("Input Error: Input not a valid zip code");
		
		}
		tN = z;
		
		n5 = tN % 10;
		tN = tN / 10;
		
		n4 = tN % 10;
		tN = tN / 10;
		
		n3 = tN % 10;
		tN = tN / 10;
		
		n2 = tN % 10;
		tN = tN / 10;
		
		n1 = tN % 10;
		tN = tN / 10;
		
		cT = n1 + n2 + n3 + n4 +5;
		cN = (10-(cT % 10)) % 10;
		
		barcode += numberToBarcode(n1);
		
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ to Java Conversion Help!

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 08:54 PM

Why did you use different variable names in Java and not using C++ ones. It make everything more confusing.
Why complicating your life soo much ?
Redo your exercise using the same variable names and I'll be glad to check/validate
Was This Post Helpful? 0
  • +
  • -

#3 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:00 PM

View Postpbl, on 09 March 2012 - 08:54 PM, said:

Why did you use different variable names in Java and not using C++ ones. It make everything more confusing.
Why complicating your life soo much ?
Redo your exercise using the same variable names and I'll be glad to check/validate

import java.util.Scanner;

public class zipBar {
	String numberToBarcode(int n1){
		return null;
	}
	public static void main(String[] args) {
		int z;
		int num1, num2, num3, num4, num5, checkNum;
		int tempNum;
		int checkTotal;
		String barcode = "|";
		
		System.out.println("Enter zip code: ");
		Scanner zip = new Scanner(System.in);
		z = zip.nextInt(); 
		
		if (z >= 10000 || z < 0)
		{
			System.out.println("Input Error: Input not a valid zip code");
		
		}
		tempNum = zip;
		
		num5 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num4 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num3 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num2 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num1 = tempNum % 10;
		tempNum = tempNum / 10;
		
		checkTotal = num1 + num2 + num3 + num4 + num5;
		checkNum = (10-(checkTotal % 10)) % 10;
		
		barcode += numberToBarcode(num1);

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:09 PM

Your Java code does not even compile. Is it what you have a problem with ?

tempNum = zip;

may be

tempNum = zip.nextInt();
Was This Post Helpful? 0
  • +
  • -

#5 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:13 PM

View Postpbl, on 09 March 2012 - 09:09 PM, said:

Your Java code does not even compile. Is it what you have a problem with ?

tempNum = zip;

may be

tempNum = zip.nextInt();


Im mainly having problems with this part:
string numberToBarcode(int);  

barcode += numberToBarcode(num1); 
045	    barcode += numberToBarcode(num2); 
046	    barcode += numberToBarcode(num3); 
047	    barcode += numberToBarcode(num4); 
048	    barcode += numberToBarcode(num5); 
049	   
050	    barcode += numberToBarcode(checkNum);  


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:22 PM

Which type of problem ?
As your code won't even compile it would be a compilation problem
Is your problem in the method invocation or in the methnod itself ?
I hate it when, to fix someone problem we have, to beg for informations
Was This Post Helpful? 0
  • +
  • -

#7 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:29 PM

View Postpbl, on 09 March 2012 - 09:22 PM, said:

Which type of problem ?
As your code won't even compile it would be a compilation problem
Is your problem in the method invocation or in the methnod itself ?
I hate it when, to fix someone problem we have, to beg for informations


I'm sorry... I am not sure why you are upset. I just need help translated the code into Java that's it. The specific part I am having problems with above is the exact part that I am stuck on. I am not sure how to translate that into Java, because I believe that part in the C++ code is using a custom function called numberToBarcode(), and in Java i've read you cannot create custom functions but theyre methods instead. I need helping turning that function into a method so i can continue with the rest of the code.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:34 PM

I am not upset. A bit frustated :)
As your code does not even compile, don't you think it would be a good idea to show us these compile error(s) ?
Was This Post Helpful? 0
  • +
  • -

#9 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 09:42 PM

I do not have any errors because I have not finished writing the code, that's why I need help, Im unsure what next to put because I do not know how to translate the part into Java.

C++ code I am stuck on:
barcode += numberToBarcode(num1);  
    barcode += numberToBarcode(num2);  
    barcode += numberToBarcode(num3);  
    barcode += numberToBarcode(num4);  
    barcode += numberToBarcode(num5);  
  
    barcode += numberToBarcode(checkNum);  


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 10:52 PM

that should work, without any modification in Java
Was This Post Helpful? 0
  • +
  • -

#11 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 11:17 PM

View Postpbl, on 09 March 2012 - 10:52 PM, said:

that should work, without any modification in Java


How though? Java does not know what "string numberToBarcode(int); " is?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 11:21 PM

and C++ does have a standard libray function named: numberToBarcode(int) ?
Was This Post Helpful? 1
  • +
  • -

#13 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 09 March 2012 - 11:36 PM

View Postpbl, on 09 March 2012 - 11:21 PM, said:

and C++ does have a standard libray function named: numberToBarcode(int) ?


c'mon man theres no need for sarcasm, im fairly new to java and i've never done c++. I can see here "string numberToBarcode(int); " that that's where the function is created, but in thought that in java you cannot do that (create a custom function) but instead make a method. That is what I attempted to do above, it is incorrect, that is where I need help. Please.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 08:10 PM

What I am saying is that your C++ code must have a function named numberToBarcode(int)
that you will also have to translate to Java
Was This Post Helpful? 0
  • +
  • -

#15 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 08:26 PM

To change your C++ function over to Java, I would basically copy it all into your Java class, below your main method.

Then I would convert
string numberToBarcode(int num)
{ ... Do something }



into
public static String numberToBarcode(int num)
{...Do something}



I very quickly looked over the C++ function. So I would just see what other errors there are from that and fix them.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2