Question: Java Exercise 1: Review of the Stack ADT Create a project using the classes in A Simple Stack Class. (Links to an external site.) Links
Java
Exercise 1: Review of the Stack ADT
Create a project using the classes in "A Simple Stack Class". (Links to an external site.)
Links to an external site.
Compile the project, run it, and review the code that is given carefully. This code tests the stack class provided in the lecture.
/****************************
* 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: An Improved Stack Class
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. For example...
Exercise 3: Using a Stack in an Application
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.
Exercise 4: Review of the Queue ADT
Create a project using the classes in "A Simple Queue Class. (Links to an external site.)
Links to an external site.
" Compile the project, run it, and review the code that is given carefully. This code tests the queue class provided in the lecture.
Exe/****************************
* Week 3 lab - exercise 4: *
* a simple Queue class *
*****************************/
/**
* Class to test the Queue class.
*/
public class Main
{
public static void main(String[] args)
{
Queue q = new Queue();
System.out.println("Insertion of 10 characters in q");
for (int i = 0; i < 10; i++)
{
int x = 32 + (int) (Math.random() * 95);
System.out.println(x + " --> " + (char) x);
q.enqueue((char) x);
}
System.out.println(" Displaying and deleting elements");
for (int i = 0; i < 10; i++)
{
System.out.println("Item at the front: " + q.getFront());
q.dequeue();
}
}
}
/****************************
* Week 3 lab - exercise 4: *
* a simple Queue class *
*****************************/
/**
* Class implementing a Queue ADT.
*/
public class Queue
{
/**
* Creates an empty queue with an array size of 100.
*/
public Queue()
{
size = 100;
list = new char[size];
front = 0;
back = size - 1;
count = 0;
}
/**
* Creates an empty queue using the array size passed as a parameter.
*
* @param s array size.
*/
public Queue(int s)
{
size = s;
list = new char[size];
front = 0;
back = size - 1;
count = 0;
}
/**
* Adds an element to the back of the queue.
*
* @param c element to be added to the queue.
*/
public void enqueue(char c)
{
back = (back + 1) % size;
list[back] = c;
count++;
}
/**
* Removes the element in the front of the queue.
*/
public void dequeue()
{
front = (front + 1) % size;
count--;
}
/**
* Returns the element at the front of the queue. Does not remove it.
*
* @return the element at the front of the queue.
*/
public char getFront()
{
return list[front];
}
/**
* Determines whether the queue is empty.
*
* @return true if the queue is empty, false otherwise.
*/
public boolean isEmpty()
{
return count == 0;
}
private char[] list; //array to store the queue items
private int size; //size of the array
private int count; //number of items in the queue
private int front, back; //front and back locations
}
Exercise 5: An Improved Queue Class
Modify the class queue to include appropriate error messages if invalid conditions occurfor example, trying to dequeue an item when the queue is empty.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
