Question
I keep getting this error when i try to test this part of my program.... Im trying to make this (below) happen and test it
I keep getting this error when i try to test this part of my program....
Im trying to make this (below) happen and test it using the tester given-------------------------------------------------------------------------
Encrypts a string according the Bellaso Cipher. Each character in plainText is offset
* according to the ASCII value of the corresponding character in bellasoStr, which is repeated
* to correspond to the length of plainText
* @param plainText an uppercase string to be encrypted.
* @param bellasoStr an uppercase string that specifies the offsets, character by character.
* @return the encrypted string
-------------------------------------------------------------------------------------
THE ERROR IS (below)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 13, length 7
at java.base/java.lang.String.checkBoundsBeginEnd(Unknown Source)
at java.base/java.lang.String.substring(Unknown Source)
at CryptoManager.encryptBellaso(CryptoManager.java:91)
at CryptoTest.main(CryptoTest.java:21)
The testing unit im using is below---------------------------------------------------------------------------------
public class CryptoTest {
public static void main(String[] args) {
String str1 = "\"THIS TEST SHOULD SUCCEED\"";
String str2 = "\"THIS TEST THAT SHOULD FAIL BECAUSE { IS OUTSIDE THE RANGE\"";
String str3 = "\"This test should fail because of lowercase letters\"";
String str4 = "THIS IS ANOTHER TEST";
String bellasoStr = "CMSC203";
String str5 = "WKLV#LV#DQRWKHU#WHVW";
String str6 = "WU\\VR9F#N!RF88U-'HED";
boolean good = CryptoManager.stringInBounds(str1);
boolean bad = CryptoManager.stringInBounds(str2);
System.out.println(str1+" Is it in bounds? "+good);
System.out.println(str2+" Is it in bounds? "+bad);
bad = CryptoManager.stringInBounds(str3);
System.out.println(str3+" Is it in bounds? "+bad);
System.out.println("Caesar cipher of \""+str4+"\" should return \"WKLV#LV#DQRWKHU#WHVW\": "+ CryptoManager.encryptCaesar(str4, 3));
System.out.println("Bellaso cipher of \""+str4+"\" should return \"WU\\VR9F#N!RF88U-'HED\": "+ CryptoManager.encryptBellaso(str4, bellasoStr));
System.out.println("Caesar decryption of \""+str5+"\" should return \"THIS IS ANOTHER TEST\": "+ CryptoManager.decryptCaesar(str5, 3));
System.out.println("Bellaso decryption of \""+str6+"\" should return \"THIS IS ANOTHER TEST\": "+ CryptoManager.decryptBellaso(str6, bellasoStr));
}
}
ANd this is the part causing the problem-----------------------------------------------------------------------------
.....
private static final char LOWER_BOUND = ' ';
private static final char UPPER_BOUND = '_';
private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1;
public static String encryptBellaso(String plainText, String bellasoStr) {
//encrypted text
String res="";
//Adjust length of bellasoStr to plainText
while(bellasoStr.length() { bellasoStr+=bellasoStr.substring(0,(plainText.length()-bellasoStr.length())); } //encryption for(int i=0;i { char c=(char)Wrap_around((int)plainText.charAt(i)+(int)bellasoStr.charAt(i) ); res+=Character.toString(c); } //return result return res; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started