Teacher’s Notes:
=======================================
Students found that this program was fairly straight forward to write but oddly satisfying to watch.
You should probably change the sample from my name to yours. If your name is not 4 letters, then you will need to change the target to something that has the same number of letters. Also, you should change the disclaimer in the NOTE to be your name. My students thought it was pretty funny.
Assignment:
=======================================
Word Evolution
NOTE:
=======================================
The random nature of this assignment means that there is no automatic testing of your code.
Also, the sample input/output implies that Chow is not already the best. Rest assured this is only an example and I am still (and always will be) the best.
=======================================
Can a word evolve into another word? In this assignment, you will read a String w from the keyboard where w represents an original word. You will then read another String t from the keyboard where t represents the target word that the original word will evolve into. Both the original and target words will be only comprised of lower case letters and be the same length.
In each generation, one random letter from the original word will change into another random letter. However, if a letter in position x of the original word matches the letter in position x of the target word, it is considered to be in the optimal state for the current environment and thus, will not be changed any further.
Write a program that simulates this process, outputting the state of the word as it changes into the target. Once the word has changed into the target, print the number of generations that were required for this to happen.
THERE ARE NO PROMPTS OR ERROR TRAPS REQUIRED FOR THIS PROGRAM
SAMPLE INPUT 1:
chow
best
SAMPLE OUTPUT 1 (Since you are generating things randomly, your results will vary from this):
chow
bhow
bpow
bpon
bpoh
bpoh
bpou
bpox
bpgx
bhgx
bhyx
bhyi
bhyi
bpyi
bpyy
bhyy
bhmy
blmy
bqmy
bqny
bqni
bxni
bxna
brna
bhna
bhny
bhcy
bbcy
bbct
bsct
bsft
bsqt
bsyt
blyt
blbt
blgt
bhgt
bygt
bogt
boht
boet
bobt
blbt
bljt
blot
blvt
bevt
beut
best
48
Solution (JAVA):
=======================================
import java.util.Scanner;
public class Main {
public static boolean theSame(char[] word, char[] target) {
for(int x = 0; x < word.length; x++)
if(word[x] != target[x])
return false;
return true;
}
public static void print(char[] word) {
for(int x = 0; x < word.length; x++)
System.out.print(word[x]);
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[] word = input.next().toCharArray();
char[] target = input.next().toCharArray();
int randomPosition = 0, generations = 0;
while(!theSame(word,target)) {
print(word);
do {
randomPosition = (int)(Math.random()*word.length);
}while(word[randomPosition] == target[randomPosition]);
word[randomPosition] = (char)(Math.random()*26+97);
generations++;
}
print(word);
System.out.println(generations);
}
}