In: Computer Science
Using JAVA:
Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should be capitalized. Hint: StringBuilder might be useful as well as a set of parallel arrays or perhaps Enums.
You should use JOptionPane for your interactions with users.
Please make sure you comment your code thoroughly.
The code should be nicely formatted and should use proper variables.
Screenshot of the Code:
Sample Output:
Code to Copy:
import javax.swing.*;
//Define the class.
public class SimpleCode
{
//Define the constructor.
SimpleCode()
{
//Print the instruction.
JOptionPane.showMessageDialog(null,
"Enter numbers between 0 - 26 including both"
+" 0 for space\n 00 for
period."
+"\n Separate each number with space.",
"Instructions.",JOptionPane.INFORMATION_MESSAGE);
//Prompt the user for input.
String input =
JOptionPane.showInputDialog(null,"Enter numbers between(0 - 26):
");
//Split the number by spaces.
String sep[] = input.split("
");
//Define the variable.
String sen = "";
//Create the string array.
String rep[] = {" ", "z", "y", "x",
"w", "v", "u",
"t", "s", "r", "q", "p", "o", "n", "m",
"l",
"k", "j", "i", "h", "g", "f", "e", "d",
"c",
"b", "a"};
//Iterate through the input.
for(int i = 0; i < sep.length;
i++)
{
//Get the
integer value.
int v =
Integer.parseInt(sep[i]);
//Iterate
through the numbers.
for(int j = 0; j
<= 26; j++)
{
//If the input is valid.
if(v == j)
{
//Add space for 0.
if(i == 0)
sen +=
rep[j].toUpperCase();
//Add period for 00.
else if(sep[i].equals(
"00"))
sen +=
".";
//Add character for other
numbers.
else
sen +=
rep[j];
//Break the loop.
break;
}
}
}
//Display the resulting
string.
JOptionPane.showMessageDialog(null,
sen,"Sentence.", JOptionPane.INFORMATION_MESSAGE);
}
//Define the main method.
public static void main(String[] ss)
{
//Create the constructor of the
class.
new SimpleCode();
}
}