I am supposed to make a charstack program that has to run an error. This is the Question:
Add explicit error reporting for stack underflow via a checked exception (i.e., one that must be declared and caught). Provide a main program that tests this new capability. I don't what the proctor means. I wrote one exception but its not right could someone help.

package charstack;
import java.io.*;

public class CharStack
{
  private char[] m_data;           // See Note #1 below
  
  private int m_ptr;
  
  public CharStack(int size)
  {
      m_ptr = 0;                   // Note #2
      m_data = new char[(size > 1 ? size : 10)]; 
  }
  
  public void push(char c)
  {
      if (m_ptr >= m_data.length) // Note #3
      {
         // Grow the array automatically
         char[] tmp = 
            new char[m_data.length * 2];
      
         System.arraycopy(m_data, 0, 
                          tmp, 0, 
                          m_data.length);
         m_data = tmp;
      }
      m_data[m_ptr++] = c;
  }
      
  public char pop()              // Note #4
  {
      return m_data[--m_ptr];
  }
  public boolean hasMoreElements()
  {
      return (m_ptr != 0);
  }
  
  // Note #5
  public static void main(String[] argv) 
      throws IOException
  {
      CharStack s = new CharStack(10);
      int i;
      while ( (i = System.in.read()) != -1 )
      {
         s.push((char) i);
      }
      while (s.hasMoreElements())
      {
         System.out.write(s.pop());
      }
      System.out.println();
    }
}

Recommended Answers

All 6 Replies

  1. Use the same code tag to end the code section as you used to start it. (That is, use CODE at the end, not ICODE.)
  2. Your main method, as given, does not cause a stack underflow condition. Do you know how to make a stack underflow occur?
  3. I think that the instructor wants you to write code that throws an exception, when needed. Consider this: Under what conditions is a stack underflow about to occur?

I need help with the stack overflow method, because it was a quick go through,[ I barely know the basics, Could anyone simplify a little more

  1. Use the same code tag to end the code section as you used to start it. (That is, use CODE at the end, not ICODE.)
  2. Your main method, as given, does not cause a stack underflow condition. Do you know how to make a stack underflow occur?
  3. I think that the instructor wants you to write code that throws an exception, when needed. Consider this: Under what conditions is a stack underflow about to occur?

I think I got it could you check.

package charstack;
import java.io.*;

public class CharStack
{
  private char[] m_data;           
  
  private int m_ptr;
  
  public CharStack(int size)
  {
      m_ptr = 0;                   
      m_data = new char[(size > 1 ? size : 10)]; 
  }
  
  public void push(char c)
  {
      if (m_ptr >= m_data.length) 
      {
        
         char[] tmp = 
            new char[m_data.length * 2];
      
         System.arraycopy(m_data, 0, 
                          tmp, 0, 
                          m_data.length);
         m_data = tmp;
      }
      m_data[m_ptr++] = c;
  }
      
  public char pop()              
  {
      return m_data[--m_ptr];
  }
  public boolean hasMoreElements()
  {
      return (m_ptr != 0);
  }
  
  
  public static void main(String[] argv) 
        throws IOException
        {
            CharStack s = new CharStack(10);
            int i;
            while ( (i = System.in.read()) != -1 )
        {
            s.push((char) i);
        }
        while (s.hasMoreElements())
        {
            System.out.write(s.pop());
        }
        System.out.println();
        try{
            while ( (i = System.in.read()) != -1 )
            {
                s.push((char) i);
            }
        }
        catch (StackOverflowError sofe)
        {
            while ( (i = System.in.read()) >= 10)
            {
                System.out.println("Stack over flow" + sofe);
            }
        }
        }
}

I need help with the stack overflow method, because it was a quick go through, I barely know the basics, Could anyone simplify a little more

  • When the stack overflows, do you want to throw an exception or increase the size of the stack?
  • You can't continue reading from 'System.in' after end of file.

The question requires underflow, not overflow.
Underflow is when you try to pop but there is nothing in the stack, eg if you go

new stack
stack.push(something)
stack.pop() // OK
stack.pop() // stack was empty   ->  underflow

Note: ArrayIndexOutOfBoundsException is not "...a checked exception (i.e., one that must be declared and caught)" (You can tell that because the code does not need to declare that this exception may be thrown.)

We're told that the proctor said "Add explicit error reporting for stack underflow via a checked exception ...". Later, vishal1949 said, "I need help with the stack overflow method, ..."

JamesCherrill is probably right in thinking that it's probably one or the other [underflow or overflow], not both. And even if it is both, it's best to work on one at a time and get it working before going on to the next one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.