?
Solved

show the position after find the keysearch

Posted on 2005-03-10
2
Medium Priority
?
187 Views
Last Modified: 2010-03-31
Hello all, my problem is i want to find the position of the keysearch after i found it, if i did not find the keysearch then it will report -1.  I have two classes one is Stack.java and other is StackTest.java

public class Stack
{
      final int EMPTY = 0;
      final int DEFAULT_VALUE = 10;
      final int NEGATIVE_ONE = -1;

      private Object[] stack;
      private int top;
      private int maxSize;

      // Default constructor

      Stack()
      {
            maxSize = DEFAULT_VALUE;
            stack = new Object[DEFAULT_VALUE];
            top = EMPTY;
      }

      // Non default constructor
      Stack(int size)
      {
            maxSize = size;
            stack = new Object[maxSize];
            top = EMPTY;
      }

      // isEmpty()
      public boolean isEmpty()
      {
            return (top == EMPTY);
      }

      // isFull()
      public boolean isFull()
      {
            return (top == stack.length);
      }

      //Object pop()

      public Object pop()
      {
            if(isEmpty())
            {
                  return null;
            }
            else
            {
                  return stack[--top];
            }
      }

      // Object push()
      public void push(Object objectItem)
      {
            if(top == (stack.length))
            {
                  ;
            }
            else
            {
                  stack[top++] = objectItem;
            }

      }

      // numItems()
      public int numItems()
      {
            return top;
      }

      // capacity()
      public int capacity()
      {
            return maxSize;
      }

      // peek()
      public Object peek()
      {
            Object temp = null;
            if(!isEmpty())
            {
                  temp = pop();
            }

            push(temp);

            return temp;

      }

      // popAll()
      public Object popAll()
      {
            int newArraySize = stack.length;

            Object newStack = new Object[newArraySize];
            int i = 0;
            while(!isEmpty())
            {
                  stack[i] = pop();
                  i++;
            }

            return newStack;
      }

      // search
       public int search(Object objectItem)
      {
            int index = EMPTY;
            for(int i = 0; i < stack.length; i++)
            {
                  if(objectItem.equals(stack[i]))
                  {
                        index = i;
                        break;
                  }

                  else
                  {
                        index = NEGATIVE_ONE;
                  }

            }
            return index;
       }

}

public class StackTest
{
    public static void main(String args[])
    {
      Integer  ival;
      Double   dval;
      Object   obj;

      Stack    stack;

      stack = new Stack();  // Default instantiation

        obj = stack.pop();    // Test empty stack null return
      System.out.println(" 0: pop      = " + obj);

      System.out.println(" 1: capacity = " + stack.capacity());
      System.out.println(" 2: numItems = " + stack.numItems());

        // push() 10 Integer elements (this fills up the stack)
      for(int i = 0; i <  stack.capacity(); i++) {
          stack.push(new Integer(i + 10));
      }
      System.out.println(" 3: numItems = " + stack.numItems());

        // pop() and report all 10 elements (this empties the stack)
      System.out.println();
      while(!stack.isEmpty()) {
          ival = (Integer)stack.pop();
          System.out.println(" ival = " + ival);
      }
      System.out.println();
      System.out.println(" 4: numItems = " + stack.numItems());

        // push() 10 Double elements (this fills up the stack)
        for(int i = 0; i <  stack.capacity(); i++) {
            stack.push(new Double(i + 10));
        }
        System.out.println(" 5: numItems = " + stack.numItems());

        // pop() and report all 10 elements (this empties the stack)
      System.out.println();
        while(!stack.isEmpty()) {
            dval = (Double)stack.pop();
            System.out.println(" dval = " + dval);
        }
      System.out.println();
        System.out.println(" 6: numItems = " + stack.numItems());

      stack = new Stack(1000);  // instantiate a 1000 element stack

      System.out.println(" 7: capacity = " + stack.capacity());
      System.out.println(" 8: numItems = " + stack.numItems());

        // push() 1000 Double elements (this fills up the stack)
        for(int i = 0; i <  stack.capacity(); i++) {
            stack.push(new Double(i + 10));
        }

        // Search for existing element
      int pos = stack.search(new Double(60.0));
      System.out.println(" 9: position = " + pos); // This is where i have a trouble.

        // Search for non-existent element
      pos = stack.search(new Double(2000.0));
      System.out.println("10: position = " + pos);

        // pop() top element
      dval = (Double)stack.pop();

      if(dval.doubleValue() != 1009.0 || stack.numItems() != 999) {
          System.out.println("11: Program logic error");
      }

        // Deallocate existing Object array[1000], allocate
        // a new  Object array[1000]
      stack.popAll();
      if(stack.capacity() != 1000 || stack.numItems() != 0) {
          System.out.println("12: Program logic error");
      }
        // push() 1000 Double elements (this fills up the stack)
        for(int i = 0; i <  stack.capacity(); i++) {
            stack.push(new Double(i + 10));
        }
        // test peek()
        dval = (Double)stack.peek();
        if(dval.doubleValue() != 1009.0 || stack.numItems() != 1000) {
          System.out.println("13: Program logic error");
        }
    }
}

The problem that i have when i search the value 60 it should report the index is 950 not 50.
any help would be appreciate.
thanks
0
Comment
Question by:thayboi3000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13508797
Basically your push method shud me something like below


 public void push(Object objectItem)
     {
             
          if(top == (stack.length))
          {
               ;
          }
          else
          {
                  int  index= stack.length-(top+1);
                  top++;
               stack[index] = objectItem;
          }

     }
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 375 total points
ID: 13508828
and search method should be something like this

  public int search(Object objectItem)
     {
          int index = EMPTY;
          for(int i = 0; i < stack.length; i++)
          {
               if(objectItem.equals(stack[i]))
               {
                    index = i+1;                        
                    break;
               }

               else
               {
                    index = NEGATIVE_ONE;
               }

          }
          return index;
      }

}

replace these two methods. then ur code gives the expected results
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question