4

I need to create a myStack class and then use it to test for Palindromes... I chose to create an ArrayList based implementation of the stack.

    import java.util.ArrayList;

    public class myStack<AnyType>{
        private ArrayList<AnyType> arr;

        public myStack(ArrayList<AnyType> a){
            arr = a;
        }

        public void push(AnyType element) {
                    arr.add(element);
        }

        public AnyType pop() {
            if(arr.size() == 0)
            {
                    System.out.println("Stack Underflow");
                    return null;
            }
            else
            {
                    AnyType element = arr.get(arr.size() -1);
                    arr.remove(element);
                    return element;
            }
        }

        public AnyType top() {
            if(arr.size() == 0)
            {
                    System.out.println("Stack Underflow");
                    return null;
            }
            else
                    return(arr.get(arr.size() -1));
        }
   }

Then I use it in a class called Palindrome (not complete yet)

However, when I compile, it tells me "Note: Palindrome.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details."

When I comment out the line myStack m = new myStack(test);

It compiles, so I know this is the problem... Why would the myStack class be the problem? Is it something to do with the "AnyType" I used in the stack class?

3 Answers3

1

Your class myStack. is generic with the template type <AnyType> and is currently using a Raw Type (hence the warning). Please follow Java naming conventions, and rename it MyStack. Then, use it with a type (and in Java 7 and up the diamond operator <>) -

// myStack m = new myStack(test);
myStack<Character> m = new myStack<>(); // <-- Please use MyStack.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

if you intend to use MyStack class for any of the type, you can use the wildcard "?".

So your MyStack will be unbounded wildcard type generic class, and therefore hold anything.

0

Ill add a little perspective.. not answering the question directly..

  1. What does stack have to do with palindrome?
  2. Generics is slightly-advanced/optional concept. There are more important things to learn first. So, fix it if you want.. but you can also ignore the warning.
  3. I would prefer to split the program into 3 aspects - (a) method to check if a single string is palindrome (b) method to read lines in a file (c) method to check if a string contains only digits and numbers
  4. Write separate methods to achieve each of them and get the best quality of code, for example by referring stackoverflow for each
  5. Wire them up together to get the final program working

IMHO... Java is vast and you need to consciously postpone learning stuff like Generics. Focus on these:

  1. Object oriented design - You could write a StringUtil and add a static method like checkPalindrome
  2. Variable and class naming convention (Java developers follow conventions almost to last letter)

I will suggest a structure for your program:

  • Your main class should be named as FilePalindromeChecker
  • Create a StringUtil class with a static method to check for palindrome: public static boolean checkPalindrome
  • Have another static method in StringUtil to check for alphanumeric: public static boolean checkPalindrome

Happy coding!

Teddy
  • 4,009
  • 2
  • 33
  • 55