Results 1 to 5 of 5
  1. #1
    kenson02 is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default can someone help

    Here is the question that i was ask: write a method isPalindrome() that returns true if the specified number reads the same forward and backward?

    here is the code that i wrote?

    public static boolean isPalindrome(String s) {
    if (s.length() == 0 || s.length() == 1)
    return true;
    if (s.charAt(0) == s.charAt(s.length() - 1))
    return isPalindrome(s.substring(1, s.length() - 1));
    return false;
    Is it correct?
    because on the Test class i find an error that says: cannot invoke isPalindrome() on primitive type int.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: can someone help

    You'll want to show us the line that causes the error. It looks like you're trying to call this method on an int when it is written to be called on a String. Thus the offending code is not the code you're showing us.

  3. #3
    kenson02 is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: can someone help

    Actually i will show the whole program.. first i wrote the MyInteger class myself and the TestMyInteger class was provide to me. My problem is what i told u on the first question.

    was provided

    public class TestMyInteger {
    public static void main(String[] args) {
    MyInteger n1 = new MyInteger(5);
    System.out.println("n1 is even? " + n1.isEven());
    System.out.println("n1 is prime? " + n1.isPrime());
    System.out.println("15 is prime? " + MyInteger.isPrime(15));

    char[] chars = {'3', '5', '3', '9'};
    System.out.println(MyInteger.parseInt(chars));

    String s = "3539";
    System.out.println(MyInteger.parseInt(s));
    int fib = MyInteger.fibonacci(50);

    MyInteger n2 = new MyInteger(24);
    System.out.println("n2 is odd? " + n2.isOdd());
    System.out.println("45 is odd? " + MyInteger.isOdd(45));
    System.out.println("n1 is equal to n2? " + n1.equals(n2));
    System.out.println("n1 is equal to 5? " + n1.equals(5));
    System.out.print("The Fibonacci number for 50 is" + MyInteger.fibonacci(50));
    System.out.print(", and the result is palindrome:" + MyInteger.fibonacci(50).isPalindrome());

    }
    }

    Wrote myself

    public class MyInteger{
    int value;

    MyInteger(int newValue) {
    value = newValue;
    }

    public int getValue() {
    return value;
    }

    public boolean isEven() {
    if (value % 2 == 0)
    return true;
    return false;
    }

    public boolean isOdd() {
    if (value % 2 != 0)
    return true;
    return false;
    }

    public boolean isPrime() {
    int factor = 0;
    for (factor = 2; factor <= value / 2; factor++) {
    if (value % factor != 0)
    return true;
    }
    return false;
    }

    public static boolean isEven(int value) {
    if (value % 2 == 0)
    return true;
    return false;
    }

    public static boolean isOdd(int value) {
    if (value % 2 != 0)
    return true;
    return false;
    }

    public static boolean isPrime(int value) {
    return isPrime(value);
    }

    public static boolean isEven(MyInteger myInt) {
    return MyInteger.isEven(myInt.getValue());
    }

    public static boolean isOdd(MyInteger myInt) {
    return MyInteger.isOdd(myInt.getValue());
    }

    public static boolean isPrime(MyInteger myInt) {
    return myInt.isPrime();
    }

    public boolean equals(int intValue) {
    return value == intValue;
    }

    public boolean equals(MyInteger myInt) {
    return equals(myInt.getValue());
    }
    char [] digits = { '1', '2', '3'};
    public static int parseInt(char[]digits) {
    return Integer.parseInt(new String (digits));
    }

    public static int parseInt(String s) {
    return Integer.parseInt(s);
    }

    public static int fibonacci(int n){
    if ((n == 0) || (n == 1))
    return n;
    else
    return fibonacci(n-2) + fibonacci(n-1);
    }

    public static boolean isPalindrome(String s) {
    if (s.length() == 0 || s.length() == 1)
    return true;
    if (s.charAt(0) == s.charAt(s.length() - 1))
    return isPalindrome(s.substring(1, s.length() - 1));
    return false;
    }
    }

  4. #4
    kenson02 is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: can someone help

    here is the line with the problem
    System.out.print(", and the result is palindrome:" + MyInteger.fibonacci(50).isPalindrome());

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: can someone help

    I don't see any isPalindrome method that allows for no parameters since there appears to be only one method, and it takes a String parameter. I suggest that you convert your int into a String, perhaps by using String.valueOf(...) and then pass this into the isPalindrome method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •