Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone fix my code? It is java. It is supposed to read lunch and dinner and

ID: 663755 • Letter: C

Question

Can someone fix my code? It is java. It is supposed to read lunch and dinner and tell you if it is a palindrome or not

I keep getting these errors:

TestGeneric.java:31: error: type ArrayList does not take parameters
ÏÏ§Ï public static boolean isPalindrome(ArrayList list)
ÏÏ§Ï ^
ϼ§ÏTestGeneric.java:13: error: type ArrayList does not take parameters
ÏÏ§Ï ArrayList aVal = new ArrayList ();
ÏÏ§Ï ^
ϼ§ÏTestGeneric.java:13: error: type ArrayList does not take parameters
ÏÏ§Ï ArrayList aVal = new ArrayList ();
ÏÏ§Ï ^
ϼ§ÏTestGeneric.java:35: error: type ArrayList does not take parameters
ÏÏ§Ï ArrayList al = new ArrayList (list.subList(0, list.size()));
ÏÏ§Ï ^
ϼ§ÏTestGeneric.java:35: error: type ArrayList does not take parameters
ÏÏ§Ï ArrayList al = new ArrayList (list.subList(0, list.size()));
ÏÏ§Ï ^
ÏϧÏ5 errors

Here is my code:


import java.util.*;
public class TestGeneric
{
   public static void main(String args[])
   {
   ArrayList aVal = new ArrayList ();
   aVal.add("lunch");
   aVal.add("dinner");

   if(isPalindrome (aVal))
   {
   System.out.println("Word in the list is a palindrome.");
   }
   else
   {
   System.out.println("Word in the list is not a palindrome.");
   }
   Iterator itr1 = aVal.iterator();
   while(itr1.hasNext())
   {
   System.out.println(itr1.next());
   }
   }
   public static boolean isPalindrome(ArrayList list)
   {
   if(list.size() == 0 || list.size() == 1)
   return true;
ArrayList al = new ArrayList (list.subList(0, list.size()));
   return list.get(0).equals(list.get(list.size()-1)) && isPalindrome(al);
   }
}

Explanation / Answer

Hi,

Your code is working fine and doesn't getting any errors. There might be chances that your java version is less that 1.5 and may be your using java.awt.* in your code or you java version not support generics.

You are using generics  ArrayList aVal = new ArrayList ();

Try to avoid that, and use like  ArrayList< String > words = new ArrayList< String >();

Because your java version doesn't support this feature of generics. Whenever you uses ArrayList please try to use as string.type every where in your code.

And your code looks like not generic if you change the input it won't work because you are calling isPalindrome method and passing the same arraylist which is never ending story.

I am providing sample generic code which might be helpful to you.

/*Program to find a palidrome*/

import java.util.*;
public class Palindrome
{
public static void main(String[] args)
{
ArrayList< String > words = new ArrayList< String >();

words.add("lunch");
words.add("dinner");
    System.out.println("Is it a palindrome? " +
isPalindrome(words));
}
static boolean isPalindrome(ArrayList< String > w)
{
int front = 0, back = w.size() - 1;
while( front < back)
if (!w.get(front++).equals(w.get(back--)))
return false;
return true;
}

==================================

Result :---

Is it a palindrome? false.