Skip to main content
added 71 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Problem J4: Big Bang Secrets. The

The encoding algorithm is a Caesar cipher with a shift (S\$S\$) that depends on a parameter (K\$K\$) and its position in the word (P\$P\$, where P = 1\$P = 1\$ for the first letter of each word): S = 3 P +\$S = 3\$, K\$P + K\$. For example, when K = 3\$K = 3\$, ZOOM is encoded as FXAB:

  • \$S_1 = 3 \times 1 + 3 = 6\$\$S_1 = 3 \cdot 1 + 3 = 6\$, so Z \$\rightarrow\$ F
  • \$S_2 = 3 \times 2 + 3 = 9\$\$S_2 = 3 \cdot 2 + 3 = 9\$, so O \$\rightarrow\$ X
  • \$S_3 = 3 \times 3 + 3 = 12\$\$S_3 = 3 \cdot 3 + 3 = 12\$, so O \$\rightarrow\$ A
  • \$S_4 = 3 \times 4 + 3 = 15\$\$S_4 = 3 \cdot 4 + 3 = 15\$, so M \$\rightarrow\$ B

The challenge is to write a decoder. TheThe first line of input contains K\$K\$ (K < 10\$K \lt 10\$). The second line contains the encoded message, containing up to 20 characters in uppercase.

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Problem J4: Big Bang Secrets. The encoding algorithm is a Caesar cipher with a shift (S) that depends on a parameter (K) and its position in the word (P, where P = 1 for the first letter of each word): S = 3 P + K. For example, when K = 3, ZOOM is encoded as FXAB:

  • \$S_1 = 3 \times 1 + 3 = 6\$, so Z F
  • \$S_2 = 3 \times 2 + 3 = 9\$, so O X
  • \$S_3 = 3 \times 3 + 3 = 12\$, so O A
  • \$S_4 = 3 \times 4 + 3 = 15\$, so M B

The challenge is to write a decoder. The first line of input contains K (K < 10). The second line contains the encoded message, containing up to 20 characters in uppercase.

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Problem J4: Big Bang Secrets

The encoding algorithm is a Caesar cipher with a shift (\$S\$) that depends on a parameter (\$K\$) and its position in the word (\$P\$, where \$P = 1\$ for the first letter of each word): \$S = 3\$, \$P + K\$. For example, when \$K = 3\$, ZOOM is encoded as FXAB:

  • \$S_1 = 3 \cdot 1 + 3 = 6\$, so Z \$\rightarrow\$ F
  • \$S_2 = 3 \cdot 2 + 3 = 9\$, so O \$\rightarrow\$ X
  • \$S_3 = 3 \cdot 3 + 3 = 12\$, so O \$\rightarrow\$ A
  • \$S_4 = 3 \cdot 4 + 3 = 15\$, so M \$\rightarrow\$ B

The challenge is to write a decoder. The first line of input contains \$K\$ (\$K \lt 10\$). The second line contains the encoded message, containing up to 20 characters in uppercase.

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 
Question Protected by Jamal
Tweeted twitter.com/#!/StackCodeReview/status/560898604152680449
Summarized the challenge
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Question: Problem J4: Big Bang Secrets. The encoding algorithm is a Caesar cipher with a shift (S) that depends on a parameter (K) and its position in the word (P, where P = 1 for the first letter of each word): S = 3 P + K. For example, when K = 3, ZOOM is encoded as FXAB:

  • \$S_1 = 3 \times 1 + 3 = 6\$, so ZF
  • \$S_2 = 3 \times 2 + 3 = 9\$, so OX
  • \$S_3 = 3 \times 3 + 3 = 12\$, so OA
  • \$S_4 = 3 \times 4 + 3 = 15\$, so MB

The challenge is to write a decoder. The first line of input contains K (K < 10). The second line contains the encoded message, containing up to 20 characters in uppercase.

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Question: Problem J4: Big Bang Secrets

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Problem J4: Big Bang Secrets. The encoding algorithm is a Caesar cipher with a shift (S) that depends on a parameter (K) and its position in the word (P, where P = 1 for the first letter of each word): S = 3 P + K. For example, when K = 3, ZOOM is encoded as FXAB:

  • \$S_1 = 3 \times 1 + 3 = 6\$, so ZF
  • \$S_2 = 3 \times 2 + 3 = 9\$, so OX
  • \$S_3 = 3 \times 3 + 3 = 12\$, so OA
  • \$S_4 = 3 \times 4 + 3 = 15\$, so MB

The challenge is to write a decoder. The first line of input contains K (K < 10). The second line contains the encoded message, containing up to 20 characters in uppercase.

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 
deleted 8 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result? Thanks.

Question: Problem J4: Big Bang Secrets

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result? Thanks.

Question: Problem J4: Big Bang Secrets

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 

Since I am new to Java, I wanted to see if there were better ways of answering the same question. Are parts of my code redundant or maybe there's an easier way to get the same result?

Question: Problem J4: Big Bang Secrets

public class Decoder { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); int k = Integer.parseInt(in.readLine()); String word = in.readLine(); char[] cArray = word.toCharArray(); for (int i = 0; i < word.length(); i++) { char out = (char) ((cArray[i]) - (((3 * (i + 1)) + k) % ('Z' - 'A'))); if (out < 'A') { char wrap = (char) (('Z' + 1) - ('A' - out)); System.out.print(wrap); } else { System.out.print(out); } } } catch (IOException e) { System.out.println("Error"); } } } 
added 1 character in body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96
Loading
Source Link
quidproquo
  • 357
  • 1
  • 2
  • 9
Loading