Question
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
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{ private ArrayList 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{ private ArrayList 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) { } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started