Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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...
    Untitled.jpg
    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...
    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;
    	}


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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...

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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.

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)

    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

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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???

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)

    can u help???
    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?

  8. #8
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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...

    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

  9. #9
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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...
    Thanks for the advice though

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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.

  11. #11
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)

    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;
    	}
    }


    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);
    	  }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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:
    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);
      }

    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)

  13. #13
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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.

  15. #15
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    If you have a blog or a facebook page that I can subscribe to that would be nice
    I'm doing computer Engineering (2nd year).., nice to meet you

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,918
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default 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:
      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.

  17. The Following User Says Thank You to Norm For This Useful Post:

    Medo Almasry (November 19th, 2011)

  18. #17
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)

    alright Thanks again

Similar Threads

  1. Replies: 0
    Last Post: November 6th, 2011, 03:55 PM
  2. Encode and decode - Caesar cipher
    By siabanie in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 07:25 PM
  3. How to shift a String in a rectangle which is type of Canvas
    By elenora in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: April 4th, 2011, 08:39 AM
  4. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  5. Implementing a queue or stack using a heap
    By tla280 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 1st, 2010, 12:29 AM