Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 24th, 2004, 9:17 AM   #1
chris-guyatt
Newbie
 
Join Date: Sep 2004
Posts: 3
Rep Power: 0 chris-guyatt is on a distinguished road
Hi,

As a part of a java refresher course at university, we are being asked to create a Caesar cipher program that manipulates strings. So far ive created to char[] one called 'translator' and the other is alphabet. in my CaesarMap constructor ive initialised translator to a new array of 26 (of course) but im not sure how i can 'add' to an array somehow. just basically, i need help. ive forgotten to mention how. we are given a key (from somewhere) and that key is added to the alphabet array to create the corresponding letter in the same position in the translator array. Ok, so now ure completely confused!? Good. Me too. hehe. heres some code (my cursors been blinking for ages now):

package cipher;

class CaesarMap
{
 
 char[] translator;
  
 char[] alphabet = new char['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
           'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'
           'U', 'V', 'W', 'X', 'Y', 'Z'];
 
 public CaesarMap(int key)
 {
  
  translator = new char[26]; /* our "enciphered" array of alphabet */
  
  int modKey = key % 26; /* this makes sure endless loops are not made */
  
  for(int i /* it is here that i am stuck and my cursor is blinking... */
 }
 
 private char translate(char c)
 {
  
  return c; 
 }
}

erm, firstly i dont even know what ive done in the constructor is "right". basically ... HELP PLZ! :s im already worried. ANY help appreciated GREATLY! hehe.

--Christopher Guyatt
chris-guyatt is offline   Reply With Quote
Old Sep 24th, 2004, 10:11 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
First off, welcome to the forums... 2nd off, Merry Christmas.

// Caesar.java: implement the Caesar cipher
//  This carries out a simple rotation of lower-case letters,
//  and does nothing to all other characters, making the decryption
//  process even easier, because caps and punctuation marks survive unchanged.
//  Usage: java Caesar (-d | -e) key
//  Above, option "-d" is for decryption, while "-e" is for encryption
import java.io.*;
public class Caesar {
  private Reader in; // standard input stream for message
  private int key; // (en|de)cryption key

  // Caesar: constructor, opens standard input, passes key
  public Caesar(int k) {
   // open file
   in = new InputStreamReader(System.in);
   key = k;
  }
  // (en|de)crypt: just feed in opposite parameters
  public void encrypt() { translate(key); }
  public void decrypt() { translate(-key); }

  // translate: input message, translate
  private void translate(int k) {
   char c;
   while ((byte)(c = getNextChar()) != -1) {
     if (Character.isLowerCase(c)) {
      c = rotate(c, k);        
     }
     System.out.print(c);
   }
  }

  // getNextChar: fetches next char.
  public char getNextChar() {
   char ch = ' '; // = ' ' to keep compiler happy
   try {
     ch = (char)in.read();
   } catch (IOException e) {
     System.out.println("Exception reading character");
   }
   return ch;
  }
  // rotate: translate using rotation, version with table lookup
  public char rotate(char c, int key) { // c must be lowercase
   String s = "abcdefghijklmnopqrstuvwxyz";
   int i = 0;
   while (i < 26) {
     // extra +26 below because key might be negative
     if (c == s.charAt(i)) return s.charAt((i + key + 26)%26);
       i++;
   }
   return c;
  }


  // main: check command, (en|de)crypt, feed in key value
  public static void main(String[] args) {
   if (args.length != 2) {
     System.out.println("Usage: java Caesar (-d | -e) key");
     System.exit(1);
   }
   Caesar cipher = new Caesar(Integer.parseInt(args[1]));
   if (args[0].equals("-e")) cipher.encrypt();
   else if (args[0].equals("-d")) cipher.decrypt();
   else {
     System.out.println("Usage: java Caesar (-d | -e) key");
     System.exit(1);
   }
  }
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 24th, 2004, 10:18 AM   #3
chris-guyatt
Newbie
 
Join Date: Sep 2004
Posts: 3
Rep Power: 0 chris-guyatt is on a distinguished road
1stly, thankyou for the welcome, you should big up linuxforums.org for that one! hehe.
2ndly, THANK YOU for the code. although that looks fantastic, i will have to 'mod' it to satisfy the criteria from the lab manual. ive actually made some progress from the blinking cursor, but as its 4pm on friday and im off to leeds tonite, i need to make a move. hehe.

CHEERS

--Christopher Guyatt (guyattc2@cs.man.ac.uk)
chris-guyatt is offline   Reply With Quote
Old Sep 24th, 2004, 12:09 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
You're welcome. Yeah, some mods need to be made, but that code as it is will demonstrate a Ceasar Cipher, I'm sure you can build off of it.

It is still noon here, so I'm stuck at work.

IR
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 27th, 2004, 6:27 AM   #5
chris-guyatt
Newbie
 
Join Date: Sep 2004
Posts: 3
Rep Power: 0 chris-guyatt is on a distinguished road
alrighty everyone, just an update. stuck again, and my brain is fried! ive got till wednesday to get this done and i really havnt got anywhere with it! so what im gonna do is literally tell you whats got to be done (to the half way point) as there is no online copy, and what ive done so far, so bear with me on this one!

---------------------------------------------
INITIAL STEPS
Write a class called Caesar, with static methods for enciphering and deciphering files with given keys. Put this class in a package called cipher.

In the same package, for use by the two methods, create another class called CaesarMap. An object of this class should contain, as an instance variable, an array which can be used to effieciently translate letters. When an instance of the class is constructed, a ley is given, and used to initialise this array. All translations of characters is done by an instance method of CaesarMap, with the heading char translate(char c), and this should be called for all characters - including non-letters, which will just be returned unchanged as the result.

The two static methods use translate to do their job. Check that they work correctly by writing a couple of test classes, TestEncipher and TestDecipher, which each take three command line arguments (the name of the file containing the data, the name of the file to contain the results, and an integer key). These test classes should be outside the package and therefore not be placed in the subdirectory.
--------------------------------------------
CODE SO FAR: This is how far i have managed to get with the files. It should be clear where im stuck lol.

CAESARMAP.JAVA
package cipher;

class CaesarMap
{
 
 char[] translator;
  
 char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
           'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
           'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
 
 public CaesarMap(int key)
 {
  
  translator = new char[26]; /* our "enciphered" array of alphabet */
  
  int modKey;
  
  if(key > 26)
   modKey = key % 26; /* this makes sure endless loops are not made */
  else
   modKey = key;
  
  for(int i = 0; i < 26; i++)
  {
   
   int shift = i + modKey; /* the shift in the alphabet array */
   
   if(shift >= 26) /* if we go out of the array size */
   {
	
	shift %= 26; /* then return is to the start of the array */
   }
   
   translator[i] = alphabet[shift]; /* sets all 26 components of translator
                     array to the alphabet one but
    shifted */
   
   System.out.println(alphabet[i] + " encrypts to " + translator[i]);
  }
 }
 
 private char translate(char c)
 {
  
  return c; 
 }
 
 public static void main(String[] args) /* only for class testing purposes */
 {
  
  CaesarMap newMap = new CaesarMap(10);
 }
}

CAESAR.JAVA
package cipher;

import java.io.*;

class Caesar
{
 
 public Caesar()
 {
  
 }
 
 
 public static void encipher(BufferedReader infile,
               BufferedWriter outfile,
               int      key)
 {
  
  
 }
  
 public static void decipher(BufferedReader infile,
               BufferedWriter outfile,
               int      key)
 {
   
 }
}

TESTENCIPHER.JAVA (and testdecipher.java)
class TestEncipher()
{
 
 public static void main(String args)
 {
  
  if(args.length != 3)
  {
   
   System.out.println("Usage: java TestEncipher infile outfile key");
   System.exit(1);
  }
  
  
  
 }
}

erm basically, this is where im stuck:

1) i dont know much about command line arguments
2) what should the argumetns be for the encipher and decipher methods be
3) how does Caesar relate to CaesarMap
4) any other ideas etc that you think i should know about would be great. but im doing a lot of headscratching and just lots of blank lines in my code is a little depressing this early on in the academic year!

any suggestions would be greatly appreciated. THANKS!

--Christopher Guyatt
chris-guyatt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC