Question: /**************************** * Week 3 lab - exercise 1: * * a simple Stack class * *****************************/ /** * Class to test the Stack class. */

/****************************

* Week 3 lab - exercise 1: *

* a simple Stack class *

*****************************/

/**

* Class to test the Stack class.

*/

public class Main

{

public static void main(String[] args)

{

Stack s = new Stack();

System.out.println("Insertion of 10 characters in s");

for (int i = 0; i < 10; i++)

{

int x = 32 + (int) (Math.random() * 95);

System.out.println(x + " --> " + (char) x);

s.push((char) x);

}

System.out.println(" Displaying and deleting elements");

for (int i = 0; i < 10; i++)

{

System.out.println("Item at the top: " + s.peek());

s.pop();

}

}

}

/****************************

* Week 3 lab - exercise 1: *

* a simple Stack class *

*****************************/

/**

* Class implementing a Stack ADT.

*/

public class Stack

{

/**

* Creates an empty stack with an array size of 100.

*/

public Stack()

{

size = 100;

list = new char[size];

n = 0;

}

/**

* Creates an empty stack using the array size passed as a parameter.

*

* @param s array size.

*/

public Stack(int s)

{

size = s;

list = new char[size];

n = 0;

}

/**

* Adds an element to the top of the stack.

*

* @param c element to be added to the stack.

*/

public void push(char c)

{

list[n] = c;

n++;

}

/**

* Removes the element at the top of the stack.

*/

public void pop()

{

n--;

}

/**

* Returns the element at the top of the stack. Does not remove it.

*

* @return the element at the top of the stack.

*/

public char peek()

{

return list[n - 1];

}

/**

* Determines whether the stack is empty.

*

* @return true if the stack is empty, false otherwise.

*/

public boolean isEmpty()

{

return n == 0;

}

private char[] list; //array to store the stack items

private int size; //size of the array

private int n; //amount of items in the stack

}

EXERCISE 2:

Modify the stack class to include appropriate error messages if invalid conditions occurfor example, trying to pop an item when the stack is empty., or push when the stack is already full.

EXERCISE 3:

Write a Java program that uses a stack to test whether an input string is a palindrome, that is a word that is the same whether it is spelled forward or backwards. Your program must make use a stack to reverse the order of the characters in the string. You can use the stack class from the previous exercise (2). The two strings, one with characters in forward order and the other from them being reassembled in reverse, may be compared character-by-character to make sure all of the entries match.

If variable x is a String type, you may make use of the Java method x.charAt(i) to access the character at the ith location in the string.

To demonstrate, make up your own fixed list of Strings that includes both palindromes and non-palindromes as its entries.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!