Question

I'm stuck in my homework which is about a caesar cipher java program which includes an...

I'm stuck in my homework which is about a caesar cipher java program which includes an infinite while loop, char array, and a switch statement

I have to encode and decode messages

Homework Answers

Answer #1

public class Main

{

public static int checkCharacter(char ch) {

if (Character.isUpperCase(ch)) {

return 1;

} else if (Character.isLowerCase(ch)) {

return 2;

} else {

return 3;

}

}

public static String encrypt(char chars[], int s)

{

StringBuffer result = new StringBuffer();

char ch;

int i = 0;

while(true)

{

if (i >= chars.length) {

break;

}

switch(checkCharacter(chars[i])) {

case 1:

ch = (char)(((int)chars[i] + s - 65) % 26 + 65);

result.append(ch);

break;

case 2:

ch = (char)(((int)chars[i] + s - 97) % 26 + 97);

result.append(ch);

break;

default:

break;

}

i ++;

}

return result.toString();

}

public static String decrypt(char chars[], int s)

{

StringBuffer result = new StringBuffer();

char ch;

int i = 0;

while(true)

{

if (i >= chars.length) {

break;

}

switch(checkCharacter(chars[i])) {

case 1:

ch = (char)(((int)chars[i] - s + 65) % 26 + 65);

result.append(ch);

break;

case 2:

ch = (char)(((int)chars[i] - s + 97) % 26 + 97);

result.append(ch);

break;

default:

break;

}

i ++;

}

return result.toString();

}

public static void main(String[] args)

{

String text = "ATTACKATONCE";

int s = 4;

System.out.println("Text : " + text);

String encrypted = encrypt(text.toCharArray(), s);

String decrypted = decrypt(encrypted.toCharArray(), s);

System.out.println("Encrypted text: " + encrypted);

System.out.println("Decrypted text: " + decrypted);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hi, I'm currently stuck with my homework. I need to know how to write this program...
Hi, I'm currently stuck with my homework. I need to know how to write this program in Python as below: Write function repeatPhrase(phrase, n), which prints the given phrase n times, alternating between lowercase and uppercase. For example, repeat('The sky is blue', 5) would print: the sky is blue THE SKY IS BLUE the sky is blue THE SKY IS BLUE the sky is blue Please help me, thank you very much!
Please make a Java program that has a menu method and includes dialog boxes. The program...
Please make a Java program that has a menu method and includes dialog boxes. The program will call the proper method, but each method will be completed later. Your program will keep track of information for a Hospital. The program will be menu-driven. Your menu is to look something like the following: UPMC Medical Center Add/Modify Patient Information Add/Modify Physician Information Add/Modify Medical Information Hospital Report Section Exit the Medical System Please Make your selection > In this first phase,...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT