nguyent063

MorseCode Java Assignment

Jan 29th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. /*
  2.  * Name: T. Nguyen
  3.  * Date: Jan. 26th, 2021
  4.  * Last updated: Jan. 26th, 2021
  5.  * Description: This class file contains the decoding/encoding methods to convert lines of Morse code to words, or vice versa.
  6.  */
  7. public class MorseCode
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         String[] text = {"* * * *    * *        _ _    * _ *    .        _ * *    .", "_ * _ _    _ _ _    * * _        _ _ *    _ _ _                           
  12.                         _ _ _    _ * *    ?",
  13.                         "* *        * _ * *    * *    _ * _    *        _    * * * *    * *    * * *    ."};
  14.         text = morseDecrypt(text);
  15.  
  16.         for (int i = 0; i < text.length; i++)
  17.         {
  18.             System.out.println(text[i]);
  19.         }
  20.     }//End main
  21.  
  22.     public static String[] morseDecrypt(String[] textArray)
  23.     {
  24.         //Declarations & Initializations
  25.         String[] decrypted = new String[textArray.length];
  26.         int morseCount = 0;
  27.  
  28.         for (int i = 0; i < textArray.length; i++)
  29.         {
  30.             textArray[i] = textArray[i].replaceAll("        ", "#%");
  31.             textArray[i] = textArray[i].replaceAll("    ", "#");
  32.             System.out.println(textArray[i]);
  33.  
  34.             decrypted[i] += morseLetDecrypt(textArray[i]);
  35.         }
  36.        
  37.         return decrypted;
  38.     }//End morseDecrypt()
  39.  
  40.     public static String morseLetDecrypt(String morseLine)
  41.     {
  42.         final String[] morseCodes = {"* _", "_ * * *", "_ * _ *", "_ * *", "*", "* * _ *", "_ _ *", "* * * *", "* *", "* _ _ _", "_ *                        
  43.                                     _", "* _ * *", "_ _",
  44.                                     "_ *", "_ _ _", "* _ _ *", "_ _ * _", "* _ *", "* * *", "_", "* * _", "* * * _", "*_ _", "_ * * _",          
  45.                                     "_ * _ _", "_ _ * *"};
  46.         final char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  47.                                 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  48.         String morseLetter = "";
  49.         int morseCount = 0;
  50.        
  51.         //Base Cases
  52.         if (morseLine.length() == 0)
  53.         {
  54.             return morseLine;
  55.         }
  56.  
  57.         //Recursive Code
  58.         if (morseLine.charAt(0) != '*' && morseLine.charAt(0) != '_' && morseLine.charAt(0) != ' ' && morseLine.charAt(0) != '#')
  59.         {
  60.             if (morseLine.charAt(0) == '%')
  61.             {
  62.                 return " " + morseLetDecrypt(morseLine.substring(1));
  63.             }
  64.             else
  65.             {
  66.                 return morseLine.charAt(0) + morseLetDecrypt(morseLine.substring(1));
  67.             }
  68.         }
  69.        
  70.         for (int i = 0; i < morseLine.length(); i++)
  71.         {
  72.             if (morseLine.charAt(i) == '*' || morseLine.charAt(i) == '_' || morseLine.charAt(i) == ' ')
  73.             {
  74.                 morseCount++;
  75.                 morseLetter += morseLine.charAt(i);
  76.             }
  77.             else
  78.             {
  79.                 break;
  80.             }
  81.         }
  82.        
  83.         for (int i = 0; i < morseCodes.length; i++)
  84.         {
  85.             if (morseLetter.equals(morseCodes[i]))
  86.             {
  87.                 return alphabet[i] + morseLetDecrypt(morseLine.substring(morseCount + 1));
  88.             }
  89.         }
  90.         return morseLetter + morseLetDecrypt(morseLine.substring(morseCount + 1));
  91.     }
  92. }//End class
Add Comment
Please, Sign In to add comment