Question

In java write a command-line program that helps to decrypt a message that has been encrypted...

In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2).

When your program is correctly run, it will be provided two strings: the encrypted message and a string that is in the deciphered text. If fewer/more arguments are provided, your program is to output an error:

$ java edu.wit.cs.comp1050.PA4a

Please supply correct inputs: : <encrypted string> <substring>

$ java edu.wit.cs.comp1050.PA4a a

Please supply correct inputs:  : <encrypted string> <substring>

$ java edu.wit.cs.comp1050.PA4a a b c

Please supply correct inputs:  : <encrypted string> <substring>

If the correct arguments are supplied, you are to output any shifts (00-25) that contain the supplied substring:

$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' is

09: Secfkjuh isyudsu yi vkd!

19: Computer science is fun!

$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' 'fun!'

19: Computer science is fun!

If no shifts contain the substring, provide an error:

$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' '?'

No valid shifts found.

To build this program in an object-oriented fashion, you must first implement a Shifter class. This class is constructed with the encrypted string, and then has methods to both shift by an arbitrary amount and find substrings across all shifts.

To implement this class in an efficient manner, you should use a StringBuilder to shift the encrypted string. You might also find it useful to use an ArrayList to accumulate an unknown number of valid shifts. Look to the JavaDoc’s of the String class for methods to search a string for a substring.

Here is the skeleton code for the Shifter class

public class Shifter {

/**

* Number of letters in the English alphabet

*/

public static final int NUM_LETTERS = ('z' - 'a') + 1;

/**

* Initializes the shifter

*

* @param s encrypted string

*/

public Shifter(String s) {

// replace with your code

}

/**

* Returns the result of shifting

* by a supplied amount

*

* @param n number of places to shift

* @return shifted string

*/

public String shift(int n) {

return null; // replace with your code

}

/**

* Finds all shifts that contain

* the supplied substring

*

* @param sub substring to find

* @return array of shifts (0-25) that contain the substring (in order)

*/

public int[] findShift(String sub) {

return null; // replace with your code

}

}

Here is the main program skeleton:

public class PA4a {

/**

* Error if incorrect command-line arguments are supplied

*/

public static final String ERR_USAGE = "Please supply correct inputs: <encrypted string> <substring>";

/**

* Error if shift could not be found

*/

public static final String ERR_NONE = "No valid shifts found.";

/**

* Outputs all shifts of the encrypted string

* that contain the supplied substring

*

* @param args command-line arguments: <encrypted string> <substring>

*/

public static void main(String[] args) {

// replace with your code

}

}

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

package myList;

class Shifter {

   String str;

   public static final int NUM_LETTERS = ('z' - 'a') + 1;

   public Shifter(String s) {

       str = s;

   }

   public String shift(int n) {

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

           if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) > 'A' && str.charAt(i) <= 'Z')) {

               if (str.charAt(i) == 'z')

                   str = str.substring(0, i) + 'a' + str.substring(i + 1);

               else if (str.charAt(i) == 'Z')

                   str = str.substring(0, i) + 'A' + str.substring(i + 1);

               else

                   str = str.substring(0, i) + (char) (str.charAt(i) + 1) + str.substring(i + 1);

           }

       }

       return str;

   }

   public int[] findShift(String sub) {

       int res[] = new int[26];

       int j = 0;

       for (int i = 0; i < NUM_LETTERS; i++) {

           str = shift(1);

           if (str.indexOf(sub) != -1) {

               res[j] = i + 1;

               j++;

           }

       }

       res[j] = -1;

       return res;

   }

}

public class ShiftTest

{

   public static void main(String args[]) {

       if (args.length != 2) {

           System.out.println("Error : Please supply correct inputs: ");

           return;

       }

       Shifter sftr = new Shifter(args[0]);

       int res[] = new int[26];

       res = sftr.findShift(args[1]);

       if (res[0] == -1)

           System.out.println("No Valid Shift found");

       for (int i = 0; i < 26; i++) {

           if (res[i] == -1)

               break;

           System.out.println(res[i]);

       }

   }

}

======================================
See Output for Input : "Jvtwbaly zjplujl pz mbu!" "fun"


41 public class ShiftTest 42 43 1 Console <terminated> ShiftTest [Java Application] /Library/Internet Plug-Ir 19 45 public st
Thanks

Add a comment
Know the answer?
Add Answer to:
In java write a command-line program that helps to decrypt a message that has been encrypted...
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
  • Your task is to write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1 . Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case

    When your program is correctly run, it will be provided two strings: the encrypted message and a string that is in the deciphered text. If fewer/more arguments are provided, your program is to output an error:$ java edu.wit.cs.comp1050.PA4aPlease supply correct inputs: : <encrypted string> <substring>$ java edu.wit.cs.comp1050.PA4a aPlease supply correct inputs:  : <encrypted string> <substring>$ java edu.wit.cs.comp1050.PA4a a b cPlease supply correct inputs:  : <encrypted string> <substring>If the correct arguments are supplied, you are to output any shifts (00-25) that contain the...

  • Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all...

    Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles {     /**      * @param args the command line arguments      */     public static void main(String[] args) throws FileNotFoundException {         // TODO code application logic here...

  • JAVA

    Write a Java class (program) which inputs a simple integer and outputs a list of values from the input variable down to 0 (not including 0). You should only show every other value in the output, starting with the input value. You must use a for loop.For example, if the input is 23, the output should be:The input value was 2323 21191715131197531Code given:import java.util.Scanner;/**  * This program counts down from the input number down to (but not including)   * 0, skipping...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...

    Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes. Shape.java will be the parent class and will contain a Point type. Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class Each class (Circle, Triangle, Rectangle) will contain the private class...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

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