-
Notifications
You must be signed in to change notification settings - Fork 0
/
cshift.java
45 lines (40 loc) · 1.46 KB
/
cshift.java
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
import java.util.*;
//Arjun Rajesh Khamkar
//10/08/2024
//CSE 123
// This class uses the CaesarKey and the substitution encryption algorithm to encode
// and decode the given text.
public class CaesarShift extends Substitution{
//Constructs a CaesarShift object using the passed shift value.
//Exception:
// - if shift value is negative or zero, then throws IllegalArgumentException.
public CaesarShift(int shift){
if(shift <= 0){
throw new IllegalArgumentException();
}
Queue<Character> shiftQueue = new LinkedList<>();
for(int i= MIN_CHAR; i<=MAX_CHAR; i++){
shiftQueue.add((char)i);
}
super.setShifter( setCaesarShiftShifter(shiftQueue, shift) );
}
//Behavior:
// This helper method creates the CaesarShiftShifter string using the shift value.
//Returns:
// - String: the shifter string which can be used for encoding/decoding using the substitution algorithm.
//Exceptions:
// - if queue is empty, throws IllegalStateException()
private String setCaesarShiftShifter(Queue<Character> queue, int shift) {
if(queue == null){
throw new IllegalStateException();
}
String shifter = "";
for (int i = 0; i < shift; i++) {
char frontChar = queue.remove();
queue.add(frontChar);
}
for (char c : queue) {
shifter = shifter + c;
} return shifter;
}
}