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

# Thread: Decimal Bianry Converter - Certain Expectation for assignment

1. ## Decimal Bianry Converter - Certain Expectation for assignment

Hello, I am stuck on this assignment:
Write a DBConverter class that has a single public static void printInBin (int n) method that implements the following algorithm: (4)

1. Let S be an empty stack of the type Integer.
2. While n is greater than 0
a. Rem = n % 2
b. Push Rem into stack S
c. n = n / 2
3. While S is not empty
a. digit = S.pop()
b. Print digit

Write a DBConverterTester that creates this array:

Integer[] numbers = { new Integer(23), new Integer(47), new Integer(257),
new Integer(1023), new Integer(0), new Integer(82), new Integer(512),
new Integer(100)};

and prints each number and then calls the printInBin method for it..

MY SOLUTION (WHAT IS WRONG):
package stack;
public class DBConverter{

public static void printInBin(int n)
{
ArrayStack<Integer> s = new ArrayStack<Integer>(n);

while( n > 0)
{
int rem = n % 2;
s.push(rem);
n = n / 2;

}
while ( n <= 0)
{
int digit = s.pop();
System.out.println(digit);
}

}

public static void main(String[] args)
{

Integer[] numbers = { new Integer(23), new Integer(47), new Integer(257),
new Integer(1023), new Integer(0), new Integer(82), new Integer(512),
new Integer(100)};

for(int i = 0; i < numbers.length; i++)
{
System.out.println(numbers.printInBin(i) );
}

}
}

2. ## Re: Decimal Bianry Converter - Certain Expectation for assignment

WHAT IS WRONG
Perhaps *you* could say what's wrong. Specifically, does that code compile? If not, and you can't understand the compiler's message, post it and say which lines of your code it's referring to. I'm sure someone can explain what it means.

3. ## Re: Decimal Bianry Converter - Certain Expectation for assignment

Hello biddum1!
`ArrayStack<Integer> s = new ArrayStack<Integer>(n);`
Do you have an ArrayStack class? If yes, can you post it?
```for(int i = 0; i < numbers.length; i++)
{
System.out.println(numbers.printInBin(i) );
}```
The appropriate way to call a static method is ClassName.methodName