I need this program to encrypt by making letter a letter z and b, y and so on. The proffesor got us started by giving us this program and for the life of me i can't figure out how to change it.

public class ceaser
{

	public static void main(String[] args)
	{
		String str = "this program is starting to upset me";
		int key = 25;

			String encrypted = encrypt(str, key);
			System.out.println(encrypted);

			String decrypted = decrypt(encrypted, key);
			System.out.println(decrypted);
	}

	public static String encrypt(String str, int key) {
		String encrypted = "";

		for(int i = 0; i < str.length(); i++) {
	int c = str.charAt(i);
		if (Character.isUpperCase(c))
		{
			c = c + (key % 26);
		if (c > 'Z')
			c = c - 26;
		} else if (Character.isLowerCase(c)) {
				c = c + (key % 26);
			if (c > 'z')
				c = c - 26;
		}
			encrypted += (char) c;
		}
			return encrypted;
		}

	public static String decrypt(String str, int key)
		{
			String decrypted = "";
		for(int i = 0; i < str.length(); i++) {
		int c = str.charAt(i);
		if (Character.isUpperCase(c)) {
			c = c - (key % 26);
		if (c < 'A')
			c = c + 26;
		}
		else if (Character.isLowerCase(c)) {
			c = c - (key % 26);
		if (c < 'a')
			c = c + 26;
		}
			decrypted += (char) c;
		}
			return decrypted;
		}


}

encrypt by making letter a letter z and b, y

Perhaps if you printed out all the values of the variables as the program executed you would see what the program is doing. Do you test with short simple Strings like "abc" and see what happens to those 3 letters. Then change them to some other simple String and see what is printed out as they are "encrypted".

One thing you need to know is that the characters 'a' to 'z' are numeric values and can be used in arithmetic operations. For example: 'a' + 1 gives 'b'
There can be problems with the compiler understanding what you want to see when you do this and print out the results so you may have to cast the result to char by adding (char) in front.

You can see the value of 'a' by casting it to int and printing it: Try printing (int)'a'

Edited 5 Years Ago by NormR1: n/a

i understand what you are saying, but i cannot seem to find the right part to change to get what is needed

Describe what is needed.
What is the program supposed to do and how are you going to code the program to do it?

right now the program just moves to the next letter (ex: a turns to b, b turns to c). I am supposed to make a coded as a z and b coded as a y and so on. I believe by changing the number in line 8. i just cannot figure out what number/equation to use.

Write down a list with 2 columns, first column the input letter (a,b...), second column what you want to change the letter in the first column to (z,y,...).
Look at the relationship between the line the letter is on and how much to add/subtract to that letter to get the letter in the second column. That is the formula.

i am trying 97-(number) to follow the idea of all the keys have a ascii value, but when i get a to encrypt as z, b is still encrypted as c

What is the derivation of : 97-(number)?
What was the formula for converting the letters?
How did it relate to which line (ie which letter) the source letter was on?

I don't see it as a constant.

so if i understand that correctly you are saying I should use something like an if statement so that it will apply to all letters

I should use something like an if statement so that it will apply to all letters

I'm not sure what you are saying. Of course it has to apply to all the letters.

What relationship did you see between the letters in the first column and those in the second column? How did it relate to the line number the letter was on?

Did you do this? Enter the first 5 lines that you wrote done for this exercise.

Write down a list with 2 columns, first column the input letter (a,b...), second column what you want to change the letter in the first column to (z,y,...).
Look at the relationship between the line the letter is on and how much to add/subtract to that letter to get the letter in the second column. That is the formula.

What is the relationship(numerical difference) between the letters in column one and column two?
How does that relationship change as the lines change?

Edited 4 Years Ago by NormR1: n/a

Be very specific. Its very hard to write code makes a variables value "gets smaller".
You need a formula.

Comments
Good point. Rep "gets bigger."

that was part of the orginal problem i still don't know how to change it to make it work right

I'm trying to show you a way to get the formula.
Take the two columns you posted above and write in a third column the numeric difference between the value of the letter in the first column and that in the second.
There is a pattern in these values and from that you can make a formula to convert what is in the first column to what is in the second column

ok i have those numbers. how do i make the formula with just these numbers?

Please post them.
You look at the number on each row and find the formula that will compute the number based on what row (what letter) you are on.

Now you need to see the how the numbers change as you go from a to b and from b to c etc
Write an equation to compute the value 25 given a row value of 1.
Now an equation to compute 23 given a row value of 2.
and 21 with a value of 3
and 19 with 4
etc

consider these expressions:
'a' - 'a' = 0
'b' - 'a' = 1
'c' - 'a' = 2

If you had posted the three columns I asked for what is being computed would be more obvious.

The other equation you need:
the row number (0 based) = the letter - 'a'

Edited 4 Years Ago by NormR1: n/a

a z 25
b y 23
c x 21
d w 19
e v 17
f u 15
g t 13
h s 11
i r 9
j q 7
k p 5
l o 3
m n 1
n n 1
o l 3
p k 5
q j 7
r i 9
s h 11
t g 13
u f 15
v e 17
w d 19
x c 21
y b 23
z a 25

Are the numbers all positive?

See my previous post for what you need to do now.

You need an equation that will solve the following for all 26 letters:
Given the letter 'd' what algebraic equation will compute 'w'?

What is 97? Is that 'a'?
What is 122?

I don't see the variable in your equation. Since each letter has a different amount to be added to it, your equation needs to have the letter in it.
Something like this:

newLetter = f(olderLetter);
where f is the equation that computes the amount and adds it to the oldLetter to get the newLetter.

Edited 4 Years Ago by NormR1: n/a

so you want me to create a variable for all 26 letters and then make a formula to change a to z ?

What is your assignment? Do the letters you put in the two columns represent the input and output for your program?
If that is the case, you need an equation that takes any letter from the first column and computes the letter in the second column

create a variable for all 26 letters

I would change that to:
create a variable for any one of the 26 letters

That is sort of the definition of a variable: It holds one of any number of different values.

Edited 4 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.