Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. class PPJ_15_11_24_12_12539_FilipBartuzi {
  2.  
  3.     public static boolean isPal(char[] text, int index){
  4.       if(index > text.length / 2 - 1 )
  5.         return true;
  6.       if(text[index] == text[text.length-1-index])
  7.         return isPal(text, index+1);
  8.       else
  9.         return false;
  10.     }
  11.  
  12.  
  13.   static double fact(int x){
  14.      return (x <= 1) ? 1 : fact(x - 1) * x;
  15.   }
  16.  
  17.   static double factTail(int x, int accumulator) {
  18.     return (x <= 1) ? accumulator : factTail(x - 1, x * accumulator);
  19.   }
  20.  
  21.   static void reverser(char[] text, int index){
  22.     if (index <= (text.length/2 - 1)) {
  23.       char temp = text[index];
  24.       text[index] = text[text.length - 1 - index];
  25.       text[text.length - 1 - index] = temp;
  26.       reverser(text, index+1);
  27.     }
  28.   }
  29.  
  30.   static char nextCharacter(char c){
  31.     if(c == 'x')
  32.       return 'o';
  33.     else
  34.       return 'x';
  35.   }
  36.  
  37.   static boolean isDiagonal(int[][] arr){
  38.     for (int i = 0; i < arr.length; i++) {
  39.       if (arr[i].length != arr.length)
  40.         return false;
  41.       if (arr[i][i] != 0)
  42.         return false;
  43.     }
  44.     return true;
  45.   }
  46.  
  47.   static void encrypt(char[] text){
  48.     System.out.print("Encrypting "+ String.valueOf(text)  + "\nEncrypted: ");
  49.     for (int i = 0; i < text.length; i++) {
  50.       text[i] += i;
  51.       System.out.print(text[i]);
  52.     }
  53.     System.out.println();
  54.   }
  55.   static void decrypt(char[] cypher){
  56.     System.out.print("Decrypting " + String.valueOf(cypher)  + "\nDecrypted: ");
  57.     for (int i = 0; i < cypher.length; i++) {
  58.       cypher[i] -= i;
  59.       System.out.print(cypher[i]);
  60.     }
  61.     System.out.println();
  62.   }
  63.  
  64.   static void task3(char[] text){
  65.     System.out.println("Given text: " + String.valueOf(text));
  66.     encrypt(text);
  67.     decrypt(text);
  68.   }
  69.  
  70.   static void sqr(int x, char initialCharacter){
  71.     char actualCharacter = initialCharacter;
  72.     for(int i = 0; i < x; i++){
  73.       for(int j = 0; j < x; j++){
  74.         System.out.print(actualCharacter);
  75.         actualCharacter = nextCharacter(actualCharacter);
  76.       }
  77.       System.out.println();
  78.     }
  79.   }
  80.  
  81.   public static void main(String[] args) {
  82.     sqr(3, 'o');
  83.     int[][] arrayTrue = {{0,1,2}, {1,0,2}, {1,2,0}};
  84.     int[][] arrayFalse = {{1,1,2}, {1,0,2}, {1,2,0}};
  85.     System.out.println(isDiagonal(arrayTrue));
  86.     System.out.println(isDiagonal(arrayFalse));
  87.     char[] code = { 'a', 'b', 'c'};
  88.     task3(code);
  89.     System.out.println(fact(3));
  90.     fact(10);
  91.     factTail(10, 1);
  92.     char[] toReverser = { 'a', 'b', 'c', 'd'};
  93.     System.out.println("Before reverser: " + String.valueOf(toReverser));
  94.     reverser(toReverser, 0 );
  95.     System.out.println("After reverser: " + String.valueOf(toReverser));
  96.     char[] aPal = { 'a', 'b', 'b', 'a'};
  97.     char[] notAPal = { 'a', 'b', 'b', 'c'};
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement