Question

In: Computer Science

Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a...

Create a Java method findLongestPalindrome that takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: Use the built in isPalindrome method. This method calls for an optimization loop.)

Where the isPalindrome Method takes a String s as a parameter and returns a boolean. It returns true if s reads the same forwards and backwards, i.e., is a Palindrome, and returns false otherwise.

I only need help creating the findLongestPalindrome method, I already have the isPalindrome method.

Solutions

Expert Solution


import java.util.Scanner;

public class LongestPalindrome {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Longest Palindrome: "+findLongestPalindrome(sc));
   }

   // takes series of words from user
   //returns longest palindrome among them
   public static String findLongestPalindrome(Scanner sc) {
       String s="";
       String longestPal="";
       while(!s.equalsIgnoreCase("Quit")){
           s=sc.nextLine();
           if(isPalindrome(s)){
               if(s.length()>longestPal.length())
                   longestPal=s;
           }
       }
       return longestPal;
   }

  
   //checks given string is palindrome or not
   public static boolean isPalindrome(String s) {
       char aC[]=s.toCharArray();
       int i=0,j=s.length()-1;
       //iterating from the both ends
       while(i<j){
           if(aC[i]!=aC[j]){
               return false;
           }
           i++;j--;
       }
       return true;
   }
}


Related Solutions

In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
IN JAVA A recursive method that takes a String as its argument and returns a list...
IN JAVA A recursive method that takes a String as its argument and returns a list of Strings which includes all anagrams of the String. This method will contain a loop that generates each possible substring consisting of all but one character in the input String, ie the substring that omits the first letter, then the substring that omits the second letter, etc. Within the loop, the method calls itself recursively for each substring. For each String in the list...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Please use JAVA to do this: Write a method that takes four strings as parameter. The...
Please use JAVA to do this: Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type(either fire, water, or leaf, where water beats fire, fire beats leaf and leaf beats water), the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. Example: Pokemon X(which is the fire type) has the...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Create your own function in Java that accepts one input parameter and returns a float number....
Create your own function in Java that accepts one input parameter and returns a float number. You decide the theme.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT