-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodes.java
More file actions
46 lines (39 loc) · 1.38 KB
/
Codes.java
File metadata and controls
46 lines (39 loc) · 1.38 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
public class Codes
{
/**
* Encode and decode a message using a key of values stored in
* a queue.
*/
public static void main ( String[] args)
{
int[] key = {5, 12, -3, 8, -9, 4, 10};
Integer keyValue;
String encoded = "", decoded = "";
String message = "All programmers are playwrights and all " +
"computers are lousy actors.";
CircularArrayQueue<Integer> keyQueue1 = new CircularArrayQueue<Integer>();
CircularArrayQueue<Integer> keyQueue2 = new CircularArrayQueue<Integer>();
/** load key queue */
for (int scan=0; scan < key.length; scan++)
{
keyQueue1.enqueue (new Integer(key[scan]));
keyQueue2.enqueue (new Integer(key[scan]));
}
/** encode message */
for (int scan=0; scan < message.length(); scan++)
{
keyValue = keyQueue1.dequeue();
encoded += (char) ((int)message.charAt(scan) + keyValue.intValue());
keyQueue1.enqueue (keyValue);
}
System.out.println ("Encoded Message:\n" + encoded + "\n");
/** decode message */
for (int scan=0; scan < encoded.length(); scan++)
{
keyValue = keyQueue2.dequeue();
decoded += (char) ((int)encoded.charAt(scan) - keyValue.intValue());
keyQueue2.enqueue (keyValue);
}
System.out.println ("Decoded Message:\n" + decoded);
}
}