X354: ArrayBasedStack Decimal to Binary

Write an ArrayBasedStack<T> member method called decimalToBinary() that takes a decimal as an int parameter, then uses an ArrayBasedStack to calculate and return the binary equivalent of the decimal number in the form of a String.

The process of calculating the binary equivalent of a decimal number is described below.

Step 1 - Take the decimal number to convert

Step 2 - while the number to convert is greater than 0

2.1 Find the remainder after dividing the number by 2, push remainder onto stack.

2.2 Set the number as number รท 2

Step 3 - Pop elements from stack and print the binary number

Example

Consider the decimal number 6

6 divided by 2 = 3 remainder 0 push 0 on to Stack number = 3

3 divided by 2 = 1 remainder 1 push 1 on to Stack number = 1

1 divided by 2 = 0 remainder 1 push 1 on to Stack number = 0

pop each element from stack and append to String return result Decimal number 6 = Binary number 110

Your method implementation will be inserted/compiled inside the ArrayBasedStack<T> class. \n\nThe member fields your method implementations may access are .Your Stack API methods. (Your solution code may also include helper methods.)

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.