Please use JAVA solve this problem. Array is not allow to use in this assignment
ID: 3587939 • Letter: P
Question
Please use JAVA solve this problem. Array is not allow to use in this assignment. Please give comments to explain each steps.
The purpose of this assignment is to familiarize you with stacks and queues. The assignment is split into 2 tasks.
The first part of the assignment is to take the Node, Stack, and Queue files and implement them (IE fill in the methods). Do not extend your class. References are to be singly linked.
Part two requires you to create another class to implement a Palindrome detector that uses a Stack and a Queue. It must fulfill the following requirements: 1. The program must loop to take input from the user. The input is a single word. If the word is quit, go to step 4. Otherwise we look to see if the input is an Palindrome (lowercase and uppercase are considered the same letter) 2. It must use both the Stack and the Queue you just wrote to detect Palindromes. 3. Output whether or not the inputed string is an Palindrome. 4. Once the user types quit, print out all the detected Palindromes in reverse order, then terminate. Use the proper data structure. 5. You cannot use array
Extra request: Rather than detecting if a word is a palindrome, instead tell me if an entire String is a palindrome. An entire String is a palindrome iff the string reads the same backwards to forwards, ignoring capitalization and punctuation. , the following line should be detected as a Palindrome.
"Go hang a salami, I’m a lasagna hog"
Explanation / Answer
//File Name: PalTest.java
import java.util.Scanner;
import javax.imageio.IIOException;
//Class PalTest definition
public class PalTest
{
//Main method definition
public static void main(String ss[]) throws IIOException
{
//Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
//Creates an object of PalindromeTesting class
PalindromeTesting x = new PalindromeTesting();
System.out.println("Please Enter a sentence: ");
//Accepts data from the console
x.setSentence(sc.nextLine());
x.setStackandQueue();
x.palindromeCompare();
System.out.println(x);
}//End of main method
}//End of class
--------------------------------------------------------------------------------
//File Name: PalindromeTesting.java
public class PalindromeTesting
{
//To store sentence
private String mySentence;
//To store result
private boolean finalResult;
//Declares stack object
private PalTestStack<String> StrStack;
//Declares queue object
private PalTestQueue<String> StrQueue;
//Default constructor
public PalindromeTesting()
{
mySentence = "";
finalResult = false;
StrStack = new PalTestStack<String>();
StrQueue = new PalTestQueue<String>();
}//End of constructor
//Method to add the sentence
public void setSentence(String value)
{
mySentence = value;
}//End of method
//Method to add the data to stack and queue
public void setStackandQueue()
{
//Split the data with space
String[] word = mySentence.split(" ");
int counter = 0;
word[counter] = word[counter].toLowerCase();
//Loops till length of the tokens
while(counter < word.length)
{
//Push the data to stack
StrStack.push(word[counter]);
//Adds the data to queue
StrQueue.addToQueue(word[counter]);
//Increase the counter
counter++;
}//End of while
}//End of method
//Method to compare stack and queue data for palindrome
public void palindromeCompare()
{
//Checks for stack or queue is empty
if(StrQueue.isEmpty() || StrStack.isEmpty())
{
finalResult = false;
}//End of if
//Not empty
else
{
//Loops till end of the queue
while(StrQueue.numberOfElements() != 0)
{
//Checks the stack and queue data for equality
if(StrQueue.look().equals(StrStack.look()))
{
//For next data in queue
StrQueue.deleteFromQueue();
//For next data in stack
StrStack.pop();
}//End of if
//Otherwise if not equal
else
{
finalResult = false;
break;
}//End of else
finalResult = true;
}//End of while
}//End of else
}//End of method
//Overrides toString() method to display information
public String toString()
{
return mySentence + " " + "is a palindrome sentence? " + finalResult;
}//End of method
}//End of class
------------------------------------------------------------------------------
//File Name: PalTestStack.java
import java.util.ArrayList;
//Creates PalTestStack class of type generic
public class PalTestStack <T>
{
//Creates an array list of type stack
private ArrayList<T> myStack;
//Constructor
public PalTestStack()
{
myStack = new ArrayList();
}//End of constructor
//Returns true if stack is empty otherwise false
public boolean isEmpty()
{
return myStack.isEmpty();
}//End of method
//To push a data to stack
public void push(T x)
{
myStack.add(x);
}//End of method
//To pop a data from the stack
public T pop()
{
//Declares generic type variable
T temp = null;
//Checks for empty
if(isEmpty())
{
System.out.println("This Stack is Empty");
}//End of if
//Otherwise if not empty
else
{
temp = myStack.get(myStack.size() - 1);
myStack.remove(myStack.size() - 1);
}//End of else
return temp;
}//End of method
//Returns stack top data
public T look()
{
//Declares generic type variable
T tempData = null;
//Checks for empty
if(isEmpty())
{
System.out.println("This stack is empty");
}//End of if
//Otherwise if not empty
else
{
tempData = myStack.get(myStack.size() -1);
}//End of if
return tempData;
}//End of method
}//End of class
-------------------------------------------------------------------------------------
//File Name: PalTestQueue
//Class PalTestQueue defined as template
public class PalTestQueue<T>
{
//Creates a inner class Node
protected class Node
{
//To store data
T data;
//To store link
Node link;
}//End of constructor
//First, second object of class Node
private Node firstN;
private Node lastN;
private int counter;
//Constructor
public PalTestQueue()
{
firstN = null;
lastN = null;
counter = 0;
}//End of constructor
//Add data to queue
public void addToQueue(T value)
{
//Temporary Node object created
Node add = new Node();
//Assigns data
add.data = value;
//Set the link to null
add.link = null;
//Checks for empty this is the first node
if(isEmpty())
{
//Adds the node to first and last
firstN = add;
lastN = add;
}//End of if
//Otherwise
else
{
//Last points to new node
lastN.link = add;
//Adds data to last
lastN = add;
}//End of else
//Increase the counter by one
counter++;
}//End of method
//Deletes a node from queue
public T deleteFromQueue()
{
//Creates a temporary generic variable
T tempData = null;
//Checks for empty
if(isEmpty())
{
System.out.println("Queue is empty cannot delete");
}//End of if
//Otherwise it is not empty
else
{
//Stores the first node data to tempData
tempData = firstN.data;
//Points to next node
firstN = firstN.link;
//Checks if first is null (the only node)
if(firstN == null)
{
lastN = null;
}//End of if
//Decrease the counter by one
counter--;
}//End of else
return tempData;
}//End of method
//Returns true if empty otherwise false
public boolean isEmpty()
{
return counter ==0;
}//End of method
//Returns first node data
public T look()
{
return firstN.data;
}//End of method
//Returns number of elements in the queue
public int numberOfElements()
{
return counter;
}//End of method
}//End of class
Sample Run 1:
Please Enter a sentence:
madam madam
madam madam
is a palindrome sentence? true
Sample Run 2:
Please Enter a sentence:
this is
this is
is a palindrome sentence? false