Hello all,
I'm a beginner in the realm of Java and am currently learning it in school. I have a simple piece of software that is an example of simple word encryption (ie moving a letter from the right to the left aka pig-latin as well as replacing certain letters with others). I have two functions created, one for moving letters around the string that's entered and another that replaces certain letters with others. My problem is that I don't know how to run a string through BOTH functions. Conseqently you'd get "abcdefghigklmnopqrstuvwxyz" with the a moved to the end and vowels turned into consenants (as well as other letters changed). The code is posted below and the part I need help with is the function called "paranoidEncrypt". Can anyone give me an example of how I'm supposed to go about this?
Thanks,
Jisoo22
class StringEncrypt_1 {
public static void main( String [] args ){
if( args.length != 1 ){
System.out.println( "usage: java StringEncrypt_1 secret-message" );
return;
}
String original = args[0]; //uses whatever you gave as the command-line arg
StringEncrypt_1 the_object = new StringEncrypt_1();
//the_object.leftEncryptTest1( original );
//the_object.leftEncryptTest2( original );
//the_object.rightEncryptTest( original );
//the_object.replaceEncryptTest( original );
the_object.paranoidEncryptTest( original );
} //main
public String leftEncrypt( String s ){
return leftEncrypt( s, 1 ); //reduced body to 1 line!
} //leftEncrypt
public String leftEncrypt( String s, int i ){
return swapPieces( s, i );
} //leftEncrypt
public String rightEncrypt( String s, int i ){
return swapPieces2(s, i);
}//rightEncrypt (Calls method to move letters from the right and place them on the left.)
public String replaceEncrypt( String s ){
/* comment to describe how method works:
a <=> r
e <=> s
i <=> n
o <=> t
u <=> g
*/
String result = s; //just an alias
result = result.replace( 'a', '0' );
//System.out.println( "step1:" + result );
result = result.replace( 'r', 'a' );
//System.out.println( "step2:" + result );
result = result.replace( '0', 'r' );
result = result.replace( 'e', '1' );
result = result.replace( 's', 'e' );
result = result.replace( '1', 's' );
result = result.replace( 'i', '2' );
result = result.replace( 'n', 'i' );
result = result.replace( '2', 'n' );
result = result.replace( 'o', '3' );
result = result.replace( 't', 'o' );
result = result.replace( '3', 't' );
result = result.replace( 'u', '4' );
result = result.replace( 'g', 'u' );
result = result.replace( '4', 'g' );
return result;
}//replaceEncrypt (Replaces letters with other letters as designated.)
public String paranoidEncrypt( String s ){
String left_shift_1 = ??? //use leftEncrypt
String replaced = ??? //then pass results to replaceEncrypt
return replaced;
} //paranoidEncrypt
public String swapPieces( String s, int i ){
String left_piece = s.substring(0, i); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring(i); //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece.concat(left_piece); //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder; //step 4
} //swapPieces (Moves letters from the left to the right)
public String swapPieces2( String s, int i ){
String left_piece = s.substring(0, (s.length() - i)); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring((s.length() - i), s.length()); //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece + left_piece; //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder; //step 4
} //swapPieces2 (Moves letters from the right to the left)
//////////////// Begin Test Methods /////////////////////////
private void leftEncryptTest1( String clear ){
String encrypted = leftEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt1: " + encrypted );
System.out.println( ); //blank line
} //leftEncryptTest1
private void leftEncryptTest2( String clear ){
String encrypted2 = leftEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt2: " + encrypted2 );
System.out.println( );
String encrypted3 = leftEncrypt( clear, 3 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt3: " + encrypted3 );
System.out.println( );
} //leftEncryptTest2
private void rightEncryptTest( String clear ){
String encrypted2 = rightEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "rightEncrypt2:" + encrypted2 );
System.out.println( );
System.out.println( "Clear: " + clear );
String encrypted3 = rightEncrypt( clear, 3 );
System.out.println( "rightEncrypt3:" + encrypted3 );
System.out.println( );
} //rightEncryptTest
private void replaceEncryptTest( String clear ){
String encrypted = replaceEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "replEncrypt: " + encrypted );
System.out.println( );
} //replaceEncryptTest
private void paranoidEncryptTest( String clear ) {
String encrypted = paranoidEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "paranEncrypt: " + encrypted );
System.out.println( );
} //paranoidEncryptTest
} //StringEncrypt_1
I'm a beginner in the realm of Java and am currently learning it in school. I have a simple piece of software that is an example of simple word encryption (ie moving a letter from the right to the left aka pig-latin as well as replacing certain letters with others). I have two functions created, one for moving letters around the string that's entered and another that replaces certain letters with others. My problem is that I don't know how to run a string through BOTH functions. Conseqently you'd get "abcdefghigklmnopqrstuvwxyz" with the a moved to the end and vowels turned into consenants (as well as other letters changed). The code is posted below and the part I need help with is the function called "paranoidEncrypt". Can anyone give me an example of how I'm supposed to go about this?
Thanks,
Jisoo22
class StringEncrypt_1 {
public static void main( String [] args ){
if( args.length != 1 ){
System.out.println( "usage: java StringEncrypt_1 secret-message" );
return;
}
String original = args[0]; //uses whatever you gave as the command-line arg
StringEncrypt_1 the_object = new StringEncrypt_1();
//the_object.leftEncryptTest1( original );
//the_object.leftEncryptTest2( original );
//the_object.rightEncryptTest( original );
//the_object.replaceEncryptTest( original );
the_object.paranoidEncryptTest( original );
} //main
public String leftEncrypt( String s ){
return leftEncrypt( s, 1 ); //reduced body to 1 line!
} //leftEncrypt
public String leftEncrypt( String s, int i ){
return swapPieces( s, i );
} //leftEncrypt
public String rightEncrypt( String s, int i ){
return swapPieces2(s, i);
}//rightEncrypt (Calls method to move letters from the right and place them on the left.)
public String replaceEncrypt( String s ){
/* comment to describe how method works:
a <=> r
e <=> s
i <=> n
o <=> t
u <=> g
*/
String result = s; //just an alias
result = result.replace( 'a', '0' );
//System.out.println( "step1:" + result );
result = result.replace( 'r', 'a' );
//System.out.println( "step2:" + result );
result = result.replace( '0', 'r' );
result = result.replace( 'e', '1' );
result = result.replace( 's', 'e' );
result = result.replace( '1', 's' );
result = result.replace( 'i', '2' );
result = result.replace( 'n', 'i' );
result = result.replace( '2', 'n' );
result = result.replace( 'o', '3' );
result = result.replace( 't', 'o' );
result = result.replace( '3', 't' );
result = result.replace( 'u', '4' );
result = result.replace( 'g', 'u' );
result = result.replace( '4', 'g' );
return result;
}//replaceEncrypt (Replaces letters with other letters as designated.)
public String paranoidEncrypt( String s ){
String left_shift_1 = ??? //use leftEncrypt
String replaced = ??? //then pass results to replaceEncrypt
return replaced;
} //paranoidEncrypt
public String swapPieces( String s, int i ){
String left_piece = s.substring(0, i); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring(i); //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece.concat(left_piece); //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder; //step 4
} //swapPieces (Moves letters from the left to the right)
public String swapPieces2( String s, int i ){
String left_piece = s.substring(0, (s.length() - i)); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring((s.length() - i), s.length()); //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece + left_piece; //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder; //step 4
} //swapPieces2 (Moves letters from the right to the left)
//////////////// Begin Test Methods /////////////////////////
private void leftEncryptTest1( String clear ){
String encrypted = leftEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt1: " + encrypted );
System.out.println( ); //blank line
} //leftEncryptTest1
private void leftEncryptTest2( String clear ){
String encrypted2 = leftEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt2: " + encrypted2 );
System.out.println( );
String encrypted3 = leftEncrypt( clear, 3 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt3: " + encrypted3 );
System.out.println( );
} //leftEncryptTest2
private void rightEncryptTest( String clear ){
String encrypted2 = rightEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "rightEncrypt2:" + encrypted2 );
System.out.println( );
System.out.println( "Clear: " + clear );
String encrypted3 = rightEncrypt( clear, 3 );
System.out.println( "rightEncrypt3:" + encrypted3 );
System.out.println( );
} //rightEncryptTest
private void replaceEncryptTest( String clear ){
String encrypted = replaceEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "replEncrypt: " + encrypted );
System.out.println( );
} //replaceEncryptTest
private void paranoidEncryptTest( String clear ) {
String encrypted = paranoidEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "paranEncrypt: " + encrypted );
System.out.println( );
} //paranoidEncryptTest
} //StringEncrypt_1