Question

In: Computer Science

(Using Java) create a class that can identify a palindrome. A palindrome is defined as -...

(Using Java) create a class that can identify a palindrome.

A palindrome is defined as
- A string of characters that reads the same from left to right as its does from right to left
- Example: Anna, Civic, Kayak, Level, Madam
- To recognize a palindrome, a queue can be used in conjunction with a stack
o A stack can be used to reverse the order of occurrences
o A queue can be used to preserve the order of occurrences
Hints: Use Stack class in JAVA library: https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Regard linked list in JAVA Library as queue:http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Solutions

Expert Solution

A palindrome is defined as
- A string of characters that reads the same from left to right as its does from right to left.

To recognize a palindrome, a queue can be used in conjunction with a stack.
o A stack can be used to reverse the order of occurrences
o A queue can be used to preserve the order of occurrences

A nonrecursive recognition algorithm for palindromes:

As you traverse the character string from left to right, insert each character into both a queue and a stack.

Compare the characters at the front of the queue and the top of the stack.(LIFO)

stack operations:

push(object o): inserts element o

pop(): removes and returns the last inserted element

isEmpty(): returns a Boolean value indicating whether no elements are stored.

a(top)
b
c
b

Queue Operations:

A queue is a list from which items are deleted from one end (front) and into which items are inserted at the other end (rear, or back).

Queue is referred to as a first-in-first-out (FIFO) data structure.

The first item inserted into a queue is the first item to leave.

isEmpty():boolean – Determine whether a queue is empty.

a(front) b c b d(back)

Programming code:

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

import java.util.Scanner;

public class Palindrome

{

   public static void main(String[ ] args)

   {

        Scanner input = new Scanner(System.in);

        String inputString;     

      System.out.print("Enter the input string: ");

        inputString = input.next( );

        if (isPalindrome( inputString )){

               System.out.println("the string is a palindrome.");

        }

        else{

               System.out.println("the string is not a palindrome.");

      }

   }

   public static boolean isPalindrome(String input)

   {  

      Queue<Character> q = new LinkedList<Character>( );

      Stack<Character> s = new Stack<Character>( );

      char letter;

      int i;

     

      for (i = 0; i < input.length( ); i++)

      {

        letter = input.charAt(i);

         q.add(letter);

         s.push(letter);

      }   

      while (!q.isEmpty( ))

      {

         if (q.remove( ) != s.pop( ))

           return false;

      }

      return true;

   }

   

}

Image of coding:

Image of output1:

imge of output2:Image of output3:


Related Solutions

Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must use var-arg concept.   Class name for this program is Palindrome.   Must pass this test:: public class PalindromeTest { public static void main(String[] args) { Palindrome palindrome = new Palindrome(); boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny"); System.out.println(“These are palindromes: " + answer); } }
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
In java: -Create a class named Animal
In java: -Create a class named Animal
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal...
Using Java Create the class RightTriangle which is a triangle with one of its angles equal to 90º – a “right angle”. Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem: a2 + b2 = c2. This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle. It will inherit equals...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT