Question

in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

in java

Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values:

front [3, 8, 17, 9, 17, 8, 3] back

Then the following call:

isPalindrome(q)

should return true because this sequence is the same in reverse order. If the list had instead stored these values:

front [3, 8, 17, 9, 4, 17, 8, 3] back

the call on isPalindrome would instead return false because this sequence is not the same in reverse order (the 9 and 4 in the middle don't match).

The empty queue should be considered a palindrome. You may not make any assumptions about how many elements are in the queue and your method must restore the queue so that it stores the same sequence of values after the call as it did before.

The method header is specified as follows:

public static boolean isPalindrome(IntQueue q)

To test your program, click to select the PalindromeTester.class and Run JUnit button Note: class name, variable name, and method names MUST be the same as specified.

Given files (for help)

IntQueue.txt

public class IntQueue{
Integer[] arr;
int numOfElements=0;
int front=0;
int rear=-1;

public IntQueue(int cap);

public void enqueue(Integer data);
public Integer dequeue();

public boolean isFull();

public boolean isEmpty();
}

IntStack.txt

methods document:

public class IntStack{
Integer[] arr;
int index=0;

public IntStack(int cap);

public void push(Integer data);
public Integer pop();
public Integer top();
public boolean isFull();

public boolean isEmpty();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//Palindrome.java

public class Palindrome {

    /**

    * required method. Assuming IntQueue and IntStack classes are defined

    * correctly and within the same package.

    *

    * @param q

    *            input queue of integers

    * @return true if numbers in q constitute a palindrome, else false

    */

    public static boolean isPalindrome(IntQueue q) {

         // initially assuming q is palindromic

         boolean pal = true;

         // creating a stack and a queue of size 100, assuming input q size is

         // never more than 100 (this is needed because the constructors in the

         // stack and queue classes you provided take size as parameter; if you

         // are having any issues with testing, then increase the size as you

         // need)

         IntStack stk = new IntStack(100);

         IntQueue que = new IntQueue(100);

         // looping until q is empty

         while (!q.isEmpty()) {

             // topping front element from q

             Integer value = q.dequeue();

             // adding to both stk and que

             stk.push(value);

             que.enqueue(value);

         }

         // now looping until que is empty

         while (!que.isEmpty()) {

             // fetching front value from que and top value from stk

             Integer value1 = que.dequeue();

             Integer value2 = stk.pop();

             // if they mismatch, setting pal to false (not a palindrome)

             if (!value1.equals(value2)) {

                 pal = false;

             }

             // adding value1 to q, so that after the loop, contents of q will

             // remain the same.

             q.enqueue(value1);

         }

         return pal;

    }

}

Add a comment
Know the answer?
Add Answer to:
in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...
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
  • Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To...

    Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is...

    IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is a palindrome or not. A palindrome is a phrase that reads the same forward​and backward; not including punctuation, spaces, or letter case. Some example palindromes include: Just the method please..

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • 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...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

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