Question

In: Computer Science

Question: The purpose for this project is to reinforce the knowledge from Chapter Two of the...

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)
    {
        
    }
}

Solutions

Expert Solution

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.


Related Solutions

The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts that are important for the origin of the elements. 1. Use the abundances of the stable isotopes of strontium and the masses of these nuclides (found at http://atom.kaeri.re.kr/nuchart/) to calculate the atomic weight of strontium. Compare the value that you get with the value shown in the periodic table found at http://www.rsc.org/periodic-table. Show your work. 2. Consult the chart of the nuclides (http://atom.kaeri.re.kr/nuchart/ )...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the following operations: Addition Subtraction Multiplication Division A “main program” needs to create one or more complex numbers, perform the various arithmetic operations, and then display results. Develop the program in ADA code with several packages. Write a short report that documents all work done, including the overall design, explanation of the implementation, the input data used to run the program, the output of the...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on the environmental engineering and sustainable development for environmental engineering. TASK 1 (DOCUMENTATION) Development in science and technology has increase the level of human lifestyle which leads to technology advancement. However, everything good comes with a price. One of the greatest problems that the world is facing today is that of environmental pollution, increasing with every passing year and causing grave and irreparable damage to...
What are the Project Management Knowledge areas? Explain the various types of knowledge areas that project...
What are the Project Management Knowledge areas? Explain the various types of knowledge areas that project managers should develop for effective and efficient management of their projects.
create two multiple choice question from each category below 10 questions total, that will test knowledge...
create two multiple choice question from each category below 10 questions total, that will test knowledge and understanding of key topics. you can choose the topics but your questions and answers should be written thoughtfully. identify key ideas from e ach category and create your questions to test knowledge of these key ideas. 1 chemical and physical growth requirements 2 questions. 2 chemical and physical growth control 2 questions. 3 DNA and RNA replication 2 questions 4 microbial genetics 2...
Question 2: List two (2) complications from asthma   Using your knowledge of anatomy and pathophysiology, explain...
Question 2: List two (2) complications from asthma   Using your knowledge of anatomy and pathophysiology, explain in detail why one of these complications could occur. Ensure ALL your statements are supported with scholarly recent and relevant literature
1. Read each question. 2. Answer based upon your knowledge of the chapter and your own...
1. Read each question. 2. Answer based upon your knowledge of the chapter and your own experience. 3. Indicate by number (1 or 2) which question you are answering first. 4. Proofread your answer--demonstrate your best writing ability. CHAPTER 12 Effective Business Presentations 1. What role do visuals play in business presentations? 2. The four Ps: Planning, preparing, practicing, and presenting, are very important to presentations. Explain how you understand each of them. Is one more important than the others?
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 3 from chapter 13:...
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 3 from chapter 13: "One idea to extend health insurance to all Americans is to require Americans to purchase a health insurance policy. People of limited means would have their purchase of insurance subsidized. Discuss the benefits and costs of requiring people to purchase health insurance."
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 6 from chapter 6:...
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 6 from chapter 6: "Give some examples of policies that could be used to reduce the inequalities associated with market-generated outcomes. Are these policies likely to increase or decrease efficiency?"
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 2 from chapter 9:...
The question is from the textbook Inequality, Discrimination, Poverty, and Mobility Question 2 from chapter 9: "Is statistical discrimination "worse" than personal prejudice? Investigate this question in all its ramifications."
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT