Palindrome

  • Palindrome = a word that is spelled the same way if you read the letters backwards

    Examples:

      mom      level      madam      racecar
      noon     kayak      radar      

  • Program assignment:

    • Write a Java program that reads in a word and prints out whether it is a palindrome or not.

    Sample outputs:

      level is a palindrome
      aabbaa is a palindrome
      abc is not a palindrome 

How to check if a word is a palindrome

  How to check if a string is a palindrome:  

         a b c d e d c b a  
         ^               ^
         |               | 
       1st letter     last letter  



Palindrome test: test1: 1st letter == last letter test2: 2nd letter == last-1 letter test3: 3rd letter == last-2 letter and so on !!! If all tests are passed ---> the string is a palindrome

Programming technique:   how to check if all required tests has been passed

  To check whether:      x pass all the tests test1, test2, ... , testk do:

     (1) Assume that x passes  // Initial assumption

     (2) For each test testi:
             check if x passes the test testi
             if x fails, then x did not pass all tests 


boolean passAllTest = true; // Initial assumption for ( each test "test i" ) { if ( x fails to pass test "test i" ) { passAllTest = false; // x did not pass all tests } } // Here: the variable passAllTest = true if all tests passed

How to check if a string passes all the palindrome tests

       Given a string x  (e.g.: "level" or "abc")

   x must pass these palindrome tests:

         1st character = last   character  
         2nd character = last-1 character  
         .... 
 

boolean isPalindrome = true; // Assume it is a palindrone for ( i = 0; i < x.length(); i++ ) { if ( character i in string x != character last - i in string x ) { isPalindrome = false; // Fail: x is not a palindrome } } // Unsolved problems: how to find character i in string x how to find character last - i in string x

  Refinement:   solving simpler problem(s) to find the solution  

  boolean isPalindrome = true;  // Assume it is a palindrone

  for ( i = 0; i < x.length(); i++ )
  {
     if ( character i in string x   !=   character last - i in string x )
     {
        isPalindrome = false;  // Fail: x is not a palindrome
     }
  }

  // Unsolved problems:  (1) how to find character i in string x
                         (2) how to find character last - i in string x


Refine the solution by solving the unsolved (simpler) problems: (1) how to find character i in string x: x.charAt(i) is the char in position i of string x (2) how to find character last - i in string x: x = "??????????????" x.length = # chars in string x ^ | index of last char = x.length() - 1

Java program that checks if a string passes all the palindrome tests

   // Why last = length() - 1:            "abcd"   length() = 4
   //                         index pos:   0123       
   //                                         ^ last =3 = 4 - 1 = length() - 1  

   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      
      String x = input.next();

      int i, last;       // Index of the characters that we are comparing

      last = x.length() - 1;

      boolean isPalindrome = true;    // Initial assumption

      for ( i = 0; i < x.length(); i++ )
      {
          if ( x.charAt( i )  !=  x.charAt( last-i ) )
          {
              isPalindrome = false;  // x is not a palindrome
          }
      }
      System.out.println(isPalindrome);
   } 

DEMO: demo/05-loops/08-alg-design/Palindrome.java

Improvement 1:   a palindrome is symmetric - we can stop half way in the string

   // Why last = length() - 1:            "abcd"   length() = 4
   //                         index pos:   0123       
   //                                         ^ last =3 = 4 - 1 = length() - 1  

   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      
      String x = input.next();

      int i, last;       // Index of the characters that we are comparing

      last = x.length() - 1;

      boolean isPalindrome = true;    // Initial assumption

      for ( i = 0; i < x.length()/2; i++ )
      {
          if ( x.charAt( i )  !=  x.charAt( last-i ) )
          {
              isPalindrome = false;  // x is not a palindrome
          }
      }
      System.out.println(isPalindrome);
   } 

DEMO: demo/05-loops/08-alg-design/Palindrome2.java

Improvement 2:   if some test failed, we can skip the remaining tests...

   // Why last = length() - 1:            "abcd"   length() = 4
   //                         index pos:   0123       
   //                                         ^ last =3 = 4 - 1 = length() - 1  

   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      
      String x = input.next();

      int i, last;       // Index of the characters that we are comparing

      last = x.length() - 1;

      boolean isPalindrome = true;    // Initial assumption

      for ( i = 0; i < x.length()/2; i++ )
      {
          if ( x.charAt( i )  !=  x.charAt( last-i ) )
          {
              isPalindrome = false;  // x is not a palindrome
              break;                 // Skip the remaining tests..
          }
      }
      System.out.println(isPalindrome);
   } 

DEMO: demo/05-loops/08-alg-design/Palindrome2b.java

Prime number

  • Prime number = an integer number that cannot be divided by other numbers except by  1  and the number by itself

    Examples:

      3    5    7    11    13    17    19   ... 

  • Program assignment:

    • Write a Java program that reads in an integer number and prints out whether it is a prime number or not.

    Sample outputs:

      11 is a prime number
      34 is not a prime number 

How to check if a number is a prime number

  How to check if a number is prime:

      n = 41

      Divide the number by:   2   3   4  ....   40 (= n-1)





Prime number tests: n%2 != 0 n%3 != 0 ... n%(n-1) != 0 If n passes all these tests, then n is a prime number

Review:   how to check if all required tests has been passed

  To check whether:      n pass the tests (test 1, test 2, ... , test k)

     (1) Assume that n passes  // Initial assumption

     (2) For each test test i:
             check if n passes the test test i
             if n fails, then n did not pass all tests 



boolean passAllTest = true; // Initial assumption for ( each test "test i" ) { if ( n fails to pass test "test i" ) { passAllTest = false; // n did not pass all tests } } // When the for-loop ends, "passAllTest = true" if n passes all the tests

How to check if a number passes all the prime number tests

       Given integer n

   n must pass these tests:

         n%2 != 0
         n%3 != 0
         ....
         n%(n-1) != 0


boolean isPrime = true; // Initial assumption for ( i = 2; i < n; i++ ) { if ( n%i == 0 ) { isPrime = false; // n is NOT a prime number } }

How to check if a number passes all the prime number tests

Java program to check if a number n is a prime number:

   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      
      int n = input.nextInt();
      boolean isPrime = true;
      int i;

      for ( i = 2; i < n; i++ )
      {
          if ( n%i == 0 )
          {
             isPrime = false;
            
          }
          
      }
      System.out.println(isPrime);
   }

DEMO: demo/05-loops/08-alg-design/IsPrime.java

How to check if a number passes all the prime number tests

Improvement:   if some test failed, we can skip the remaining tests...

   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);
      
      int n = input.nextInt();
      boolean isPrime = true;
      int i;

      for ( i = 2; i < n; i++ )
      {
          if ( n%i == 0 )
          {
             isPrime = false;
             break;
          }
          
      }
      System.out.println(isPrime);
   }

DEMO: demo/05-loops/08-alg-design/IsPrime2.java       (step demo with an even number !)