Palindromes help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tmoney
    New Member
    • Mar 2007
    • 3

    Palindromes help!!

    hello any guidance on this would be greatly appreciated( im very new to programming the more comments the better)

    the objective is to create a class that defines a single String data member. The class should define a isPalindrome method which returns true when the String is a Palindrome and false otherwise. The class's toString method should return the String data member along with the result of the isPalindrome method is a readable format.

    then you need a client application and its responsible for retrieving a word or sentence from the user, passing the input String to the class and calling the toString method. The client should allow the user to test any number of strings.

    Heres what i got so far:

    import java.lang.Strin g;
    import java.lang.Objec t;


    public class Palindrome
    {
    private String palin;

    public Palindrome()
    {
    palin = " ";
    }

    public Palindrome(Stri ng newPalin, String initPalin)
    {
    palin = initPalin.toUpp erCase();
    palin = newPalin;

    }

    public String getPalin()
    {
    return palin;
    }

    public void setPalin(String newPalin)
    {
    palin = newPalin;

    }

    public static boolean isPalindrome(St ring palin)
    {
    int left = 0;
    int right = palin.length() -1;
    while (left < right)
    {
    if (palin.charAt(l eft) != palin.charAt(ri ght))
    {
    return false;
    }
    left++;
    right--;
    }
    return true;
    }

    public String toString()
    {
    return isPalindrome();
    }

    }


    Thats ^^ the standard class ( which is incomplete) and as far as my client goes this is where im confused and cant figure out how to do any of it

    import java.util.Scann er;
    import java.lang.Strin g;

    public class palinClient
    {
    public static void main(String[] args)
    {
    String entry;

    Scanner scan = new Scanner(System. in);
    Palindrome palin1 = new Palindrome();

    System.out.prin tln("Please enter your expression");
    entry = scan.next();

    palin1.setPalin (entry);

    }
    }

    Ty for the Help!!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    So, is the test program you wrote working? Is it making the palindrome object correctly? Try printing out the object's toString() result to check. Then, work on a whole() loop that will continue making Palindrome objects until the user wants to stop.

    Comment

    • Tmoney
      New Member
      • Mar 2007
      • 3

      #3
      Originally posted by Ganon11
      So, is the test program you wrote working? Is it making the palindrome object correctly? Try printing out the object's toString() result to check. Then, work on a whole() loop that will continue making Palindrome objects until the user wants to stop.

      Well, currently I'm having trouble compiling the program for instance here are some of my errors:



      Palindrome.java :50: isPalindrome(ja va.lang.String) in Palindrome cannot be applied to ()
      return isPalindrome();
      ^
      1 error

      thats ^ is in the Palindrome class at the bottom i dont understand why it wouldnt compile correctly

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You have written the isPalindrome method to take a String argument, but in your .toString() method, you try to call isPalindrome without arguments.

        The method should not accept an argument, but instead try to find out if palin (the instance variable) is a palindrome.

        Comment

        • Tmoney
          New Member
          • Mar 2007
          • 3

          #5
          Originally posted by Ganon11
          You have written the isPalindrome method to take a String argument, but in your .toString() method, you try to call isPalindrome without arguments.

          The method should not accept an argument, but instead try to find out if palin (the instance variable) is a palindrome.

          Okay things are starting to look good still having a little trouble getting it all finished this is what i have now

          import java.lang.Strin g;
          import java.lang.Objec t;


          public class Palindrome
          {
          private String palin;

          public Palindrome()
          {
          palin = " ";
          }

          public Palindrome(Stri ng newPalin, String initPalin)
          {
          palin = initPalin.toUpp erCase();
          palin = newPalin;

          }

          public String getPalin()
          {
          return palin;
          }

          public void setPalin(String newPalin)
          {
          palin = newPalin;

          }

          public boolean isPalindrome()
          {
          int left = 0;
          int right = palin.length() -1;
          while (left < right)
          {
          if (palin.charAt(l eft) != palin.charAt(ri ght))
          {
          return false;
          }
          left++;
          right--;
          }
          return true;
          }

          public String toString()
          {
          return "Palindrome " + isPalindrome();
          }

          }


          then the client class

          import java.util.Scann er;
          import java.lang.Strin g;

          public class palinClient
          {
          public static void main(String[] args)
          {
          String palin;
          boolean isP = false;

          Scanner scan;
          scan = new Scanner(System. in);

          System.out.prin tln("Enter text and press [enter]:");
          String input = scan.nextLine() ;
          Palindrome text = new Palindrome(pali n);

          isP = text.isPalindro me();

          if(isP)
          System.out.prin tln(input + " is a palindrome");
          else System.out.prin tln(input + " is not a palindrome");
          }

          }


          But now my biggest problem is that it reports every word as being a Palindrome, whether its right or wrong

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Was that palindrome solution given to you, or did you have to write it yourself? It looks correct, but I usually use a different type of function to check palindromes.

            Also, when posting your code, please be sure to use the [CODE] tags to make it easier to read. Just select the coded text and hit the # symbol in the editing box when making your reply.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Tmoney
              Okay things are starting to look good still having a little trouble getting it all finished this is what i have now

              import java.lang.Strin g;
              import java.lang.Objec t;


              public class Palindrome
              {
              private String palin;

              public Palindrome()
              {
              palin = " ";
              }

              public Palindrome(Stri ng newPalin, String initPalin)
              {
              palin = initPalin.toUpp erCase();
              palin = newPalin;

              }

              public String getPalin()
              {
              return palin;
              }

              public void setPalin(String newPalin)
              {
              palin = newPalin;

              }

              public boolean isPalindrome()
              {
              int left = 0;
              int right = palin.length() -1;
              while (left < right)
              {
              if (palin.charAt(l eft) != palin.charAt(ri ght))
              {
              return false;
              }
              left++;
              right--;
              }
              return true;
              }

              public String toString()
              {
              return "Palindrome " + isPalindrome();
              }

              }


              then the client class

              import java.util.Scann er;
              import java.lang.Strin g;

              public class palinClient
              {
              public static void main(String[] args)
              {
              String palin;
              boolean isP = false;

              Scanner scan;
              scan = new Scanner(System. in);

              System.out.prin tln("Enter text and press [enter]:");
              String input = scan.nextLine() ;
              Palindrome text = new Palindrome(pali n);

              isP = text.isPalindro me();

              if(isP)
              System.out.prin tln(input + " is a palindrome");
              else System.out.prin tln(input + " is not a palindrome");
              }

              }


              But now my biggest problem is that it reports every word as being a Palindrome, whether its right or wrong
              1.)Use code tags when posting code
              2.)You don't have to import classes in the java.lang package
              3.)You have two constructors in your Palindrome class :

              Code:
               public Palindrome() 
              	{
              		palin = " ";
              	}
              takes no arguments and

              Code:
               public Palindrome(String newPalin, String initPalin) 
              	{
              		palin = initPalin.toUpperCase();
              		palin = newPalin;
               
              	}
              takes two arguments but in your test class you have the line
              Code:
               Palindrome text = new Palindrome(palin);
              which call a one argument constructor. This of course will not compile so show us the code that you are using that is compiling and give the incorrect results.

              Comment

              Working...