I cannot get the right output in the right place. Where is my problem?
this is the outpu that i am getting:
Code :
Transpose
The encrypted message is
wkh txlfn eurzq ira
The decrypted message is
the quick brown fox
The encrypted Transpose message is
wkh txlfn eurzq ira
The decripted Transpose message is
eht kciuq nworb xof <--------------------- I need this to be on encrypted Transpose and here it should be the decrypted message like on the first one
Reverser
The encrypted Reverse message is
xof nworb kciuq eht
The decrypted Reverse message is
qeb nrfzh yoltk clu <-------------------- I also need the decrypted message here like on the first one
I believe the problem is in my methods (loop), but i have tried every possible configuration of the loop and this is as close as I can get it to the right output. Here is the code of all the classes.
Code :
package assignment2;
public class Caeser extends Cipher
{
public Caeser(String s)
{
super(s);
}
public String encode(String word)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < word.length(); i ++)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants.ENCODE_SHIFT);
result.append(ch);
}
return result.toString();
}
public String decode(String word)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < word.length(); i ++)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants.DECODE_SHIFT);
result.append(ch);
}
return result.toString();
}
public char determineCharacter(char ch, int shift)
{
if(Character.isLowerCase(ch) || Character.isUpperCase(ch))
ch = (char)('a' + (ch - 'a' + shift) % Constants.WRAP_AROUND);
return ch;
}
}
Code :
package assignment2;
public class Transpose extends Cipher
{
Transpose(String s)
{
super(s);
}
public String encode(String word)
{
StringBuilder result = new StringBuilder();
for (int i= 0; i < word.length(); i++)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants.ENCODE_SHIFT);
result.append(ch);
}
return result.toString();
}
public String decode(String word)
{
StringBuilder result = new StringBuilder();
for (int i = word.length()-1;i >=0; i --)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants.DECODE_SHIFT);
result.append(ch);
}
return result.toString();
}
public char determineCharacter(char ch, int shift)
{
if(Character.isLowerCase(ch) || Character.isUpperCase(ch))
ch = (char)('a' + (ch - 'a' + shift) % Constants.WRAP_AROUND);
return ch;
}
}
Code :
package assignment2;
public class Reverser extends Transpose
{
Reverser(String s)
{
super(s);
}
String reverseText(String word)
{
StringBuilder result = new StringBuilder();
for (int i = word.length()-1;i >=0; i --)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants.DECODE_SHIFT);
result.append(ch);
}
return result.toString();
}
}
Re: I cannot get the right output in the right place. Where is my problem?
You need to try debugging the code by adding printlns to show the values of the variables as they change.
Where is the driver code that calls the other code and prints out the results?
Re: I cannot get the right output in the right place. Where is my problem?
Quote:
Originally Posted by
Norm
You need to try debugging the code by adding printlns to show the values of the variables as they change.
Where is the driver code that calls the other code and prints out the results?
This?
Code :
package assignment2;
import javax.swing.JOptionPane;
public class Main {
public static void main(String arg[]) {
String code, output = "";
String text = JOptionPane.showInputDialog("Enter message");
output += "The original message is \n" + text + "\n";
Cipher c = new Caeser(text);
c.encrypt();
code = c.getEncodedMessage();
output += "\nCeasar Cipher\nThe encrypted message is \n" + code + "\n";
c.decrypt(code);
code = c.getDecodedMessage();
output += "The decrypted message is \n" + code + "\n";
c = new Transpose(text);
c.encrypt();
code = c.getEncodedMessage();
output += "\nTranspose\nThe encrypted Transpose message is \n" + code + "\n";
c.decrypt(code);
code = c.getDecodedMessage();
output += "The decripted Transpose message is \n" + code + "\n";
Reverser r = new Reverser(text);
r.encrypt();
code = r.reverseText(r.getEncodedMessage());
output += "\nReverser\nThe encrypted Reverse message is \n" + code + "\n";
code = r.decode(code);
output += "The decrypted Reverse message is \n" + code;
System.out.println(output);
}
}
Re: I cannot get the right output in the right place. Where is my problem?
Your code does not compile. Its missing the Cipher class and the Constants class