Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help for Java programming assignment: Modify the CharStack class, adding explicit error reporting for stack underflow via a checked exception (i.e., one that must

Need help for Java programming assignment:

Modify the CharStack class, adding 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.

**********************

Original code:

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(); } } 

Note #1: Note the use of the "decorated" names m_ptr and m_data to denote member variables. This makes their memberness obvious by inspection, and can make code easier to understand. If you use names like this, be sure to use them consistently!

Note #2: The constructor won't let you try to create a stack with an initial capacity of less than one element. This is an example of the kind of fail-safety you should try to build into all of your classes: make it impossible for them to become internally corrupted.

Note #3: When we've filled up our initial storage array, these few lines of code allocate a new one and copy the contents of our old one. Again, code that uses CharStack won't risk CharStack's running out of storage space; CharStack takes care of everything.

Note #4: pop( ) can fail if m_ptr is zero. We haven't talked about the right way to handle this yet (using Exceptions), so I'm ignoring it. In a real class, you'd never ignore this possibility!

Note #5: This main( ) function creates a CharStack and lets you type in some characters. When you send an end-of-file character (Ctrl-Z on a line by itself for Windows; Ctrl-D for Unix), it prints the characters reversed. I've placed this function inside CharStack for convenience, but it could just as easily be inside another class. It's actually a good habit to get into to write main( ) functions in many of your classes that do nothing but test the class they appear in. Then you'll always have a test driver handy when you need to check some of your code, and this test function can serve as a usage example too.

The Java API provides a Stack class (java.util.Stack) into which you can insert objects but not chars, ints, etc. It also provides a Vector class (growable array) and a Hashtable class (associative array).

****************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions