Access over 35 million academic & study documents

A)The Stack implementation has a fixed limit on the maximum number o

Content type
User Generated
Showing Page:
1/6
a)The Stack implementation has a fixed limit on the
maximum number of items that can be stored (up to
maxSize) and the program shows message (i.e., Stack
full) while the stack is full. Now modify Stack
implementation to (i) make more space available by
resizing the stack when necessary and (ii) show the sum of
values stored in the stack at any time.
class Stack {
privateint size;
privateint[] data;
public Stack(int maxSize) {
data = newint[maxSize];
size = 0;
}
publicvoid push(int value) {
if (size >= data.length)
throw new RuntimeException(\"Stack full\");
data[size] = value;
size++;
}
publicint pop() {
if (isEmpty())
throw new RuntimeException(\"Stack empty\");
size--;
return data[size];
}
publicboolean isEmpty() {
return size == 0;
}
publicint top() {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/6
int top = pop(); // Get the top
push(top); // Put it back
return top;
}
publicvoid clear() {
// size = 0; is O(1)
// This is O(n):
while (!isEmpty())
pop(); // Ignore the result
}
publicvoid swap() {
int top, second;
top = pop();
second = pop();
push(top); // Push the top first
push(second); // The second becomes the top
}
}
b)
Write a program in Java to determine whether a string of
characters entered from the keyboard is a palindrome, i.e.
the string reads the same both forwards and backwards.
The program should store user input into both a stack and
a queue of characters. After all the characters are entered,
compare the contents of the stack and queue using pop
and dequeuer operations. If the contents are the same, the
string reads the same forwards and backwards, thus it is a
palindrome. If a mismatch is found, the string is not a
palindrome.
Note:
(i) The program should ignore the case of letters (i.e. a
and A are the same) when comparing
(ii) It should also ignore blanks and punctuation

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/6

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 6 pages?
Access Now
Unformatted Attachment Preview
a)The Stack implementation has a fixed limit on the maximum number of items that can be stored (up to maxSize) and the program shows message (i.e., “Stack full”) while the stack is full. Now modify Stack implementation to (i) make more space available by resizing the stack when necessary and (ii) show the sum of values stored in the stack at any time. class Stack { privateint size; privateint[] data; public Stack(int maxSize) { data = newint[maxSize]; size = 0; } publicvoid push(int value) { if (size >= data.length) throw new RuntimeException(\"Stack full\"); data[size] = value; size++; } publicint pop() { if (isEmpty()) throw new RuntimeException(\"Stack empty\"); size--; return data[size]; } publicboolean isEmpty() { return size == 0; } publicint top() { int top = pop(); // Get the top push(top); // Put it back return top; } publicvoid clear() { // size = 0; is O(1) // This is O(n): while (!isEmpty()) pop(); // Ignore the result } publicvoid swap() { int top, second; top = pop(); second = pop(); push(top); // Push the top first push(second); // The second becomes the top } } b) Write a program in Java to determine whether a string of characters entered from the keyboard is a palindrome, i.e. the string reads the same both forwards and backwards. The program should store user input into both a stack and a queue of characters. After all the characters are entered, compare the contents of the stack and queue using pop and dequeuer operations. If the contents are the same ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4