-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteCipher.java
More file actions
92 lines (75 loc) · 2.16 KB
/
RouteCipher.java
File metadata and controls
92 lines (75 loc) · 2.16 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
/**
* Write a description of class RouteCipher here.
*
* @author Sofie Budman
* Period 5
* @version 3/10/25
*/
public class RouteCipher
{
// instance variables - replace the example below with your own
private String[][] letterBlock;
private int numRows;
private int numCols;
/* Constructor
* Postcondition: letterBlock has been instantiated. All instant variables have been initialized.
*/
public RouteCipher(int numRows, int numCols)
{
this.numRows = numRows;
this.numCols = numCols;
letterBlock = new String[numRows][numCols];
}
private void fillBlock(String str)
{
int s =0;
for(int i = 0; i < numRows; i++ ){
for(int j = 0; j < numCols; j ++ ){
if(s < str.length()){
letterBlock[i][j] = str.substring(s, s+1);
}else{
letterBlock[i][j] = "A";
}
s++;
}
}
}
private String encryptBlock()
{
String out = "";
for(int i = 0; i < numCols; i ++ ){
for(int j = 0; j < numRows; j ++ ){
out += letterBlock[j][i];
}
}
return out;
}
public String encryptMessage(String message)
{
String out = "";
fillBlock(message);
while(message.length() >= numRows*numCols){
fillBlock(message.substring(0, (numRows*numCols)));
out += encryptBlock();
message = message.substring(numRows*numCols);
}
fillBlock(message);
out += encryptBlock();
return out;
}
public void printLetterBlock()
{
for(int i=0;i<numRows;i++)
{
for(int j=0;j<numCols;j++)
System.out.print(letterBlock[i][j] + " ");
System.out.println();
}
}
public static void main(String[] args)
{
RouteCipher r = new RouteCipher(2,3);
System.out.println(r.encryptMessage("Meet at midnight"));
System.out.println("Expected: Mte eati dmnitgAhA");
}
}