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 Using the java.util.Calendar class, create 3 dates and times. Write a method to identify and...
JAVA Using the java.util.Calendar class, create 3 dates and times. Write a method to identify and print out the oldest of the 3 timestamps. Likewise, create another method that prints out the most recent time.
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?");...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome. If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object. The constructor will use a single stack to evaluate if the String is a palindrome. You must...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT