Question

In: Computer Science

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

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)


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();
}

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();

}

are the available methods, and we do not have size() or length() which is how i have done these types of problems before.

Solutions

Expert Solution

public static boolean isPalindrome(IntQueue q)
{
   //FIRST PUSHING ALL ELEMENTS IN Q TO STACK AND ANOTHER QUEUE
   IntQueue temp = new IntQueue(q.numOfElements);
   IntStack s = new IntStack(q.numOfElements);
  
   while(!q.isEmpty())
   {
       Integer t = q.dequeue();
       temp.enqueue(t);
       s.push(t);  
   }
  
   //now checking palindrome or not
   boolean pal=true;
   while(!temp.isEmpty())
   {
       Integer t1 = temp.dequeue();
       Integer t2 = s.pop();
       if(t1!=t2)
       pal=false;//means not a palindrome
       //restoring previous queue
       q.enqueue(t1);
   }
   return pal;
}


Related Solutions

JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Question: Write a method named reduce that: ● Takes a function of type (A, A) =>...
Question: Write a method named reduce that: ● Takes a function of type (A, A) => A ● Returns A ● Combines all the elements of the list into a single value by applying the provided function to all elements ○ You may assume the function is commutative ● If the list has size 1, return that element without calling the provided function Example: If head stores a reference to the List(4, 6, 2) head.reduce((a: Int, b: Int) => a...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler. iterator: returns an...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
Complete the class code below and include a method named partition which takes an integer array...
Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer. The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT