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) );
}
}
}
Re: Decimal Bianry Converter - Certain Expectation for assignment
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.
Re: Decimal Bianry Converter - Certain Expectation for assignment
Hello biddum1!
Code :
ArrayStack<Integer> s = new ArrayStack<Integer>(n);
Do you have an ArrayStack class? If yes, can you post it?
Code :
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