8 Replies - 1480 Views - Last Post: 09 September 2013 - 03:42 PM Rate Topic: -----

#1 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Making Binary Conversion class

Posted 07 September 2013 - 09:11 PM

This code does not output any usable data can someone give any suggestions.

public class BinaryConverter {

	// instance Variable of binary data

	private double binaryNumber;
	private int row;

	// Constructor :: Constructor

	public BinaryConverter(double number) {

		binaryNumber = number;

	}

	// Array Row & Column Setters

	public void setRow(int row) {
		this.row = row;
	}

	public int getRow() {
		return row;
	}

	public int sizeOf(int number) {
		int count = 0;
		while (number / 10 > 0) {
			count++;
		}
		return count;
	}

	public int binaryConversion(int binaryNumber) {

		int binary[] = new int[row];
		int powerOperator = 0;
		int sumOperator = 0;

		int mod = 0;
		for (int j = 0; j < binary.length; j++, mod += 10) {
			// System.out.print(j);

			binary[j] = binaryNumber % mod;
			binaryNumber /= 10;

		}

		for (int i = 0; i < binary.length; i++) {

			int power = 0;

			powerOperator = (int) (binary[i] * Math.pow(2, power));
			sumOperator += powerOperator;
			power++;

		}

		return sumOperator;
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Making Binary Conversion class

#2 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: Making Binary Conversion class

Posted 07 September 2013 - 09:19 PM

just realized this is the wrong section to post code
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Making Binary Conversion class

Posted 07 September 2013 - 09:27 PM

Moved to Java.

Just an FYI- the tutorials sections are the wrong place to ask for help as well. The Java Tutorials forum is for posting tutorials.

Your problem is that the code doesn't have a main() method, you don't instantiate the class, and you don't even use it. Is this your code even?
Was This Post Helpful? 0
  • +
  • -

#4 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: Making Binary Conversion class

Posted 07 September 2013 - 09:36 PM

yes it is my code and I do realize there is no main method. But i would like to apologize for the inconvience of my tired eyes i've been typing this class for awhile. The programs intended purpose is taking a binary number lets say 00001001 and change it to a base 10 number.would you like to see the main method?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Making Binary Conversion class

Posted 07 September 2013 - 09:37 PM

That would probably be helpful, as well as a specific description of your problems or errors.
Was This Post Helpful? 0
  • +
  • -

#6 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: Making Binary Conversion class

Posted 07 September 2013 - 09:47 PM

The problem is when I type in lets say 01010010
The computer does not read in the number. My method sizeOf(binarynumber) within my BinaryConversion class is not able to count the amount of digits there are in the number. This causes the columns in my array no opportunity to initialize to either a 0 or 1. Then the program does not terminate at all. Although i just realized i should remove line 45 of the class BinaryCoversion. Other then that I do not see any other errors in my program. But I am still new to programming so any advice would be helpful. If you wish to delete this topic all together that's fine with me too I guess i'll be able to figure it out myself eventually.

public class TestBinaryConverter {

	
	public static void main(String[] args) {
		int binaryNumber = 0;
		
		Scanner wiz = new Scanner (System.in);
		
		System.out.print("Welcome to Binary Converter v.1.0\n");
		System.out.print("Enter your Binary Integers: ");
		
		binaryNumber = wiz.nextInt();
		
		
		BinaryConverter binary1 = new BinaryConverter(binaryNumber);
		
		int numberOfInts = binary1.sizeOf(binaryNumber);
		System.out.println(numberOfInts);
		
		System.out.print(binaryNumber + "\n");
		binary1.setRow(numberOfInts);
		System.out.print(binary1.getRow());
		
		int decimalNumber = binary1.binaryConversion(binaryNumber);
		
		
		System.out.print("Your Decimal Number is " + decimalNumber);
		
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#7 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Making Binary Conversion class

Posted 08 September 2013 - 12:26 AM

You can use the Integer.parseInt method to parse strings into numbers, like so
Integer.parseInt(string input, int base)
Integer.parseInt("01010010", 2)
And you can get binary representation of a number like so
Integer.toString(int input, int base)
Integer.toString(82, 2)


The computer already stores numbers in binary, if you try to change it to superficially resemble a binary number when seen in decimal, you'll end up changing the value of the number, not to mention that even a 64 bit integer could only represent 20 binary digits.

This post has been edited by Witchking: 08 September 2013 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,444
  • Joined: 20-September 08

Re: Making Binary Conversion class

Posted 08 September 2013 - 02:57 AM

Quote

You can use the Integer.parseInt method to parse strings into numbers
I would guess the classwork spec would forbid that
Was This Post Helpful? 0
  • +
  • -

#9 bruins301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-November 12

Re: Making Binary Conversion class

Posted 09 September 2013 - 03:42 PM

I fixed my problem it turns out the class never let my binary number exit the loop in the sizeOf() method. My power variable in binaryConversion also kept initializing to zero everytime the loop ran. Other than that my code was good this is my new code. Anyone is welcome to use this code if they wish. :bananaman: :gun_bandana:
public class BinaryConverter {

	// instance Variable of binary data

	private int binaryNumber;
	private int row;

	// Constructor :: Constructor

	public BinaryConverter(int number) {

		binaryNumber = number;

	}

	// Array Row & Column Setters

	public void setRow(int row) {
		this.row = row;
	}


	public int sizeOf(int number) {
		int count = 0;

		while (number > 0) {
			count++;
			number /= 10;
		}
		return count;
	}

	public int binaryConversion(int binaryNumber) {

		int binary[] = new int[row];
		int powerOperator = 0;
		int sumOperator = 0;

		int mod = 10;
		for (int j = 0; j < binary.length; j++) {
			// System.out.print(j);

			binary[j] = binaryNumber % mod;
			System.out.println("binary array : " + binary[j]);
			binaryNumber /= 10;
			

		}

		int power = 0;
		for (int i = 0; i < binary.length; i++) {

			
			powerOperator = (int) (binary[i] * Math.pow(2, power));
			sumOperator += powerOperator;
			power++;

		}

		return sumOperator;
	}

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1