Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need urgent help for Java programming assignment: Modify the CharStack class (see code below), adding explicit error reporting for stack underflow via a checked exception

Need urgent help for Java programming assignment:

Modify the CharStack class (see code below), 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.

Please do not copy the answer from elsewhere.

Many thanks!!

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

Original code:

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

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

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

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

Students also viewed these Databases questions

Question

What is computer neworking ?

Answered: 1 week ago