Question

I'm also having trouble on dividing the code into methods. This is my work done so...

I'm also having trouble on dividing the code into methods. This is my work done so far and net beans give me a lot of errors. Help!

package caesarcipher;
import textio.TextIO;

/**
*
* @author
*/
public class CaesarCipher {

enum code {encodeText, decodeText };
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
  
String code;
char x;
  
int totalcount = 0;
  
while(true){
TextIO.put("Type in an E to encode a message or a D to decode a message "
+ "or an X to exit the program: ");
code = TextIO.getlnWord();
  
switch(code.toUpperCase()){
case "E":
TextIO.putln("You have chosen to encode a message!");
break;

case "D":
TextIO.putln("You have chosen to decode a message!");
break;
  
case "X":
TextIO.putln("Bye!");
return;
  
  
}
}   
  
}
  
  
static String encodeText(String code){
switch(code){

TextIO.put("Enter a shift count: ");
int shift = TextIO.getlnInt();

TextIO.put("Enter your plain text: ");

char[] message = TextIO.getln().toUpperCase().toCharArray();
for(int i=0; i < message.length; i++){

char alpha;
if((message[i] >= 'A' && message[i] <= 'Z')){
int cryp =((int)message[i] + (shift));
if(cryp > (int) 'Z'){
cryp -= 26;
}
alpha = ((char)cryp);
}
else {
alpha = message[i];
}
System.out.print(alpha);
}
System.out.print("\n");
break;
}
}
static String decodeText()
switch(code){

TextIO.put("Enter a shift count: ");
shift = TextIO.getlnInt();
  
TextIO.put("Enter the encrypted message: ");
char[] dcode = TextIO.getln().toUpperCase().toCharArray();
for(int i=0; i< dcode.length;i++){
  
char alpha;
if((dcode[i] >= 'A' && dcode[i] <= 'Z')){
int cryp =((int)dcode[i]) - shift;
if(cryp < (int)'A'){
cryp += 26;
}
alpha = ((char)cryp);
  
} else {
alpha = dcode[i];
  
}
System.out.print(alpha);
}
System.out.print("\n");
break;
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package caesarcipher;
import textio.TextIO;

/**
*
* @author
*/
public class CaesarCipher {

  enum code {

    encodeText, decodeText

  };

  /**

   * @param args

   * the command line arguments

   */

  public static void main(String[] args) {

    String code;

    char x;

    int totalcount = 0;

    while (true) {

      TextIO.put(

          "Type in an E to encode a message or a D to decode a message " + "or an X to exit the program: ");

      code = TextIO.getlnWord();

      switch (code.toUpperCase()) {

      case "E":

        TextIO.putln("You have chosen to encode a message!");

        break;

      case "D":

        TextIO.putln("You have chosen to decode a message!");

        break;

      case "X":

        TextIO.putln("Bye!");

        return;

      }

    }

  }

  static void encodeText(String code) {

    TextIO.put("Enter a shift count: ");

    int shift = TextIO.getlnInt();

    TextIO.put("Enter your plain text: ");

    char[] message = TextIO.getln().toUpperCase().toCharArray();

    for (int i = 0; i < message.length; i++) {

      char alpha;

      if ((message[i] >= 'A' && message[i] <= 'Z')) {

        int cryp = ((int) message[i] + (shift));

        if (cryp > (int) 'Z') {

          cryp -= 26;

        }

        alpha = ((char) cryp);

      } else {

        alpha = message[i];

      }

      System.out.print(alpha);

    }

    System.out.print("\n");

  }

  public static void decodeText() {

    TextIO.put("Enter a shift count: ");

    int shift = TextIO.getlnInt();

    TextIO.put("Enter the encrypted message: ");

    char[] dcode = TextIO.getln().toUpperCase().toCharArray();

    for (int i = 0; i < dcode.length; i++) {

      char alpha;

      if ((dcode[i] >= 'A' && dcode[i] <= 'Z')) {

        int cryp = ((int) dcode[i]) - shift;

        if (cryp < (int) 'A') {

          cryp += 26;

        }

        alpha = ((char) cryp);

      } else {

        alpha = dcode[i];

      }

      System.out.print(alpha);

    }

    System.out.print("\n");

  }

}

==================================
You need to call these functions now, I have just made it compile....You need call those functions generate Output

1357 1358 1359 1360 1361 enum code { encodeText, decodeText Textio Console Type in an E to encode a message or a D to decode

PLEASE COMMENT if there is any concern.

==========================================

=================================

Add a comment
Know the answer?
Add Answer to:
I'm also having trouble on dividing the code into methods. This is my work done so...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

  • Hello, There are some elements in my code that aren't working properly that need to be...

    Hello, There are some elements in my code that aren't working properly that need to be fixed but I don't know what I have done wrong. The printHealthBars() should print out a '+' for each point of current health and a '-' for each difference between current & max health. So, for instance, suppose the pokemon had 6/8 health, the method should print: "[ ++++++-- ]" Something is up with the damage calculation method. The idea is that it should...

  • THIS IS MY CODE ALL I NEED IS THE SECTIONS TO BE FILLED IN AND CODE...

    THIS IS MY CODE ALL I NEED IS THE SECTIONS TO BE FILLED IN AND CODE SHOULD RUN. FILL IN THE CODE PLEASE. package p02; import static java.lang.System.*; import java.io.*; import java.util.*; public class P02 {    public static Vector<Double> wallet=new Vector<Double>();    public static void addMoneyToWallet(){    }    public static void removeMoneyFromWallet(){    }    public static void displayMoneyInWallet(){    }    public static void emptyWallet(){    }    public static void main(String[] args){ int choice=0; String menu="Wallet...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • I have almost done with this code to Implement Huffman Coding. However I have an error...

    I have almost done with this code to Implement Huffman Coding. However I have an error at the "Heap<Tree>". Could anyone help with solving this issue, really appreciated. Thank you!! import java.util.Scanner; public class HuffmanEncoding { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a text: "); String text = input.nextLine();    int[] counts = getCharacterFrequency(text); // Count frequency System.out.printf("%-15s%-15s%-15s%-15s\n", "ASCII Code", "Character", "Frequency", "Code");    Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[]...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT