4 Replies - 1715 Views - Last Post: 11 October 2008 - 01:02 PM Rate Topic: -----

#1 peoples06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 08

Java code not compiling

Posted 10 October 2008 - 04:57 PM

I know this is probably an easy fix, but I need help on my Java homework. In it, we're supposed to find the prime, perfect, and binary palindromes of a set of numbers from zero to MAX_NUMBER. I've got the perfect and the primes down, but the binary palindromes are tripping me up. I've got it written how I think it should go, but I get an error before compiling it. The error occurs under the listPalindromes method, specifically on the "if (isPalindrome(upperBound)){" line. It is telling me 'The method isPalindrome(String) in the type PrimePerfectPalindrome is not applicable for the arguments (int).' Again, I know this is probably simple, but I haven't been able to figure it out myself. Also, if anybody knows how to get the binary representation using the modulo operation and putting it together, any help would be appreciated. Thanks for your help.

package primePerfectPalindrome;

public class PrimePerfectPalindrome {

	/** Main method */
	public static void main(String []args) {

		//Set up the maximum number
		final int MAX_NUMBER = 100;

		//List all primes to MAX_NUMBER
		listPrimes(MAX_NUMBER);

		//List all perfects to MAX_NUMBER
		listPerfects(MAX_NUMBER);

		//List all binary palindromes
		listPalindromes(MAX_NUMBER);
	}

	/** listPrimes method */
	public static void listPrimes(int upperBound) {
		System.out.println("2 is prime.");
		for (int index = 3; index <= upperBound; index += 2){
			//Test if index is prime
			if (isPrime (index)){
				System.out.printf(index + " is prime.\n");
			}
		}
	}
	/** isPrime method */
	public static boolean isPrime(int number) {
		for (int index = 2; index <= Math.sqrt(number); index ++){
			if (number % index == 0){
				return false;
			}
		}
		return true;
	}
	/** listPerfects method */
	public static void listPerfects(int upperBound) {
		for (int index = 2; index < upperBound; index ++){
			//Test if index is perfect
			if (isPerfect(index)){
				System.out.println(index + " is perfect.");
			}
		}
	}
	/**isPerfect method */
	public static boolean isPerfect(int number) {
		int sumOfDivisors = 1;
		for (int divisor = 2; divisor < number - 1; divisor ++){
			if (number % divisor == 0){
				sumOfDivisors += divisor;
			}
		}
		if (sumOfDivisors == number){ 
			return true;
		}
		else{
			return false;
		}		
	}
	/**listPalindromes method */
	public static void listPalindromes(int upperBound) {
		for (int index = 0; index <= upperBound; index ++){
			if (isPalindrome(upperBound)){
				System.out.println(index + " is a binary palindrome.");
			}
		}
	}
	/**isPalindrome method */
	public static boolean isPalindrome(String binary) {
		int low = 0;
		int high = binary.length () - 1;
		while (low < high) {
			if (binary.charAt(low) != binary.charAt(high))
				return false;
			low ++;
			high ++;
		}
		return true;
	}
	/**intToBinaryString method */
	public static String intToBinaryString(int number) {
		String binary = Integer.toBinaryString(number);
		return binary;

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java code not compiling

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Java code not compiling

Posted 10 October 2008 - 05:38 PM

The reason you get that compilation error is because your isPalindrome() method wants a String as the variable passed to it. Right now, you're passing upperBound, which is an int, to it.

Simply change that if line to this...

if (isPalindrome(Integer.toString(upperBound)){


I think that should solve that problem. :D

This post has been edited by Locke37: 10 October 2008 - 05:41 PM

Was This Post Helpful? 1
  • +
  • -

#3 peoples06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 08

Re: Java code not compiling

Posted 10 October 2008 - 10:47 PM

View PostLocke37, on 10 Oct, 2008 - 05:38 PM, said:

The reason you get that compilation error is because your isPalindrome() method wants a String as the variable passed to it. Right now, you're passing upperBound, which is an int, to it.

Simply change that if line to this...

if (isPalindrome(Integer.toString(upperBound)){


I think that should solve that problem. :D


Hey, thanks for your help. The code compiled, but it didn't list the binary palindromes. Any advice on this problem?
Was This Post Helpful? 0
  • +
  • -

#4 stauffski   User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 216
  • Joined: 03-November 07

Re: Java code not compiling

Posted 10 October 2008 - 11:22 PM

Great stuff, only thing you need to change is as follows:

public static void listPalindromes(int upperBound) {
    for (int index = 0; index <= upperBound; index ++){
        if (isPalindrome(Integer.toString(upperBound))){ //<----- change "upperBound" to "index"
            System.out.println(index + " is a binary palindrome.");
        }
    }
}



public static boolean isPalindrome(String binary) {
    int low = 0;
    int high = binary.length () - 1;
    while (low < high) {
        if (binary.charAt(low) != binary.charAt(high))
            return false;
        low ++;
        high ++; //<------ change "high++" to "high--";
    }
    return true;
}


This post has been edited by stauffski: 10 October 2008 - 11:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 peoples06   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 08

Re: Java code not compiling

Posted 11 October 2008 - 01:02 PM

Well, I've got one more problem with my code. Never knew this would be this difficult. The code compiles and works for the most part, but the palindromes it returns are decimal palindromes, not binary ones. Again, it is probably an easy fix, but I can't find it. I'm doing it like my book is telling me to. Any help would be appreciated.

package primePerfectPalindrome;

public class PrimePerfectPalindrome {

	/** Main method */
	public static void main(String []args) {

		//Set up the maximum number
		final int MAX_NUMBER = 100;

		//List all primes to MAX_NUMBER
		listPrimes(MAX_NUMBER);

		//List all perfects to MAX_NUMBER
		listPerfects(MAX_NUMBER);

		//List all binary palindromes
		listPalindromes(MAX_NUMBER);
	}

	/** listPrimes method */
	public static void listPrimes(int upperBound) {
		System.out.println("2 is prime.");
		for (int index = 3; index <= upperBound; index += 2){
			//Test if index is prime
			if (isPrime (index)){
				System.out.printf(index + " is prime.\n");
			}
		}
	}
	/** isPrime method */
	public static boolean isPrime(int number) {
		for (int index = 2; index <= Math.sqrt(number); index ++){
			if (number % index == 0){
				return false;
			}
		}
		return true;
	}
	/** listPerfects method */
	public static void listPerfects(int upperBound) {
		for (int index = 2; index < upperBound; index ++){
			//Test if index is perfect
			if (isPerfect(index)){
				System.out.println(index + " is perfect.");
			}
		}
	}
	/**isPerfect method */
	public static boolean isPerfect(int number) {
		int sumOfDivisors = 1;
		for (int divisor = 2; divisor < number - 1; divisor ++){
			if (number % divisor == 0){
				sumOfDivisors += divisor;
			}
		}
		if (sumOfDivisors == number){ 
			return true;
		}
		else{
			return false;
		}		
	}
	/**listPalindromes method */
	public static void listPalindromes(int upperBound) {
		for (int index = 0; index <= upperBound; index ++){
			if (isPalindrome(Integer.toString(index))){
				System.out.println(index + " is a binary palindrome.");
			}
		}
	}
	/**isPalindrome method */
	public static boolean isPalindrome(String binary) {
		int low = 0;
		int high = binary.length () - 1;
		while (low < high) {
			if (binary.charAt(low) != binary.charAt(high))
				return false;
			low ++;
			high --;
		}
		return true;
	}
	/**intToBinaryString method */
	public static String intToBinaryString(int number) {
		String binary = "";
		while (number > 0){
			if (number % 2 == 0){
				binary = 0 + binary;				
			}
			else{
				binary = 1 + binary;
			}
		}
		return binary;
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1