Question
Please Code in Java and follow the directions given

Thanks

Program required

.A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from

Directions


Program #3 1, Show the LinkedtStackADT<T> interface 2. Create a LinkedStackDS<T> with the following methods: default construc
.A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell if the line is a a palindrome. Usea stack to read each non-blank character on a stack. Treat both upper-case and lower-case version of the letter as being the same character - Provide these 5 sample outputs and tell if each is a palindrome or not Too bad--I hid a boot Some men interpret eight memos "Go Hang a Salami! I'm a Lasagna Hog" (title of a book on palindromes by Jon Agee, 1991) A man, a plan, a canal-Panama Gateman sees my name, garageman sees name tag
Program #3 1, Show the LinkedtStackADT interface 2. Create a LinkedStackDS with the following methods: default constructor, overloaded constructor, copy constructor isEmptyStack, push, peek, pop 3. Create a private inner StackNode class with the following methods: default constructor, overloaded constructor, toString 3. Exception classes: StackException, StackUnderflowException, StackOverflowException 4. Create a PalindromeDemo class that instantiates a LinkedStackDS object. Execute a do-while loop that asks the user using dialog boxes to "Input a String for Palindrome Test:" Use the replaceAll method to remove all blanks and special characters from testStr. Output whether or not it is a palindrome in a dialog box. [Use the 5 inputs given on the other handout sheet for testing.]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedStackADT.java

public interface LinkedStackADT<T>

{
boolean isEmptyStack();
void push(T val);
T peek() throws StackUnderflowException;
void pop() throws StackUnderflowException;
}



StackUnderflowException.java


public class StackUnderflowException extends Exception

{
public StackUnderflowException(){

}
public StackUnderflowException(String msg)
{
super(msg);
}
}


LinkedStackDS.java


public class LinkedStackDS<T> implements LinkedStackADT<T>

{
private class StackNode
{
T data;
StackNode next;
StackNode()
{
}
StackNode(T data, StackNode next)
{
this.data = data;
this.next = next;
}
public String toString()
{
return data.toString();
}
}
private StackNode top;
public LinkedStackDS()

{
}

public boolean isEmptyStack()

{
return top == null;
}
public void push(T val)

{
top = new StackNode(val, top);
}


public T peek() throws StackUnderflowException

{
if(isEmptyStack())
throw new StackUnderflowException("stack is empty");
return top.data;
}

public void pop() throws StackUnderflowException

{
if(isEmptyStack())
throw new StackUnderflowException("stack is empty");
top = top.next;
}
}



PalindromeDemo.java

import javax.swing.JOptionPane;
public class PalindromeDemo

{
private static boolean isPalindrome(String input)
{
LinkedStackDS<Character> stack1 = new LinkedStackDS<Character>();
LinkedStackDS<Character> stack2 = new LinkedStackDS<Character>();
input = input.replaceAll("[^a-zA-Z]", "").toLowerCase();
//put the chars into stack1
for(int i = 0; i < input.length(); i++)
{
stack1.push(input.charAt(i));
}

while(!stack1.isEmptyStack())
{
try

{
stack2.push(stack1.peek());
stack1.pop();
}

catch (StackUnderflowException e)

{
System.out.println(e.getMessage());
}
}
for(int i = 0; i < input.length(); i++)
{
stack1.push(input.charAt(i));
}

while(!stack1.isEmptyStack())
{
try

{
if(!stack1.peek().equals(stack2.peek()))
return false;
stack1.pop();
stack2.pop();
}

catch (StackUnderflowException e)

{
System.out.println(e.getMessage());
}
}
return true;
}
public static void main(String[] args)

{
String input;
do
{
input = JOptionPane.showInputDialog("Enter the input string: ");
if(input != null)
{
if(isPalindrome(input))
JOptionPane.showMessageDialog(null, input + " it is a palindrome");
else
JOptionPane.showMessageDialog(null, input + " it is NOT a palindrome");
}
}

while(input != null);
}
}

// u can directly copy paste this code and then compile

Add a comment
Know the answer?
Add Answer to:
Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program using java that determines whether an input string is a palindrome; that is,...

    Write a program using java that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. please type out the code :)

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design:...

    Language = C++ How to complete this code? C++ Objects, Structs and Linked Lists. Program Design: You will create a class and then use the provided test program to make sure it works. This means that your class and methods must match the names used in the test program. Also, you need to break your class implementation into multiple files. You should have a car.h that defines the car class, a list.h, and a list.cpp. The link is a struct...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Exercise 11 - in Java please complete the following: For this exercise, you need to work...

    Exercise 11 - in Java please complete the following: For this exercise, you need to work on your own. In this exercise you will write the implementation of the pre-written implementation of the class CAR. The class CAR has the following data and methods listed below: . Data fields: A String model that stores the car model, and an int year that stores the year the car was built. . Default constructor . Overloaded constructor that passes values for both...

  • please write code in java and comment. thanks. the program is about interface . Implement the...

    please write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT