daniel_lawson9999

Exercises 3/7 and isPalindrome

May 9th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. //3
  2. public static void writeSequence(int n){//10-5 9-4 8-3 7-2
  3.         if(n<1){
  4.             throw new IllegalArgumentException("Value cannot be less than 1!");
  5.         }
  6.         if(n==1){
  7.             System.out.print("1 ");
  8.         }
  9.         else if(n==2){
  10.             System.out.print("1 1");
  11.         }
  12.         else {
  13.             int r = n - n / 2;
  14.             System.out.print(r +" ");
  15.             writeSequence(n-2);
  16.             System.out.print(r + " ");
  17.  
  18.         }
  19.     }
  20. //7
  21.     public static void writeChars(int n){
  22.         if(n<1){
  23.             throw new IllegalArgumentException("Value cannot be less than 1!");
  24.         }
  25.         if(n==1){
  26.             System.out.print("*");
  27.         }
  28.         else if(n==2){
  29.             System.out.print("**");
  30.         }
  31.         else{
  32.             System.out.print("<");
  33.             writeChars(n-2);
  34.             System.out.print(">");
  35.         }
  36.     }
  37.  
  38. //isPalindrome
  39.  public static boolean isPalindrome(String s){
  40.         if(s.length()<=1){
  41.             return true;
  42.         }
  43.         if(s.charAt(0)==s.charAt(s.length()-1)){
  44.             return (isPalindrome(s.substring(1,s.length()-1)));
  45.         }
  46.         else {
  47.             return false;
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment