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

Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction" Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "The number is a palindrome." If it is not then print "The number is NOT a palindrome" Make sure to use an array Allow input...
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?");...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
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,...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
In java: -Create a class named Animal
In java: -Create a class named Animal
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT