1 Attachment(s)
Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Hi there, this is an assignment that I am working on...
Attachment 850
this is my solution but for some UNLOGICAL REASON THE CODE DOESN'T MANAGE ANYTHING BEYOND 6 LETTERS... :S :S :S
for instance try
encoding("encrypt" , {1,4,3}, {3,2,4}); // now remove the t and run it, it works just fine :S
BTW StackObj and QueueObj are stack of Objects and Queue of Objects...
any hint would be quite helpful thank you...
Code :
public static StackObj reverse(StackObj x){
int size = x.size();
StackObj temp1 = new StackObj(size);
StackObj temp2 = new StackObj(size);
while(!x.isEmpty()){
temp1.push(new Integer(((Integer)x.pop()).intValue()));
}
while(!temp1.isEmpty()){
temp2.push(new Integer(((Integer)temp1.pop()).intValue()));
}
while(!temp2.isEmpty()){
x.push(new Integer(((Integer)temp2.pop()).intValue()));
}
return x;
}
public static String encoding(String message, int[] key1, int[] key2){
QueueObj Qforkey1 = new QueueObj(key1.length);
for(int i = 0; !Qforkey1.isFull() ; i++){
Qforkey1.enqueue(new Integer (key1[i]));
}
StackObj Sforkey2 = new StackObj(key2.length);
StackObj tempSforkey2 = new StackObj(key2.length);
for(int i = key2.length-1; !Sforkey2.isFull() ; i--){
Sforkey2.push(new Integer(key2[i]));
tempSforkey2.push(new Integer(key2[i]));
}
String EncodedMessage = "";
for(int i = 0; i < message.length(); i++){
int x = ((Integer)Qforkey1.dequeue()).intValue();
int y = ((Integer)Sforkey2.pop()).intValue();
int offset = x + y;
Qforkey1.enqueue(new Integer (x));
if(Sforkey2.isEmpty()){
Sforkey2 = reverse(tempSforkey2);
}
if((message.charAt(i) >= 97) && (message.charAt(i) <= 122)){
int encryptedchar = message.charAt(i) + offset;
if(encryptedchar > 122){
encryptedchar -= 122;
encryptedchar += 96;
}
EncodedMessage += (char)encryptedchar;
}else{
EncodedMessage += message.charAt(i);
}
}
return EncodedMessage;
}
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
according to my timings over here its 2: 32 AM so don't feel offended if i didn't reply... THANK YOU...
BTW am not asking for a solution am just asking for a hint... I know this is a graded assignment and its immoral to cheat...
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
You do not show any proof of the problem. Post the program's input and output with comments describing what it should output.
For debugging these types of problems, I use println statements to print out the values of variables as they change in the program and to show execution flow.
Add lots of printlns to show the values of variables so you can see exactly what your code is doing as it executes.
Is there anything in your code that could be limited to 6 characters?
I see lots of "magic" numbers in your code: 97, 122, 96
What are they for? There are much better ways to write code than to use naked numbers like that.
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
This is what it means....
when the shift is 3 and the letter is z I will start over from the beginning of the alphabet...
a>> b >> c :D
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Quote:
This is what it means....
What are you talking about?
My comment about your code was the poor style you are using with magic numbers: 96, 97 and 122.
I assume these are the int values of ASCII chars like 'a' and 'z'. If so then use 'a' vs 97 etc
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
well am sorry my style doesn't appeal to you but the question remains... can u help???
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
I have made some suggestions in post #3 about how you should debug your code. Have you tried them?
Can the problem be in either of the classes: StackObj and QueueObj?
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Ok its been hours and the best I came up with is the following... yet I can't find an explanation and I was hoping you could help me with that...
THE POINT OVER HERE IS MAKING A STACK WITH FOR EXAMPLE THE FOLLOWING (3,2,4)
According to the assignment am trying to make the code pop the stack then its inverse then the stack then its inverse till the whole string is done... so I should pop 3 then 2 then 4 then 4 then 2 then 3 then 3 then 2 then 4 and so on....
I took ur advice and examined every part in the code and everything works fine except for this part...
It prints the stack then its reverse then zeros which doesn't make sense because I made it clear in the code that when both stacks (the original and the reverse) are empty u should recreate them...
ANy hint would be helpful thank you...
Code :
StackObj Sforkey2 = {3,2,4} // am just making clear what is the original stack
StackObj tempS = Sforkey2; // copy of original stack to be used
StackObj revS = reverse(tempS); // reverse of the copy, the reverse method returns the original stack in its original shape
public static StackObj reverse(StackObj x){
int size = x.size();
StackObj temp = new StackObj(size);
StackObj rev = new StackObj(size);
while(!x.isEmpty()){
temp.push(new Integer((Integer)x.top()).intValue());
rev.push(new Integer(((Integer)x.pop()).intValue()));
}
while(!temp.isEmpty()){
x.push(new Integer(((Integer)temp.pop()).intValue()));
}
return rev;
}
for(int i = 0; i < message.length(); i++){
int y = 0;
if(!tempS.isEmpty()){
y = (((Integer)tempS.pop()).intValue());
}else if(!revS.isEmpty()){
y = (((Integer)revS.pop()).intValue());
}
if((revS.isEmpty()) && (tempS.isEmpty())){ // recreation of stack and its reverse to repeat the operation
tempS = Sforkey2;
revS = reverse(tempS);
}
System.out.println(y);
}
This prints
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
I've used QueueObj and StackObj for a lot of questions and they worked fine so no the problem couldn't possibly be anywhere near them... :D
Thanks for the advice though
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
I have no way to test your code without definitions for ALL of the classes it uses.
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Code :
public class QueueObj{
private int maxsize;
private int front;
private int rear;
private int nItems;
private Object [] elements;
public QueueObj(int s){
maxsize = s;
front = 0;
rear = -1;
nItems = 0;
elements = new Object[maxsize];
}
public void enqueue(Object x){
if(rear == maxsize - 1)
rear = -1;
elements[++rear] = x;
nItems++;
}
public Object dequeue(){
Object result = elements[front];
front++;
if(front == maxsize)
front = 0;
nItems--;
return result;
}
public Object peek(){
return elements[front];
}
public boolean isEmpty(){
return (nItems == 0);
}
public boolean isFull(){
return (nItems == maxsize);
}
public int size(){
return nItems;
}
}
Code :
public class StackObj {
private Object[] theStack;
private int maxSize;
private int top;
public StackObj(int s){
maxSize = s;
theStack = new Object[maxSize];
top = -1;
}
public void push(Object elem){
top++;
theStack[top] = elem;
}
public Object pop(){
Object result = theStack[top];
top--;
return result;
}
public Object top(){
return theStack[top];
}
public boolean isFull(){
return (top == (maxSize-1) );
}
public boolean isEmpty(){
return (top == -1);
}
public int size() {
return (top+1);
}
}
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
I get the following error when I run the code. You have not provided a main/driver for testing. Here is what I am using:
Code :
public static void main(String[] args) {
String encMsg = encoding("encrypt" , new int[]{1,4,3}, new int[]{3,2,4});
System.out.println("encMsg=" + encMsg);
}
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at StackProblem$StackObj.pop(StackProblem.java:65)
at StackProblem.encoding(StackProblem.java:184)
at StackProblem.main(StackProblem.java:13)
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
THIS IS HONESTLY DEPRESSING -.-
I MANAGED TO GET THE FLOW CHART AND THE THE ENTIRE PROGRAM DONE IN 2 HOURS AND I CAN'T FIX HOW TO REVERSE A STACK CORRECTLY...
This assignment is driving me nuts...
Its kinda ironic how things like that makes u wonder if u've been admitted into the right major :S
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
If you've never seen the statistics on program development cycle, it is something like this: 90% of the code takes 20% of the time and that last 10% takes 80% of your time.
Here are some debugging tips to help you find the problem.
Add a toString() method to the StackObj and QueueObj classes. Have them print out the current values (Arrays.toString() is useful here) and also print out super.toString() which will give you an ID on the object that you are working with. When there are more than one, an ID helps.
Then add printlns to your code to show the status of the stacks/queues as you are using them.
The print out may show you where there is a problem in your logic.
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Alright... Will do...
BTW, We've been here before for another question of mine a month ago... just wanted to say am a fan and thanks for everything :D
If you have a blog or a facebook page that I can subscribe to that would be nice :D
I'm doing computer Engineering (2nd year).., nice to meet you :)
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
Another tip:
When you see that a particular call to a method is a problem and a variable gets a unique value during that call, call Thread.dumpStack() to get a stack trace that will show you who called.
Something like this:
Code :
if(var == specialVal) // are we at the point we want to see who called
Thread.dumpStack(); //show who called
Good luck with your studies.
No blog or anything else besides the few java forums.
Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)