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) );
}
}
}