New to java, Caesar Cipher question.

Reply

Join Date: Oct 2008
Posts: 2
Reputation: steven.doyle2 is an unknown quantity at this point 
Solved Threads: 0
steven.doyle2 steven.doyle2 is offline Offline
Newbie Poster

New to java, Caesar Cipher question.

 
0
  #1
Oct 13th, 2008
Well there are two parts of code I'm working on, one is a driver for Caesar Cipher and the other is the Caesar Cipher class itself. I can't figure out how to pass the information the user inputs from the driver class to the cipher class. Any help would be great, this is getting aggravating.

  1. public class Driver
  2. {
  3. public static void main (String [] args)
  4. {
  5. Scanner in = new Scanner(System.in); // read input from terminal window
  6. CaesarCipher code; // an instance of a cipher
  7. String text; // user input to convert
  8. String result; // result of conversion
  9. int key; // key for Caesar cipher
  10. int menuSelection; // user's menu selection
  11.  
  12. System.out.println("CS 160, Program 2: Caesar Cipher");
  13. System.out.println("--------------------------------\n");
  14.  
  15. System.out.print("Enter the cipher key [0..25]: ");
  16. key = in.nextInt();
  17. code = new CaesarCipher(key);
  18.  
  19. displayMenu();
  20. menuSelection = getMenuChoice(in);
  21.  
  22. while (menuSelection != QUIT)
  23. {
  24. if (menuSelection == ENCODE)
  25. {
  26. System.out.print("Enter a sentence to encode: ");
  27. text = in.nextLine();
  28. result = code.encode(text);
  29. System.out.println("Plain: " + text);
  30. System.out.println("Cipher: " + result);
  31. }
  32. else if (menuSelection == DECODE)
  33. {
  34. System.out.print("Enter numbers to decode: ");
  35. text = in.nextLine();
  36. result = code.decode(text);
  37. System.out.println("Cipher: " + text);
  38. System.out.println("Plain: " + result);
  39. }
  40. else
  41. {
  42. System.out.println("Invalid option, please try again.");
  43. }
  44.  
  45. displayMenu();
  46. menuSelection = getMenuChoice(in);
  47. }
  48.  
  49. System.out.println("\nGoodbye.");
  50.  
  51. }
  52.  
  53. private static void displayMenu()
  54. {
  55. System.out.println();
  56. System.out.println("1. Encode a message.");
  57. System.out.println("2. Decode a message.");
  58. System.out.println("3. Quit.");
  59. }
  60.  
  61. private static int getMenuChoice(Scanner in)
  62. {
  63. int menuChoice; // user's menu selection
  64.  
  65. System.out.print("Please enter your menu choice: ");
  66. menuChoice = in.nextInt();
  67. in.nextLine(); // clear the user input line
  68. return menuChoice;
  69. }
  70.  
  71. public static final int ENCODE = 1; // user wants to encode text
  72. public static final int DECODE = 2; // user wants to decode numbers
  73. public static final int QUIT = 3; // user wants to quit
  74. }


  1. import java.util.Scanner;
  2.  
  3.  
  4. public class CaesarCipher
  5. {
  6. public CaesarCipher(int key)
  7. {
  8. }
  9.  
  10. public String encode(String text)
  11. {
  12. char current;
  13. //int code;
  14. this.text = text;
  15.  
  16. for (int position = 0; position < text.length(); position++)
  17. {
  18. current = text.charAt(position);
  19. current = Character.toLowerCase(current);
  20. System.out.print(current + " ");
  21.  
  22. int code = (int) current;
  23. //System.out.print(code + " ");
  24.  
  25. if (Character.isLowerCase(current))
  26. {
  27. code = code - A_VALUE; //+ key;
  28. System.out.print(code + " ");
  29. }
  30.  
  31. //return code;
  32. }
  33. return "coded string";
  34. }
  35.  
  36. public String decode(String text)
  37. {
  38. return "decoded string";
  39. }
  40.  
  41. public int code;
  42. public String text;
  43. public int key;
  44. private static final int A_VALUE = (int) 'a';
  45. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: steven.doyle2 is an unknown quantity at this point 
Solved Threads: 0
steven.doyle2 steven.doyle2 is offline Offline
Newbie Poster

Re: New to java, Caesar Cipher question.

 
0
  #2
Oct 13th, 2008
Anybody? I'm so lost right now...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: New to java, Caesar Cipher question.

 
0
  #3
Oct 14th, 2008
read your text in as a String Object, and pass that as arguments to the methods of your second class. Basic OO-stuff actually
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC