• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Java Stacks.

mooo

thecyberninja
#1
in my csci class we are looking at stacks. Tomorrow we have a quiz and the teacher told us to look at stacks and how they are used, he said the best to look at would be for binary conversion involving stacks and then, reverse input with stacks. I have found a great website explaining what stack and all the methods it calls do. Now my only problem is trying to find examples on how it would be used for binary conversion. I figured out the string with a friend but it was too late last night to finish the other. Any ideas on where i can look at some sample code?
 

mooo

thecyberninja
#3
he said, it would be something like
get an int from a user,
allocate stack size to exactly the number of binary digits
convert it to binary using stacks (pop,push...)
print the binary numbers
 
#4
java.util.Stack

basically a Stack is like a tower of lego blocks, you can only really add and remove to it at the top of the tower.

I am guessing that your lecturer intends for you to go through and use this kind of method

Code:
143

Division	Result		Binary
143 / 2		=> 71.5		1
71 / 2		=> 35.5		1
35 / 2		=> 17.5		1
17 / 2		=> 8.5		1
8 / 2		=> 4		0
4 / 2		=> 2		0
2 / 2		=> 1		0
1 / 2		=> 0.5		1
0 / 2		=> 0		0

128	64	32	16	8	4	2	0
1	0	0	0	1	1	1	1
I think you can get the idea of where the stack comes in now as the order in which the binary values come out is the inverse of the way they are displayed. So you could push each binary value on to the stack as you do the division and then pop them all back out in order to display the result. I hope thats not helping you too much but I found the idea quite interesting and sorta ran with it :p
 

mooo

thecyberninja
#5
i figured it out, made my own program took me a while, but got the hard part done. I will post it once i get home. The allocate exact stack size was pretty easy though once i worked on it and figured out exactly what he ment, thought it was going to take days to just figure out that part. The only thign i have to do is reverse my stack and it will be done :)
int size = (int)((Math.log(input) / Math.log(base) +1)
myStack stk = new myStack(size)
 

mooo

thecyberninja
#6
hmm i am running into trouble now pop off the stack.
it outputs ???? instead of the stack in reverse. Any ideas on what i am missing?

-------------start of code----------------------
Code:
   import java.io.*;
   import java.math.*;
	
	
    public class maintest
   {
   
   
       public static void main (String args[])throws IOException
      {
      
         int a=1;
         while(a==1)
         {
         
         
            String str,str1;
            System.out.println("Enter a Decimal Value:");
            InputStreamReader sr=new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(sr);
            str=br.readLine();
            int q=Integer.parseInt(str);
            
         	
            System.out.println("Enter a Base Value:");
            InputStreamReader sr1 =new InputStreamReader(System.in);
            BufferedReader br1 = new BufferedReader(sr1);
            str1=br1.readLine();
            int base=Integer.parseInt(str1);
            int remain; 
           
            if ( q == 0 && base == 0)
               break;
           
            int size = (int)((Math.log(q)) / (Math.log(base)) + 1);
            myStack stk = new myStack(size);
         	
            System.out.println();
            while ( q > 0 )
            {
               remain = q % base;
               q = q / base;
               System.out.print(remain);
               stk.push((char)(remain- 0x30  ));
            }
            
            System.out.println();
         	
            while( !stk.isEmpty() )
            {
              
               char output =  stk.pop();      
               System.out.print(output );
            }
            System.out.println(" ");

         	
         }
      	
      }}


-------------------------------------------------------------------------

Code:
   import java.io.*;

    class myStack
   {
      private int maxSize;
      private char[] stackArray;
      private int top;
   
       public myStack(int max)
      {
         maxSize = max;
         stackArray = new char[maxSize];
         top = -1;
      }
   
       public void push(char j)
      {
         stackArray[++top] = j;
      }
   
       public char pop()
      {
         return stackArray[top--];
      }
   
       public char peek()
      {
         return stackArray[top];
      }
   
       public boolean isEmpty()
      {
         return (top== -1);
      }
   }
edit by Geffy: Added [ CODE ] tags to make it more readable
 

mooo

thecyberninja
#7
YaY !!! i got it!!!


woot woot. Here it is. just have to clean it up some.

-----------------------------------------------
Code:
   import java.io.*;
   import java.math.*;
	
	
    public class maintest
   {
   
   
       public static void main (String args[])throws IOException
      {
      
         int a=1;
         while(a==1)
         {
         
         
            String str,str1;
            System.out.println("Enter a Decimal Value:");
            InputStreamReader sr=new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(sr);
            str=br.readLine();
            int q=Integer.parseInt(str);
            
         	
            System.out.println("Enter a Base Value:");
            InputStreamReader sr1 =new InputStreamReader(System.in);
            BufferedReader br1 = new BufferedReader(sr1);
            str1=br1.readLine();
            int base=Integer.parseInt(str1);
            int remain; 
           
            if ( q == 0 && base == 0)
               break;
           
            int size = (int)((Math.log(q)) / (Math.log(base)) + 1);
            myStack stk = new myStack(size);
         	
            System.out.println();
            while ( q > 0 )
            {
               remain = q % base;
               q = q / base;
               System.out.print(remain);
               //stk.push((char)(remain- 0x30  ));
               stk.push((char)remain);
            	
            }
            
            System.out.println();
         	
            while( !stk.isEmpty() )
            {
              
               char output = stk.pop();      // pop a char,
               System.out.print((int)output);
            }
            System.out.println(" ");
            System.out.println("Enter 0 for both to quit.");
            System.out.println(" ");
         	
         }
      	
      }}
edit by Geffy: Added [ CODE ] tags to make it more readable
 
#8
editted your posts to use the code tags so that it treats the code more like preformatted text and does proper indentation and the like
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,013
Latest member
Pdawgintown