1 answer

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

Question:

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

}

}


Answers

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

.

Similar Solved Questions

1 answer
For a reservoir with the following data: → pore-volume compressibility is 17x10-6 psi-1 overburden pressure gradient...
For a reservoir with the following data: → pore-volume compressibility is 17x10-6 psi-1 overburden pressure gradient = 1 psi/ft > Pore pressure gradient = 0.5 psi/ft What is the percentage of porosity reduction for every 1000 feet increase in depth....
1 answer
Please urgent electrical or mechanical engineer can help? I have 20 hours.I have to finish. Pleaseeee :(
Question 1) A \(380 \mathrm{~V} 60 \mathrm{~Hz}\) two-pole three-phase asynchronous motor has a reverse power factor of 0.80. Stator resistance \(0,200 \Omega\) rotor resistance \(0,120 \Omega\) iron reactance resistance \(150 \Omega\) friction andwind losses \(450 \mathrm{~W}\), iron losses \(1500 ...
3 answers
Case Study N.G. is a 21-year-old year-old college student who is admitted to the hospital with...
Case Study N.G. is a 21-year-old year-old college student who is admitted to the hospital with a diagnosis acute poststreptococcal glomerulonephritis. He was seen at the college's student neam center because of swelling around his eyes and rusty-colored urine. His history based on medical record...
1 answer
Let 1 0 0 0 0 0 2 0 0 0 A=0 0 3 0 0...
Let 1 0 0 0 0 0 2 0 0 0 A=0 0 3 0 0 0 0 0 4 0 0 0 0 0 5 (a) (5 points) Find the general solution to the sysem of equations I' = AT (b) (5 points) Solve the initial value problem x1(0) = x2(0) - = ... = C5(0) = 5....
1 answer
Please need the correct answer and the rationales A nur se is cariną tor a client...
please need the correct answer and the rationales A nur se is cariną tor a client with hyperparathyroidism. The nurse identifies the most appropriate nursing intervention for this client? A) Pad side rails as a seizure pre caution B Increase fluid intake to 3-4L daily C)Maintain bedrest to ...
1 answer
Discuss one drug of abuse and report it's impact on the patient's health
Discuss one drug of abuse and report it's impact on the patient's health...
1 answer
For the reaction 2 HCl + Ca(OH), CaCl2 + 2H2O how many grams of calcium chloride,...
For the reaction 2 HCl + Ca(OH), CaCl2 + 2H2O how many grams of calcium chloride, CaCl2, are produced from 78.3 g of hydrochloric acid, HCI? mass of calcium chloride:...
1 answer
The individual financial statements for Gibson Company and Keller Company for the year ending December 31,...
The individual financial statements for Gibson Company and Keller Company for the year ending December 31, 2018, follow. Gibson acquired a 60 percent interest in Keller on January 1, 2017, in exchange for various considerations totaling $930,000. At the acquisition date, the fair value of the noncon...
1 answer
5. [1/3 Points] DETAILS PREVIOUS ANSWERS BBUNDERSTAT12 3.2.005.MI Consider the data set. 2, 3, 5, 6,...
5. [1/3 Points] DETAILS PREVIOUS ANSWERS BBUNDERSTAT12 3.2.005.MI Consider the data set. 2, 3, 5, 6, 9 (a) Find the range. 17 (b) Use the defining formula to compute the sample standard deviation s. (Round you 2.7 (c) Use the defining formula to compute the population standard deviation o. (Round 2....
1 answer
Use the Gauss-Seidel Method to solve the set of equations given below. Arrange the equations so...
Use the Gauss-Seidel Method to solve the set of equations given below. Arrange the equations so as to guarantee convergence. Start with X-X2 X3-1 and perform at least 3 iterations. 2x +6x2+16x3=47 14x +4x2+5x3=56 4x1+17x2+3x3=25...
1 answer
2-2 Chemoautotrophic bacteria are responsible for the oxidation of NH:-N to NO -N in many biologi...
please help 2-2 Chemoautotrophic bacteria are responsible for the oxidation of NH:-N to NO -N in many biological wastewater treatment processes. These bacteria, collectively called nitrifiers, consist of the genera Nitrosomonar and Ntro- bacter. The oxidation of ammonia to nitrate is considered ...
1 answer
Fill in the missing items In the balance sheet. PURRFECT PETS, INC Balance Sheet September 30,...
Fill in the missing items In the balance sheet. PURRFECT PETS, INC Balance Sheet September 30, Year 3 Assets Cash Accounts Receivable Inventories Equipment Other Assets S 745,800 55,100 121,300 73.400 Total Assets Liabilities Accounts Payable Notes Payable S 343,500 Total Liabilities $ 609,600 Stock...
1 answer
Assume a function F(a,b,c) is 0 when abc-010 or abc=101, and is 1 when abc are...
Assume a function F(a,b,c) is 0 when abc-010 or abc=101, and is 1 when abc are otherwise. Which Karnaugh-map correctly describes set of prime implicants for a minimum sum-of-products for the function F? C ab ab ab 00 00 00 01 01 10 11 11 10 11 10 3 2 1 O 1 only 2 only 03 only Both 1 and 2 are correc...
1 answer
Write formal report on impact of covid 19 on Domino's?
write formal report on impact of covid 19 on Domino's?...
1 answer
(1 point) Find the maximum volume of a rectangular box that can be inscribed in the...
(1 point) Find the maximum volume of a rectangular box that can be inscribed in the ellipsoid x2 y2 + + 1. 81 9 49 with sides parallel to the coordinate axes. Volume =...
1 answer
Force vectors
Determine themagnitude of the resultant force by adding the rectangular components of the three forces....
1 answer
Case 5.1: Incentives in the Firm – Managing the Carpet Store Jerry runs a small carpet...
Case 5.1: Incentives in the Firm – Managing the Carpet Store Jerry runs a small carpet store. His firm sells and installs carpets. He manages the operation from his office in the store where carpet is displayed. He pays his receptionist/secretary an hourly wage. He has a saleswoman in the offi...
1 answer
Singing Fish Fine Foods has a current annual cash dividend policy of $2.75. The price of...
Singing Fish Fine Foods has a current annual cash dividend policy of $2.75. The price of the stock is set to yield a return of 12%. What is the price of this stock if the dividend will be paid a. for 11 years? b. for 17 years? c. for 41 years? d. for 60 years? e. for 100 years? f. forever? a. What i...
1 answer
Problem 7.10 In the circuit the switch has been closed for a long time before opening...
Problem 7.10 In the circuit the switch has been closed for a long time before opening at t=0. (Figure 1) Part A Find the value of L so that v. (t) equals 0.5v. (0+) when t= 2 ms. Take R = 11 12 Express your answer with the appropriate units. TH TA ? L = 9 H Submit Previous Answers Request Answer * I...