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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Decimal Bianry Converter - Certain Expectation for assignment

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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

Similar Threads

  1. GUI temp converter program troubles
    By copelandtml in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 14th, 2011, 10:28 AM
  2. Military time converter
    By ebone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 02:56 AM
  3. J2ME converter program
    By sackling in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 6th, 2010, 09:13 AM
  4. phonetic to cyrillic converter
    By Brt93yoda in forum Java Theory & Questions
    Replies: 1
    Last Post: September 5th, 2010, 02:26 PM
  5. Piglatin Converter
    By jross21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:09 PM