In: Computer Science
Question: The purpose for this project is to reinforce the knowledge from Chapter Two of the textbook. The ...
The purpose for this project is to reinforce the knowledge from Chapter Two of the textbook. The students will practice how to implement stack and queue data structure using list structure, for instance, ArrayList. The students will also apply stack and queue in real project, for instance, to check if a string is a palindrom.
Tasks:
1. Use ArrayList to implement MyStack class which define the data
structure that has Last In First Out property (35%)
2. Use ArrayList to implement MyQueue class which define the data
structure that has First In First Out property (35%)
3. Write a function public static Boolean isPalindrome(String sentence)
(30%). This function returns true if sentence is a palindrome; false
otherwise.
******************************************************************************************************
import java.util.Scanner; public class CSCI463ProjectTwo { public static void main(String [] args) { Scanner input = new Scanner(System.in); String sentence; String again; do{ System.out.println("Enter a sentence, I will tell you if it is a palindrome: "); sentence = input.nextLine(); if(isPalindrome(sentence)) System.out.println("\"" + sentence + "\" is a palindrome!"); else System.out.println("\"" + sentence + "\" is not a palindrome!"); System.out.println("Do you want another test (\"YES\" or \"NO\"): "); again = input.nextLine(); }while(again.equalsIgnoreCase("YES")); } /** * isPalindrom returns true if the given String is a palindrom * @ */ public static boolean isPalindrome(String sentence) { // declare a MyStack s // declare a MyQueue q for(int i = 0; i < sentence.length(); i++) { // if ith character in sentence is a letter // convert to upper case and push it into s and q } while(!s.isEmpty()){ // if the front of the queue not match the top of stack // return false // pop out top of the stack and front of the queue } return true; } }
***********************************************************************************************************************
import java.util.ArrayList; public class MyStack<E> { private ArrayList<E> list; // used to store elements in stack private int top; // the index of top element /** * constructor construct an empty stack */ public MyStack() { } /** * push push a given element on the top of the stack */ public void push(E item) { } /** * isEmpty return true if the stack is empty; false otherwise * @return true if the stack is empty; false otherwise */ public boolean isEmpty() { } /** * peek Return the top element */ public E peek() { } /** * pop Remove the top element from the stack. If the stack is empty,nothing happen */ public void pop() { } /** * size return the size of the stack * @return number of elements in stack */ public int size() { } }
****************************************************************************************
import java.util.ArrayList; public class MyQueue<E> { private ArrayList<E> list; // hold the elements in queue private int tail; // index of the last element in queue /** * constructor construct an empty queue */ public MyQueue() { } /** * isEmpty return true if the queue is empty; false otherwise * @return true if the queue is empty; false otherwise */ public boolean isEmpty() { } /** * size return the size of the queue * @return the number of elements in queue */ public int size() { } /** * peek return the front element of the queue * @return the front element of the queue. If the queue is empty, return null */ public E peek() { } /** * pop remove the front element of the queue */ public void pop() { } /** * push push a new element to the queue */ public void push(E item) { } }
import java.util.Scanner; import java.util.ArrayList; class MyQueue<E> { private ArrayList<E> list; // used to store elements in stack /** * constructor construct an empty stack */ public MyQueue() { list = new ArrayList<>(); } /** * push push a given element on the top of the stack */ public void push(E item) { list.add(item); } /** * isEmpty return true if the stack is empty; false otherwise * * @return true if the stack is empty; false otherwise */ public boolean isEmpty() { return list.isEmpty(); } /** * peek Return the top element */ public E peek() { return list.get(0); } /** * pop Remove the top element from the stack. If the stack is empty,nothing * happen */ public void pop() { list.remove(0); } /** * size return the size of the stack * * @return number of elements in stack */ public int size() { return list.size(); } } class MyStack<E> { private ArrayList<E> list; // used to store elements in stack /** * constructor construct an empty stack */ public MyStack() { list = new ArrayList<>(); } /** * push push a given element on the top of the stack */ public void push(E item) { list.add(item); } /** * isEmpty return true if the stack is empty; false otherwise * * @return true if the stack is empty; false otherwise */ public boolean isEmpty() { return list.isEmpty(); } /** * peek Return the top element */ public E peek() { return list.get(list.size()-1); } /** * pop Remove the top element from the stack. If the stack is empty,nothing * happen */ public void pop() { list.remove(list.size()-1); } /** * size return the size of the stack * * @return number of elements in stack */ public int size() { return list.size(); } } public class CSCI463ProjectTwo { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out.println("Enter a sentence, I will tell you if it is a palindrome: "); sentence = input.nextLine(); if (isPalindrome(sentence)) System.out.println("\"" + sentence + "\" is a palindrome!"); else System.out.println("\"" + sentence + "\" is not a palindrome!"); System.out.println("Do you want another test (\"YES\" or \"NO\"): "); again = input.nextLine(); } while (again.equalsIgnoreCase("YES")); } /** * isPalindrom returns true if the given String is a palindrom @ */ public static boolean isPalindrome(String sentence) { MyStack<Character> myStack = new MyStack<>(); MyQueue<Character> myQueue = new MyQueue<>(); for (int i = 0; i < sentence.length(); i++) { // if ith character in sentence is a letter // convert to upper case and push it into s and q if(Character.isLetter(sentence.charAt(i))) { myQueue.push(Character.toUpperCase(sentence.charAt(i))); myStack.push(Character.toUpperCase(sentence.charAt(i))); } } while (!myStack.isEmpty()) { // if the front of the queue not match the top of stack // return false if(myStack.peek() != myQueue.peek()) { return false; } myStack.pop(); myQueue.pop(); // pop out top of the stack and front of the queue } return true; } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.