Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PalindromeTester: */ package StudentProjects.P2_Stacks.StarterFiles.PalindromeTester; import StudentProjects.P2_Stacks.StarterFiles.StackArray.*; /** * */ public class PalindromeTester { // ************************************************************************ // Method to test if a string is a Palindrome

PalindromeTester:

*/ package StudentProjects.P2_Stacks.StarterFiles.PalindromeTester; import StudentProjects.P2_Stacks.StarterFiles.StackArray.*;

/** * */ public class PalindromeTester {

// ************************************************************************ // Method to test if a string is a Palindrome using a stack // 1. Create an ArrayStack that will hold characters (that's given) // 2. Loop through string, putting letters or digits onto the stack // 3. Loop through the string and for letters or digits // pop character from the stack // convert both characters to lower case and compare them // if they are not the the same, return false // 4. return true // ************************************************************************ public boolean isPalindrome(String string) { ArrayStack stringStack = new ArrayStack(); boolean result = true; System.out.println(\"STUB method : replace with your code. \"); return result; } public static void main(String[] args) { PalindromeTester pt = new PalindromeTester(); String currentString = \"madam\"; if (pt.isPalindrome(currentString)) System.out.println(currentString + \" IS a palindrome.\"); else System.out.println(currentString + \" is NOT a palindrome.\"); currentString = \"adam\"; if (pt.isPalindrome(currentString)) System.out.println(currentString + \" IS a palindrome.\"); else System.out.println(currentString + \" is NOT a palindrome.\"); currentString = \"Race car\"; if (pt.isPalindrome(currentString)) System.out.println(currentString + \" IS a palindrome.\"); else System.out.println(currentString + \" is NOT a palindrome.\"); } }

and StackArray :

package StudentProjects.P2_Stacks.StarterFiles.StackArray;

/** * * STACK where the last element in the array is the bottom of the stack * * * * @param */ public class ArrayStack implements StackInterface {

private T[] stack; // Array of stack entries private int topIndex; // Index of top entry private static final int DEFAULT_CAPACITY = 50;

public ArrayStack() { this(DEFAULT_CAPACITY); } // end default constructor

public ArrayStack(int initialCapacity) {

System.out.println(\"STUB ArrayStack constructor : replace with your code. \");

} // end constructor

@Override public void push(T newEntry) {

System.out.println(\"STUB push method : replace with your code. \");

} // end push

@Override public T peek() { T top = null;

System.out.println(\"STUB peek method : replace with your code. \");

return top; } // end peek

@Override public T pop() { T top = null;

System.out.println(\"STUB pop method : replace with your code. \");

return top; } // end pop

@Override public boolean isEmpty() { boolean result = false;

System.out.println(\"STUB isEmpty method : replace with your code. \");

return result; }

@Override public void clear() {

System.out.println(\"STUB clear method : replace with your code. \"); }

// Doubles the size of the array stack private void doubleArray() {

System.out.println(\"STUB doubleArray method : replace with your code. \"); }

public static void main(String[] args) {

System.out.println(\"Create a stack: \"); StackInterface myStack = new ArrayStack(); System.out.println(\"isEmpty() returns \" + myStack.isEmpty()); System.out.println(\" Add to stack to get \" + \"Joe Jane Jill Jess Jim\");

myStack.push(\"Jim\"); myStack.push(\"Jess\"); myStack.push(\"Jill\"); myStack.push(\"Jane\"); myStack.push(\"Joe\"); System.out.println(\" isEmpty() returns \" + myStack.isEmpty());

System.out.println(\" Testing peek and pop:\"); while (!myStack.isEmpty()) { String top = myStack.peek(); System.out.println(\" \" + top + \" is at the top of the stack.\"); top = myStack.pop(); System.out.println(top + \" is removed from the stack.\"); } // end while

System.out.print(\" The stack should be empty: \"); System.out.println(\"isEmpty() returns \" + myStack.isEmpty()); System.out.println(\" Add to stack to get \" + \"Jim Jess Joe \"); myStack.push(\"Joe\"); myStack.push(\"Jess\"); myStack.push(\"Jim\");

System.out.println(\" Testing clear:\"); myStack.clear();

System.out.println(\"The stack should be empty: \"); System.out.println(\" isEmpty() returns \" + myStack.isEmpty()); /* System.out.println(\" myStack.peek() returns \"); System.out.println(myStack.peek()); System.out.println(\" myStack.pop() returns \"); System.out.println(myStack.pop()); */ } // end main

} // end ArrayStack

StackInterface

package StudentProjects.P2_Stacks.StarterFiles.StackArray;

import StudentProjects.P2_Stacks.StarterFiles.LispExpression.*;

/** */ public interface StackInterface {

/** * Adds a new entry to the top of this stack. @param newEntry An object to * be added to the stack. * @param newEntry Entry that we'll be putting onto the top of the stack */ public void push(T newEntry);

/** * Removes and returns this stack's top entry. @return The object at the top * of the stack. @throws EmptyStackException if the stack is empty before * the operation. * @return */ public T pop();

/** * Retrieves this stack's top entry. @return The object at the top of the * stack. @throws EmptyStackException if the stack is empty. * @return */ public T peek();

/** * Detects whether this stack is empty. @return True if the stack is empty. * @return */ public boolean isEmpty();

/** * Removes all entries from this stack. */ public void clear(); } // end StackInterface.




Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Write a formal description of the following graph. 1 4 2 3 6

Answered: 1 week ago

Question

d. Is it part of a concentration, minor, or major program?

Answered: 1 week ago