/** Simulates a frog attempting to reach the goal as described in part (a).
* Returns true if the frog successfully reached or passed the goal during the simulation;
* false otherwise.
*/
public boolean simulate() {
int position = 0; // frog starts at 0
for (int count = 0; count < maxHops; count++) { // maxHops is the max the frog can hop, and after each hop the count goes up by one, the loop repeating until count = maxHops
position += hopDistance();
if (position >= goalDistance) { // if the frog goes farther than needed, return true
return true;
}
else if (position < 0) { // if the frog isn't there, return false and keep going
return false;
}
}
return false; // if the hops are taken up without the goal being met, it returns false
}
/** Runs num simulations and returns the proportion of simulations in which the frog
* successfully reached or passed the goal.
* Precondition: num > 0
*/
public double runSimulations(int num) { // double makes it so the value can be a decimal
int countSuccess = 0; // starts at 0
for (int count = 0; count < num; count++) { // checks if the simulation was successful for each time it was run
if(simulate()) { // if it turns out true, raise the count of the number of successful runs
countSuccess++;
}
}
return (double)countSuccess / num; // divides the number of successes to the overall amount of simulations to find the final result
}
public class CaesarCipher {
public static void main(String[] args) {
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
String message1 = "kfzb gly";
String message2 = "zlab zlab zlab";
String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
String cipher(String msg, int shift) {
String s = "";
int len = msg.length();
for(int x = 0; x < len; x++) {
letters c = (letters)(msg.charAt(x) + shift);
if (c > "z")
s += (letters)(msg.charAt(x) - (26-shift));
else
s += (letters)(msg.charAt(x) + shift);
}
return s;
}
System.out.println(cipher(message1, 3));
System.out.println(cipher(message2, 3));
System.out.println(cipher(message3, 3));
}
}
CaesarCipher.main(null);