[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Missing post? - see this post about spam for more information.
Secret messages  XML
Forum Index » New to Java
Author Message
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

"create a Java program that allows you to encode and decode secret messages. Initially, a message should be stored in your Java program as a String, and should then be encoded and displayed. Finally, the coded message should then be decoded and displayed (to check it is the same as the original message)"

*Create a new class called "Encode".
*Write a method called "scrambleString" that takes a string as a parameter and returns a string.
*The method should encode the string provided in the parameter by using a Caesar cipher with a displacement of 3, (see this diagram for an illustration). The resulting string should be returned from the method.
*You should only change the characters in the string if they are letters (ignore any other characters).

i need some help starting this, i'm really struggling with the whole java concept and i need some advice!
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

Never mind Java for the moment. Pretend you're writing a recipe - and the person reading the recipe has no idea how to cook. Or pretend you're writing for a clymer manual if you prefer.

What steps would you take to perform these tasks?
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

k, first you need to scramble the word using a cipher that moves the letters along 3 spaces
then this needs to be asigned to an ASCII code number
then you need to subtract all the ASCII values to get the coded message
this needs to be displayed

this then needs to be done in reverse to get the message back
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

I'm sorry to be so specific, but this is important if you want to learn how to program. How specifically do you scramble the letters (not Java - just in words)? How specifically do you unscramble the letters?
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

the letters are moved on 3 spaces
e.g abc becomes cde this carries on in a loop so xyz becomes abc
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

Excellent. This means that you need some way to map the letter "a" to "d" - yes? So let's start with that:

In the Java API, there is a class called "HashMap". The idea is that you have a "key" and a "value". If you "Map" "a" to "d", then everytime you find an "a" you can substitute a "d". Armed with this, you could parse your string and substitute each character.

Before I get too much farther, do you have Java 5? If you're not sure, go to a command prompt and type "java -version".
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

i have to do it using the "ceaser cipher" as it says in my notes. ive sorta given up anyway, i really cant do it, ive written 3 lines of code and i cant get past that! its too hard
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

Oh come now. Suppose I gave you this:

Map<Character,Character> caeserMap = new HashMap<Character,Character>();
caesarMap.put( 'a', 'd' );
caesarMap.put( 'b', 'e' );
...

and I gave you this:
System.out.println( caesarMap.get( 'a' ) ); //prints d

Would that help?
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

lol makes no difference, i still cant do any of the rest and it wudnt be my work, dont really fully understand what it does anyway
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

It's a very small part of what you need. What it does is map a supplied character with a return character. If you were to make a complete map of all the characters - that is to say continue the pattern at the ellipsis (...) - and map all the way to z, you would have the mechanism to translate your characters.

That's the point behind the map - you map characters.

This message was edited 1 time. Last update was at 12/12/2005 16:17:58

amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

i officially hate java! its pants, i wish i'd taken geography instead!!!!!
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

I'm sorry. I don't know what to say to that. I'm not going to write this for you.

Maybe you should start here Your first cup of java. . Come back when you feel a bit more comfortable (and less annoyed with Java).
tfecw

Newbie

Joined: 09/19/2005 15:02:20
Messages: 144
Location: No. VA.
Offline

With respect to Mrider, I would suggest a potentially easier solution.

While a Hash map would certianly work, i think it's slightly hardcoded and maybe out of bounds for a java 101 class.

At the OP:

what do you ahve so far? Do you have your code set up so that once you drop the decryptor methods or encryption methods in, everything will work correctly?

I'm sure you opted for java for a reason, but like anything it has a learning curve. Break the problem into small steps. First step is to get the "skeleton" of your code working. Then concentrate on what the program does on paper (as Mrider pointed out) then start translating it to java.

I'm gonig to be a little cryptic with my advice (heh, get it cryptic) for 2 reasons. One I'm not doing your project for you, 2 I'm not sure of the proper terms for what I'm doing. Anyway here goes:

When you wrote up how the program works in english you used said "the letter moved up 3 spaces"

Lets slightly modify that to say "we add 3 to the letter" If we have A adding 3 gets us D.

So consider the following code. Read over it and see what it does. Then actually compile and run the code and see what it does. Then play around with it and see what different letters/numbers/math operations do.


The output is:
A
68
65
D

I think i've basicaly given away how to do the encrpytion piece of the program, so i'll leave the rest of it up to you. Let us know if you have specific questions over the actual syntax


aim icon
amzy28587

Newbie

Joined: 10/31/2005 15:09:46
Messages: 11
Offline

cheers for that. erm i'm sorta stuck on the first bit "write a method that takes a string as a parameter and returns a string. " dont understand what that means!

all i got so far is:

public Encode {
public static void main (String [] args)
public scrambleString();



LOL im well pants at this stuff!
mrider

Newbie

Joined: 10/25/2005 11:50:02
Messages: 25
Offline

To tfecw: I considered the add to the character route. The trick comes when the OP is expected to decide whether a character is a ASCII character. Using the "add to the character" method, "This is a test." becomes "Wklv#lv#d#whvw1" - which doesn't quite fit the bill.

I considered the regular expression route - and thought that it'd probably be easier to just create a map, and use a loop to add characters.

YMMV...
 
Forum Index » New to Java
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2012 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle