-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorseCode.java
More file actions
157 lines (136 loc) · 4.05 KB
/
MorseCode.java
File metadata and controls
157 lines (136 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// =========================================================================
// CS 1113 Program 4 : Morse Code
// Semester : Fall 2016
//
// Andrew Bevelhymer
// 61049
//
// Description: Encodes or decodes morse code
// =========================================================================
// Import Scanner class
import java.util.Scanner;
public class MorseCode
{
// Creating Scanner object
public static Scanner scan = new Scanner(System.in);
// Creating array of english characters
public static String[] english = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0"};
// Creating array of morse characters
public static String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----"};
public static void main(String[] args)
{
// Printing info about program
System.out.println("Morse code program by Andrew Bevelhymer");
// if no argument, print usage statement
if (args.length < 1)
{
usage();
}
else
{
// run encode method if -e is used as the argument
if (args[0].equals("-e"))
{
encode();
}
// run decode method if -d is the argument
else if (args[0].equals("-d"))
{
decode();
}
// if anything else is used as an argument, print the usage statement
else
{
usage();
}
}
}
// Prints out a usage statement
public static void usage()
{
System.out.println("usage: java MorseCode [-e, -d] < input file > output file");
}
// Encodes (English to Morse)
public static void encode()
{
// Creating line
String line = scan.nextLine();
// Creating array of strings using a space as the delimiter
String[] character = line.split("");
// Loop through array
for (int i = 0; i < character.length; i++)
{
// getting letter
String letter = character[i];
letter = letter.toLowerCase();
// Searching through english array to see if letter matches any of them
for (int j = 0; j < english.length; j++)
{
// if the index equals the letter print its morse equivalent
if (letter.equals(english[j]))
{
System.out.print(morse[j] + " ");
break;
}
else if (letter.equals(" "))
{
System.out.print(" ");
break;
}
else
{
continue;
}
}
}
// Print blank line
System.out.println();
}
// Decodes (Morse to English)
public static void decode()
{
// Getting Line
String line = scan.nextLine();
// Creating string array splitting line using space as a delimiter
String[] wordArr = line.split(" ");
// Using nested loop to go through each letter in each word
for(int i = 0; i < wordArr.length; i++)
{
// Getting word
String word = wordArr[i];
// Getting each character from word and storing it in an array
String[] character = word.split(" ");
// Printing space after every word (not before first word though)
if (i > 0) System.out.print(" ");
// Loop through character array
for(int j = 0; j < character.length; j++)
{
// Defining letter variable
String letter = character[j];
// Loop through morse array
for (int k = 0; k < morse.length; k++)
{
// Check if k in morse equals the letter and print the english equivalent
if (letter.equals(morse[k]))
{
System.out.print(english[k]);
break;
}
else
{
continue;
}
}
}
}
// Print blank line
System.out.println();
}
}