Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Can someOne please help me with this one


Message #1 by sam-armen@e... on Sun, 14 Apr 2002 11:03:23
> How can I change TestStackPT.java to test both the array and linked 
i> mplementations in  one execution of the program.  I think it has 
s> omething to do with putting a for loop in TestStackPT.java that will 
make 
i> t run twice, but i'm not sure on how to do it. Can someone please help.

Easiest way would be:

Un-comment //StackPT s = new LinkedStackPT(); and change the s to another 
variable identifier, say x. Then just perform all the operations on x as 
well as s, i.e. you'd do:
    for (int i = 0; i < 10; i++ ) {
        s.push (new Integer(i)); 
        x.push (new Integer(i));
    }
OR
If you really HAD to use a for loop you could just trap for if its the 
first, or second iteration and create your StackPT object as a 
LinkedStackPT or an ArrayStackPT accordingly.

Good Luck! 

Charlie


> 

> 
i> mport BreezyGUI.*;

> public class TestStackPT{
 >   public static void main (String[] args){
 >      StackPT s = new ArrayStackPT(10);
 >      //StackPT s = new LinkedStackPT();
 >      try{
 >          String str = (String)s.peek();
 >      }catch (Exception e){
 >          System.out.println("Expect error because peeking when 
empty:\n" 
+>  e);
 >      }
 >      
 >      for (int i = 0; i < 10; i++ )
 >          s.push (new Integer(i));
 >          
 >      System.out.println ("Expect 987 : "+ s.pop() + s.pop() + s.pop()); 
 >      
 >      try{
 >         for (int i = 0; i < 4; i ++)
 >            s.push (new Integer(i));
 >      }catch (Exception e){
 >            System.out.println("Expect full stack error with 
A> rrayStackPT:\n" + e);
 >      }
 >      
 >      System.out.print ("Expect 2106543210 for ArrayStackPT,\n" +
 >                        "Expect 32106543210 for LinkedStackPT : "); 
 >                          
 >      while (! s.isEmpty())
 >          System.out.print(s.pop());
 >      
 >      GBFrame.pause();
 >   }                    
}> 
----------------SNIP----------------------------------

  Return to Index